Merge branch 'quickcommand3' of https://github.com/fofolee/uTools-quickcommand into quickcommand3

This commit is contained in:
fofolee
2022-04-29 22:56:41 +08:00
6 changed files with 92 additions and 63 deletions

View File

@@ -69,6 +69,8 @@ export default {
frameInitHeight: 0,
childProcess: null,
timeStamp: null,
urlReg:
/^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/,
};
},
props: {
@@ -102,15 +104,18 @@ export default {
},
async fire(currentCommand) {
currentCommand.cmd = this.assignSpecialVars(currentCommand.cmd);
this.enableHtml = currentCommand.output === "html";
if (currentCommand.output === "html") {
this.enableHtml = true;
currentCommand.cmd = await this.cacheScript(currentCommand.cmd);
}
let { hideWindow, outPlugin, action } =
outputTypes[currentCommand.output];
// 需要隐藏的提前隐藏窗口
hideWindow && utools.hideMainWindow();
// 对于本身就没有输出的命令,无法确认命令是否执行完成,所以干脆提前退出插件
// 弊端就是如果勾选了隐藏后台就完全退出的话,会造成命令直接中断
let quitBeforeShowResult = this.fromUtools && outPlugin;
quitBeforeShowResult &&
let earlyExit = this.fromUtools && outPlugin;
earlyExit &&
setTimeout(() => {
utools.outPlugin();
}, 500);
@@ -118,19 +123,12 @@ export default {
window.runCodeInSandbox(
currentCommand.cmd,
(stdout, stderr) => {
if (stderr) {
return quitBeforeShowResult
? alert(stderr)
: this.showRunResult(stderr, false, action);
}
outPlugin
? action(stdout.toString())
: this.showRunResult(stdout, true);
this.handleResult(stdout, stderr, { outPlugin, action, earlyExit });
},
{ enterData: this.$root.enterData }
);
} else if (currentCommand.program === "html") {
this.showRunResult(currentCommand.cmd, true, action);
this.showRunResult(currentCommand.cmd, true);
} else {
let option =
currentCommand.program === "custom"
@@ -143,14 +141,7 @@ export default {
option,
currentCommand.output === "terminal",
(stdout, stderr) => {
if (stderr) {
return quitBeforeShowResult
? alert(stderr)
: this.showRunResult(stderr, false, action);
}
outPlugin
? action(stdout.toString())
: this.showRunResult(stdout, true);
this.handleResult(stdout, stderr, { outPlugin, action, earlyExit });
}
);
// ctrl c 终止
@@ -235,6 +226,16 @@ export default {
payload: await commandTypes[type]?.tempPayload?.(),
};
},
handleResult(stdout, stderr, options) {
if (stderr) {
return options.earlyExit
? alert(stderr)
: this.showRunResult(stderr, false);
}
options.outPlugin
? options.action(stdout.toString())
: this.showRunResult(stdout, true);
},
// 显示运行结果
showRunResult(content, isSuccess) {
this.isResultShow = true;
@@ -278,6 +279,19 @@ export default {
frameLoad(initHeight) {
this.frameInitHeight = initHeight;
},
// 预先下载远程脚本
async cacheScript(cmd) {
let html = quickcommand.htmlParse(cmd);
let scriptDoms = html.querySelectorAll("script");
for (let i = 0; i < scriptDoms.length; i++) {
let src = scriptDoms[i].src;
if (!this.urlReg.test(src)) continue;
let dest = window.getQuickcommandTempFile("js", "remoteScript_" + i);
await quickcommand.downloadFile(src, dest);
scriptDoms[i].src = "file://" + dest;
}
return html.documentElement.innerHTML;
},
},
unmounted() {
this.stopRun();

View File

@@ -82,10 +82,21 @@ export default {
frameInit() {
let cfw = this.$refs?.iframe?.contentWindow;
if (!cfw) return;
let showError = (...args) => {
quickcommand.showMessageBox(args.join(" "), "error", 0);
};
let showLog = (...args) => {
quickcommand.showMessageBox(args.join(" "), "success", 0);
};
let ctx = {
quickcommand: _.cloneDeep(quickcommand),
utools: _.cloneDeep(utools),
parent: undefined,
console: {
log: showLog,
error: showError,
},
onerror: (e) => showError(e),
};
Object.assign(cfw, ctx);
cfw.onload = () => {

View File

@@ -177,7 +177,7 @@ export default {
argvs: imgUrl,
readfile: false,
});
let imgPath = window.getQuickcommandTempFile(imgInfo.ext);
let imgPath = window.getQuickcommandTempFile(imgInfo.ext, 'TempImgFile');
quickcommand
.downloadFile(imgUrl, imgPath)
.then(() => {

View File

@@ -92,6 +92,15 @@ export default {
message: message,
timeout: time,
position: "top",
actions:
time === 0
? [
{
label: "确定",
color: "white",
},
]
: [],
});
},

View File

@@ -138,7 +138,7 @@ interface quickcommandApi {
* ```
* @param message 显示的消息内容
* @param icon 图标,默认为 success
* @param time 多少毫秒后消失,默认为 3000
* @param time 多少毫秒后消失,默认为 3000,当设为 0 时则需要手动点击关闭
*/
showMessageBox(
message: string,
@@ -352,18 +352,6 @@ interface quickcommandApi {
*/
simulatePaste();
/**
* 读取剪贴板
*/
readClipboard(): text<string>;
/**
* 写入剪贴板
*
* @param text 要写入的文本
*/
writeClipboard(text: string);
/**
* 唤醒 uTools
*