mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-10 23:54:57 +08:00
新增 {{py}} 和 {{js}} 两个特殊变量
This commit is contained in:
parent
00e94588d5
commit
0cd878adfb
@ -4,7 +4,8 @@ const child_process = require("child_process")
|
|||||||
const iconv = require('iconv-lite')
|
const iconv = require('iconv-lite')
|
||||||
const electron = require('electron')
|
const electron = require('electron')
|
||||||
const {
|
const {
|
||||||
NodeVM
|
NodeVM,
|
||||||
|
VM
|
||||||
} = require('./lib/vm2')
|
} = require('./lib/vm2')
|
||||||
const path = require("path")
|
const path = require("path")
|
||||||
const util = require("util")
|
const util = require("util")
|
||||||
@ -187,6 +188,18 @@ if (process.platform == 'win32') quickcommand.runVbs = function(script) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// python -c
|
||||||
|
runPythonCommand = py => {
|
||||||
|
try {
|
||||||
|
return child_process.execFileSync("python", ["-c", py], {
|
||||||
|
windowsHide: true,
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
utools.showNotification(e)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 在终端中执行
|
// 在终端中执行
|
||||||
if (process.platform !== 'linux') quickcommand.runInTerminal = function(cmdline, dir) {
|
if (process.platform !== 'linux') quickcommand.runInTerminal = function(cmdline, dir) {
|
||||||
@ -347,6 +360,10 @@ let parseItem = item => {
|
|||||||
|
|
||||||
let parseStdout = stdout => stdout.map(x => parseItem(x)).join("\n")
|
let parseStdout = stdout => stdout.map(x => parseItem(x)).join("\n")
|
||||||
|
|
||||||
|
VmEval = (cmd, sandbox = {}) => new VM({
|
||||||
|
sandbox: sandbox
|
||||||
|
}).run(cmd)
|
||||||
|
|
||||||
// The vm module of Node.js is deprecated in the renderer process and will be removed
|
// The vm module of Node.js is deprecated in the renderer process and will be removed
|
||||||
runCodeInVm = (cmd, cb) => {
|
runCodeInVm = (cmd, cb) => {
|
||||||
const vm = createNodeVM()
|
const vm = createNodeVM()
|
||||||
|
@ -119,7 +119,7 @@ export default {
|
|||||||
assignSpecialVars(cmd) {
|
assignSpecialVars(cmd) {
|
||||||
let spVars = _.filter(specialVars, (sp) => sp.repl);
|
let spVars = _.filter(specialVars, (sp) => sp.repl);
|
||||||
_.forIn(spVars, (val, key) => {
|
_.forIn(spVars, (val, key) => {
|
||||||
if (cmd.includes(val.label.slice(0, 12))) {
|
if (cmd.includes(val.label.slice(0, -2))) {
|
||||||
cmd = cmd.replace(val.match, (x) => val.repl(x));
|
cmd = cmd.replace(val.match, (x) => val.repl(x));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -180,7 +180,7 @@ export default {
|
|||||||
showRunResult(content, isSuccess, action) {
|
showRunResult(content, isSuccess, action) {
|
||||||
this.isResultShow = true;
|
this.isResultShow = true;
|
||||||
this.runResultStatus = isSuccess;
|
this.runResultStatus = isSuccess;
|
||||||
let contlength = content.length;
|
let contlength = content?.length || 0;
|
||||||
if (contlength > this.resultMaxLength)
|
if (contlength > this.resultMaxLength)
|
||||||
content =
|
content =
|
||||||
content.slice(0, this.resultMaxLength - 100) +
|
content.slice(0, this.resultMaxLength - 100) +
|
||||||
|
@ -8,21 +8,21 @@ let escapeItem = item => {
|
|||||||
return item.replace('$', '$$$')
|
return item.replace('$', '$$$')
|
||||||
}
|
}
|
||||||
|
|
||||||
let parseTheFirstLayerOfObjects = obj => {
|
let handlingJsonVar = (jsonVar, name) => {
|
||||||
let matched = /{{(\w+)(\[(\d+)\]){0,1}\.(\w+)}}/.exec(obj);
|
try {
|
||||||
return matched ? {
|
return escapeItem(window.VmEval(jsonVar.slice(2, -2), {
|
||||||
obj: matched[1],
|
[name]: quickcommand.enterData.payload
|
||||||
index: matched[3],
|
}))
|
||||||
prop: matched[4],
|
} catch {
|
||||||
} : {};
|
return ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let handlingJsonVar = (jsonVar, srcObj) => {
|
let handlingJsExpression = js => {
|
||||||
try {
|
try {
|
||||||
let parsed = parseTheFirstLayerOfObjects(jsonVar);
|
return window.VmEval(js.slice(5, -2), {
|
||||||
if (!parsed.obj) return escapeItem(srcObj)
|
utools: window.getuToolsLite(),
|
||||||
else if (!parsed.index) return escapeItem(srcObj[parsed.prop])
|
})
|
||||||
else return escapeItem(srcObj[parsed.index][parsed.prop])
|
|
||||||
} catch {
|
} catch {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -63,10 +63,9 @@ const specialVars = {
|
|||||||
},
|
},
|
||||||
subinput: {
|
subinput: {
|
||||||
name: "subinput",
|
name: "subinput",
|
||||||
label: "{{subinput}}",
|
label: "{{subinput:请输入}}",
|
||||||
disabledType: [],
|
disabledType: [],
|
||||||
tooltip: `可以自定义占位符,如{{subinput:请输入}}`,
|
desc: "子输入框的文本,冒号后为占位符",
|
||||||
desc: "子输入框的文本",
|
|
||||||
match: /{{subinput(:.+?){0,1}}}/mg,
|
match: /{{subinput(:.+?){0,1}}}/mg,
|
||||||
},
|
},
|
||||||
input: {
|
input: {
|
||||||
@ -86,11 +85,10 @@ const specialVars = {
|
|||||||
WindowInfo: {
|
WindowInfo: {
|
||||||
name: "WindowInfo",
|
name: "WindowInfo",
|
||||||
label: "{{WindowInfo}}",
|
label: "{{WindowInfo}}",
|
||||||
desc: "当前窗口信息,JSON格式字符串",
|
desc: "当前窗口信息,JSON格式,可以指定键值,如{{WindowInfo.id}}",
|
||||||
tooltip: `可以选择性读取其中的某一个属性,如{{WindowInfo.id}}`,
|
|
||||||
type: "json",
|
type: "json",
|
||||||
match: /{{WindowInfo(\.\w{1,7}){0,1}}}/mg,
|
match: /{{WindowInfo(.*?)}}/mg,
|
||||||
repl: jsonVar => handlingJsonVar(jsonVar, quickcommand.enterData.payload)
|
repl: jsonVar => handlingJsonVar(jsonVar, "WindowInfo")
|
||||||
},
|
},
|
||||||
SelectFile: {
|
SelectFile: {
|
||||||
name: "SelectFile",
|
name: "SelectFile",
|
||||||
@ -102,25 +100,43 @@ const specialVars = {
|
|||||||
MatchedFiles: {
|
MatchedFiles: {
|
||||||
name: "MatchedFiles",
|
name: "MatchedFiles",
|
||||||
label: "{{MatchedFiles}}",
|
label: "{{MatchedFiles}}",
|
||||||
tooltip: `可以选择性读取其中的某一个属性,如{{MatchedFiles[0].path}}`,
|
desc: "匹配的文件,JSON格式,可以指定键值,如{{MatchedFiles[0].path}}",
|
||||||
desc: "匹配的文件,JSON格式字符串",
|
|
||||||
type: "json",
|
type: "json",
|
||||||
match: /{{MatchedFiles(\[\d+\]){0,1}(\.\w{1,11}){0,1}}}/mg,
|
match: /{{MatchedFiles(.*?)}}/mg,
|
||||||
repl: jsonVar => handlingJsonVar(jsonVar, quickcommand.enterData.payload)
|
repl: jsonVar => handlingJsonVar(jsonVar, "MatchedFiles")
|
||||||
},
|
},
|
||||||
type: {
|
type: {
|
||||||
name: "type",
|
name: "type",
|
||||||
label: "{{type}}",
|
label: "{{type}}",
|
||||||
desc: "专业模式的type",
|
desc: "onPluginEnter的type,匹配的类型",
|
||||||
match: /{{type}}/mg,
|
match: /{{type}}/mg,
|
||||||
repl: () => quickcommand.enterData.type
|
repl: () => quickcommand.enterData.type
|
||||||
},
|
},
|
||||||
payload: {
|
payload: {
|
||||||
name: "payload",
|
name: "payload",
|
||||||
label: "{{payload}}",
|
label: "{{payload}}",
|
||||||
desc: "专业模式的payload",
|
desc: "onPluginEnter的payload,当为JSON时可以指定键值,如{{payload.id}}",
|
||||||
match: /{{payload}}/mg,
|
type: "json",
|
||||||
repl: () => escapeItem(quickcommand.enterData.payload)
|
match: /{{payload(.*?)}}/mg,
|
||||||
|
repl: jsonVar => handlingJsonVar(jsonVar, "payload")
|
||||||
|
},
|
||||||
|
js: {
|
||||||
|
name: "js",
|
||||||
|
label: "{{js:}}",
|
||||||
|
desc: "获取js表达式的值,如{{js:utools.isMacOs()}}",
|
||||||
|
tooltip: "注意,必须为表达式而非语句,类似Vue的文本插值。不支持异步/Node",
|
||||||
|
type: "command",
|
||||||
|
match: /{{js:(.*?)}}/mg,
|
||||||
|
repl: js => handlingJsExpression(js)
|
||||||
|
},
|
||||||
|
python: {
|
||||||
|
name: "python",
|
||||||
|
label: "{{py:}}",
|
||||||
|
desc: "模拟python -c,并获取返回值,如{{py:print(1)}}",
|
||||||
|
tooltip: "只支持单行语句",
|
||||||
|
type: "command",
|
||||||
|
match: /{{py:(.*?)}}/mg,
|
||||||
|
repl: py => window.runPythonCommand(py.slice(5, -2))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user