feat: 支持自定义脚本的编码和输出的解码方式

This commit is contained in:
fofolee 2020-12-29 19:41:05 +08:00
parent 4699473145
commit c095084c35
2 changed files with 53 additions and 23 deletions

View File

@ -57,8 +57,7 @@
if (program == 'custom') customoptions = { if (program == 'custom') customoptions = {
custombin: $('#custombin').val(), custombin: $('#custombin').val(),
customarg: $('#customarg').val(), customarg: $('#customarg').val(),
customext: $('#customext').val(), customext: $('#customext').val()
customcodec: $('#customcodec').val()
} }
putDB({ cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, CFG_PREFIX + 'codeHistory') putDB({ cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, CFG_PREFIX + 'codeHistory')
} }
@ -102,6 +101,7 @@
option = programs[db.program]; option = programs[db.program];
} }
option.scptarg = db.scptarg option.scptarg = db.scptarg
option.charset = db.charset ? db.charset : autoCharset(db.program)
cmd = special(cmd); cmd = special(cmd);
if (cmd.includes('{{type}}')) { if (cmd.includes('{{type}}')) {
cmd = cmd.replace(/\{\{type\}\}/mg, type) cmd = cmd.replace(/\{\{type\}\}/mg, type)
@ -414,21 +414,18 @@
bin: '', bin: '',
argv: '', argv: '',
ext: 'bat', ext: 'bat',
codec: 'gbk',
color: '#C1F12E' color: '#C1F12E'
}, },
powershell: { powershell: {
bin: 'powershell', bin: 'powershell',
argv: '-NoProfile -File', argv: '-NoProfile -File',
ext: 'ps1', ext: 'ps1',
codec: utools.isWindows() ? 'gbk' : '',
color: '#012456' color: '#012456'
}, },
python: { python: {
bin: 'python', bin: 'python',
argv: '-u', argv: '-u',
ext: 'py', ext: 'py',
codec: utools.isWindows() ? 'gbk' : '',
color: '#3572A5' color: '#3572A5'
}, },
javascript: { javascript: {
@ -453,14 +450,12 @@
bin: 'gcc', bin: 'gcc',
argv: '-o', argv: '-o',
ext: 'c', ext: 'c',
codec: utools.isWindows() ? 'gbk' : '',
color: '#555555' color: '#555555'
}, },
csharp: { csharp: {
bin: 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe', bin: 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe',
argv: '/Nologo', argv: '/Nologo',
ext: 'cs', ext: 'cs',
codec: 'gbk',
color: '#178600' color: '#178600'
}, },
lua: { lua: {
@ -479,11 +474,19 @@
bin: '', bin: '',
argv: '', argv: '',
ext: '', ext: '',
codec: '',
color: '#438eff' color: '#438eff'
} }
} }
// 自动设置编码
let autoCharset = program => {
let charset = { scriptCode: '', outputCode: '' }
if (!utools.isWindows()) return charset
if (program == 'powershell' || program == 'cmd') charset.scriptCode = 'GBK'
if (['cmd', 'powershell', 'python', 'c', 'csharp'].includes(program)) charset.outputCode = 'GBK'
return charset
}
let getCmdsType = cmds => { let getCmdsType = cmds => {
try { try {
JSON.stringify(cmds) JSON.stringify(cmds)
@ -689,8 +692,8 @@
<input type="text" id="custombin" placeholder="解释器路径"> <input type="text" id="custombin" placeholder="解释器路径">
<input type="text" id="customarg" placeholder="解释器参数"> <input type="text" id="customarg" placeholder="解释器参数">
<input type="text" id="customext" placeholder="后缀,不含."> <input type="text" id="customext" placeholder="后缀,不含.">
<input type="text" id="customcodec" placeholder="输出编码">
</span> </span>
<span id="charset" class="footBtn robot">编码设置</span>
<span class="quickactions"> <span class="quickactions">
<span id="addAction" class="footBtn robot">动作</span> <span id="addAction" class="footBtn robot">动作</span>
<span id="addKey" class="footBtn robot">按键</span> <span id="addKey" class="footBtn robot">按键</span>
@ -698,7 +701,7 @@
<span id="beautifyCode" class="footBtn robot">格式化</span> <span id="beautifyCode" class="footBtn robot">格式化</span>
</span> </span>
</p> </p>
<textarea id="cmd" placeholder="◆基础◆\nquickcommand环境下点击“﹢按键”来执行模拟按键的操作;点击“﹢动作”添加打开软件,访问网址等\n常用动作\n◆进阶◆\nquickcommand可使用nodejs、electron、uTools、quickCommand的api详情查看文档\n其他脚本本机装了相应环境即可执行可以直接拖放脚本文件至此处可在脚本参数输入框处填写传递\n给脚本的参数\ncustom可以手动设置解释器路径、参数、脚本后缀及编码方式\n◆快捷键◆\n支持VSCode快捷键\nAlt+Enter 全屏\nCtrl+B 运行\nCtrl+F 搜索\nShift+Alt+F 格式化仅JS/PY"></textarea> <textarea id="cmd" placeholder="◆基础◆\nquickcommand环境下点击“﹢按键”来执行模拟按键的操作;点击“﹢动作”添加打开软件,访问网址等\n常用动作\n◆进阶◆\nquickcommand可使用nodejs、electron、uTools、quickCommand的api详情查看文档\n其他脚本本机装了相应环境即可执行可以直接拖放脚本文件至此处可在脚本参数输入框处填写传递\n给脚本的参数\ncustom可以手动设置解释器路径、参数及脚本后缀\n◆快捷键◆\n支持VSCode快捷键\nAlt+Enter 全屏\nCtrl+B 运行\nCtrl+F 搜索\nShift+Alt+F 格式化仅JS/PY"></textarea>
<p class="bottom"> <p class="bottom">
<img id="win32" class="platform" src="./img/win32.svg"> <img id="win32" class="platform" src="./img/win32.svg">
<img id="darwin" class="platform" src="./img/darwin.svg"> <img id="darwin" class="platform" src="./img/darwin.svg">
@ -912,8 +915,9 @@
let mode = $('#program').val(); let mode = $('#program').val();
$('.customscript').hide(); $('.customscript').hide();
$('.quickactions').hide(); $('.quickactions').hide();
$('#scptarg').show(); $('#scptarg, #charset').show();
$('#showInTerm').prop("disabled", false); $('#showInTerm').prop("disabled", false);
$('#charset').data(autoCharset(mode));
if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`); if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`);
switch (mode) { switch (mode) {
case 'custom': case 'custom':
@ -923,7 +927,7 @@
break; break;
case 'quickcommand': case 'quickcommand':
$('.quickactions').show(); $('.quickactions').show();
$('#scptarg').hide(); $('#scptarg, #charset').hide();
$('#showInTerm').prop("disabled", true); $('#showInTerm').prop("disabled", true);
mode = 'javascript'; mode = 'javascript';
break; break;
@ -1065,10 +1069,10 @@
$('#custombin').show().val(data.customOptions.bin); $('#custombin').show().val(data.customOptions.bin);
$('#customarg').show().val(data.customOptions.argv); $('#customarg').show().val(data.customOptions.argv);
$('#customext').show().val(data.customOptions.ext); $('#customext').show().val(data.customOptions.ext);
$('#customcodec').show().val(data.customOptions.codec);
} }
typeCheck(); typeCheck();
programCheck(); programCheck();
if (data.charset) $("#charset").data(data.charset);
// 分段载入,保障动画流畅 // 分段载入,保障动画流畅
if (animate) { if (animate) {
window.editor.setValue(data.cmd.slice(0, 2000)); window.editor.setValue(data.cmd.slice(0, 2000));
@ -1088,6 +1092,31 @@
editCurrentCommand(data) editCurrentCommand(data)
}) })
// 编码设置
$("#options").on('click', '#charset', function () {
var html = `
脚本编码 <input id="scriptCode" class="swal2-input" placeholder="未出现乱码问题请留空">
输出解码 <input id="outputCode" class="swal2-input" placeholder="未出现乱码问题请留空">
`
Swal.fire({
title: "编码设置",
onBeforeOpen: () => {
let charset = $('#charset').data()
document.getElementById('scriptCode').value = charset.scriptCode || ''
document.getElementById('outputCode').value = charset.outputCode || ''
},
html: html,
showCancelButton: true,
footer: `基于 iconv-lite, 查看支持的<a href="#" onclick="utools.ubrowser.goto('https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings').run()">编码</a>`,
preConfirm: () => {
let scriptCode = document.getElementById('scriptCode').value
let outputCode = document.getElementById('outputCode').value
$('#charset').data({scriptCode: scriptCode, outputCode: outputCode})
}
})
})
// 添加模拟按键 // 添加模拟按键
$("#options").on('click', '#addKey', function () { $("#options").on('click', '#addKey', function () {
$("#addKey").text("▶ 录制中").addClass('record') $("#addKey").text("▶ 录制中").addClass('record')
@ -1471,6 +1500,7 @@
code = $("#code").val(), code = $("#code").val(),
tags = $('#tags').val(), tags = $('#tags').val(),
rule = $('#rule').val(), rule = $('#rule').val(),
charset = $('#charset').data()
cmd = window.editor.getValue(); cmd = window.editor.getValue();
if (tags && tags.includes("默认") && !isDev()) return if (tags && tags.includes("默认") && !isDev()) return
if (type != "window" && !rule) return quickcommand.showMessageBox(`${$('#ruleWord').text().replace(" ", "")} 不能留空!`, 'error') if (type != "window" && !rule) return quickcommand.showMessageBox(`${$('#ruleWord').text().replace(" ", "")} 不能留空!`, 'error')
@ -1554,7 +1584,8 @@
cmd: cmd, cmd: cmd,
output: output, output: output,
hasSubInput: hasSubInput, hasSubInput: hasSubInput,
scptarg: scptarg scptarg: scptarg,
charset: charset
} }
if (extraInfo) { if (extraInfo) {
Object.assign(pushData, extraInfo) Object.assign(pushData, extraInfo)
@ -1568,8 +1599,7 @@
pushData.customOptions = { pushData.customOptions = {
"bin": $('#custombin').val(), "bin": $('#custombin').val(),
"argv": $('#customarg').val(), "argv": $('#customarg').val(),
"ext": $('#customext').val(), "ext": $('#customext').val()
'codec': $('#customcodec').val()
} }
} }
putDB(pushData, QC_PREFIX + code); putDB(pushData, QC_PREFIX + code);
@ -1690,10 +1720,10 @@
if (program == "custom") option = { if (program == "custom") option = {
"bin": $('#custombin').val(), "bin": $('#custombin').val(),
"argv": $('#customarg').val(), "argv": $('#customarg').val(),
"ext": $('#customext').val(), "ext": $('#customext').val()
'codec': $('#customcodec').val()
} }
option.scptarg = $('#scptarg').val() option.scptarg = $('#scptarg').val()
option.charset = $('#charset').data()
runCodeFile(cmd, option, terminal, (stdout, stderr) => { runCodeFile(cmd, option, terminal, (stdout, stderr) => {
if (terminal) return if (terminal) return
if (stderr) return showRunResult(stderr, raw, false) if (stderr) return showRunResult(stderr, raw, false)
@ -1731,9 +1761,9 @@
<input type="text" id="custombin" placeholder="解释器路径"> <input type="text" id="custombin" placeholder="解释器路径">
<input type="text" id="customarg" placeholder="解释器参数"> <input type="text" id="customarg" placeholder="解释器参数">
<input type="text" id="customext" placeholder="后缀,不含."> <input type="text" id="customext" placeholder="后缀,不含.">
<input type="text" id="customcodec" placeholder="输出编码">
</span> </span>
<span id="runCode" class="footBtn robot"> </span> <span id="runCode" class="footBtn robot"> </span>
<span id="charset" class="footBtn robot">编码设置</span>
<input type="text" id="scptarg" placeholder="脚本参数"> <input type="text" id="scptarg" placeholder="脚本参数">
<span class="quickactions"> <span class="quickactions">
<span id="beautifyCode" class="footBtn robot">格式化</span> <span id="beautifyCode" class="footBtn robot">格式化</span>
@ -1766,7 +1796,6 @@
$('#custombin').val(custom.custombin) $('#custombin').val(custom.custombin)
$('#customarg').val(custom.customarg) $('#customarg').val(custom.customarg)
$('#customext').val(custom.customext) $('#customext').val(custom.customext)
$('#customcodec').val(custom.customcodec)
} }
} }
programCheck() programCheck()

View File

@ -776,10 +776,11 @@ runCodeFile = (cmd, option, terminal, callback) => {
var bin = option.bin, var bin = option.bin,
argv = option.argv, argv = option.argv,
ext = option.ext, ext = option.ext,
charset = option.charset,
scptarg = option.scptarg || ""; scptarg = option.scptarg || "";
let script = getQuickCommandScriptFile(ext) let script = getQuickCommandScriptFile(ext)
// 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题 // 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题
if (ext == 'bat' || ext == 'ps1') cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), 'GBK'); if (charset.scriptCode) cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), charset.scriptCode);
fs.writeFileSync(script, cmd); fs.writeFileSync(script, cmd);
// var argvs = [script] // var argvs = [script]
// if (argv) { // if (argv) {
@ -828,12 +829,12 @@ runCodeFile = (cmd, option, terminal, callback) => {
// err_chunks = []; // err_chunks = [];
console.log('running: ' + cmdline); console.log('running: ' + cmdline);
child.stdout.on('data', chunk => { child.stdout.on('data', chunk => {
if (option.codec) chunk = iconv.decode(chunk, option.codec) if (charset.outputCode) chunk = iconv.decode(chunk, charset.outputCode)
callback(chunk.toString(), null) callback(chunk.toString(), null)
// chunks.push(chunk) // chunks.push(chunk)
}) })
child.stderr.on('data', stderr => { child.stderr.on('data', stderr => {
if (option.codec) stderr = iconv.decode(stderr, option.codec) if (charset.outputCode) stderr = iconv.decode(stderr, charset.outputCode)
callback(null, stderr.toString()) callback(null, stderr.toString())
// err_chunks.push(err_chunk) // err_chunks.push(err_chunk)
}) })