mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-09 15:04:06 +08:00
新增axios,downloadFile,禁用部分utools函数
This commit is contained in:
parent
2e2e6dcd34
commit
73357b1a87
@ -7,9 +7,10 @@ const { NodeVM } = require('vm2')
|
|||||||
const path = require("path")
|
const path = require("path")
|
||||||
const util = require("util")
|
const util = require("util")
|
||||||
const PinyinMatch = require('pinyin-match');
|
const PinyinMatch = require('pinyin-match');
|
||||||
|
const axios = require('axios');
|
||||||
|
// axios.defaults.adapter = require('axios/lib/adapters/http')
|
||||||
fofoCommon = require('./common').fofo
|
fofoCommon = require('./common').fofo
|
||||||
|
|
||||||
|
|
||||||
if (!utools.isWindows()) process.env.PATH += ':/usr/local/bin:/usr/local/sbin'
|
if (!utools.isWindows()) process.env.PATH += ':/usr/local/bin:/usr/local/sbin'
|
||||||
|
|
||||||
// window.startTime = new Date().getTime()
|
// window.startTime = new Date().getTime()
|
||||||
@ -273,6 +274,26 @@ quickcommand = {
|
|||||||
// dom 解析
|
// dom 解析
|
||||||
htmlParse: function (html) {
|
htmlParse: function (html) {
|
||||||
return new DOMParser().parseFromString(html, 'text/html')
|
return new DOMParser().parseFromString(html, 'text/html')
|
||||||
|
},
|
||||||
|
|
||||||
|
// 下载文件
|
||||||
|
downloadFile: function (url, defaultPath = '', showDialog = false) {
|
||||||
|
return new Promise((reslove, reject) => {
|
||||||
|
var filepath = showDialog ? utools.showSaveDialog({ defaultPath: defaultPath }) : defaultPath
|
||||||
|
axios({
|
||||||
|
method: 'get',
|
||||||
|
url: url,
|
||||||
|
responseType: 'arraybuffer'
|
||||||
|
}).then(res => {
|
||||||
|
var filebuffer = Buffer.from(res.data)
|
||||||
|
if (!filepath) reslove(filebuffer)
|
||||||
|
fs.writeFile(filepath, filebuffer, err => {
|
||||||
|
err && reject(err) || reslove(filebuffer)
|
||||||
|
})
|
||||||
|
}).catch(err => {
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,20 +301,26 @@ swalOneByOne = options => {
|
|||||||
swal.getQueueStep() && Swal.insertQueueStep(options) || Swal.queue([options])
|
swal.getQueueStep() && Swal.insertQueueStep(options) || Swal.queue([options])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 屏蔽危险函数
|
||||||
|
var getuToolsLite = () => {
|
||||||
|
var utoolsLite = Object.assign({}, utools)
|
||||||
|
delete utoolsLite.db
|
||||||
|
delete utoolsLite.removeFeature
|
||||||
|
delete utoolsLite.setFeature
|
||||||
|
return utoolsLite
|
||||||
|
}
|
||||||
|
|
||||||
var getSandboxFuns = () => {
|
var getSandboxFuns = () => {
|
||||||
var sandbox = {
|
var sandbox = {
|
||||||
utools: utools,
|
utools: getuToolsLite(),
|
||||||
quickcommand: Object.assign(quickcommand, {
|
quickcommand: quickcommand,
|
||||||
get: $.get,
|
|
||||||
post: $.post,
|
|
||||||
ajax: $.ajax
|
|
||||||
}),
|
|
||||||
electron: electron,
|
electron: electron,
|
||||||
fs: fs,
|
fs: fs,
|
||||||
path: path,
|
path: path,
|
||||||
os: os,
|
os: os,
|
||||||
child_process: child_process,
|
child_process: child_process,
|
||||||
util: util,
|
util: util,
|
||||||
|
axios: axios,
|
||||||
alert: alert
|
alert: alert
|
||||||
}
|
}
|
||||||
shortCodes.forEach(f => {
|
shortCodes.forEach(f => {
|
||||||
@ -342,6 +369,7 @@ runCodeInVm = (cmd, cb, payload = "") => {
|
|||||||
|
|
||||||
//重定向 console
|
//重定向 console
|
||||||
vm.on('console.log', stdout => {
|
vm.on('console.log', stdout => {
|
||||||
|
console.log(stdout);
|
||||||
cb(parseItem(stdout), null)
|
cb(parseItem(stdout), null)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -353,16 +381,19 @@ runCodeInVm = (cmd, cb, payload = "") => {
|
|||||||
try {
|
try {
|
||||||
vm.run(cmd, path.join(__dirname, 'preload.js'));
|
vm.run(cmd, path.join(__dirname, 'preload.js'));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
cb(null, error.toString())
|
cb(null, error.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
let cbUnhandledError = e => {
|
let cbUnhandledError = e => {
|
||||||
removeAllListener()
|
removeAllListener()
|
||||||
|
console.log(e)
|
||||||
cb(null, e.error.toString())
|
cb(null, e.error.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
let cbUnhandledRejection = e => {
|
let cbUnhandledRejection = e => {
|
||||||
removeAllListener()
|
removeAllListener()
|
||||||
|
console.log(e)
|
||||||
cb(null, e.reason.toString())
|
cb(null, e.reason.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user