From c095084c352324ae6e341a14e47dcfac0f6fe1cc Mon Sep 17 00:00:00 2001
From: fofolee
@@ -912,8 +915,9 @@
let mode = $('#program').val();
$('.customscript').hide();
$('.quickactions').hide();
- $('#scptarg').show();
+ $('#scptarg, #charset').show();
$('#showInTerm').prop("disabled", false);
+ $('#charset').data(autoCharset(mode));
if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`);
switch (mode) {
case 'custom':
@@ -923,7 +927,7 @@
break;
case 'quickcommand':
$('.quickactions').show();
- $('#scptarg').hide();
+ $('#scptarg, #charset').hide();
$('#showInTerm').prop("disabled", true);
mode = 'javascript';
break;
@@ -1065,10 +1069,10 @@
$('#custombin').show().val(data.customOptions.bin);
$('#customarg').show().val(data.customOptions.argv);
$('#customext').show().val(data.customOptions.ext);
- $('#customcodec').show().val(data.customOptions.codec);
}
typeCheck();
programCheck();
+ if (data.charset) $("#charset").data(data.charset);
// 分段载入,保障动画流畅
if (animate) {
window.editor.setValue(data.cmd.slice(0, 2000));
@@ -1088,6 +1092,31 @@
editCurrentCommand(data)
})
+
+ // 编码设置
+ $("#options").on('click', '#charset', function () {
+ var html = `
+ 脚本编码:
+ 输出解码:
+ `
+ 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, 查看支持的编码`,
+ preConfirm: () => {
+ let scriptCode = document.getElementById('scriptCode').value
+ let outputCode = document.getElementById('outputCode').value
+ $('#charset').data({scriptCode: scriptCode, outputCode: outputCode})
+ }
+ })
+ })
+
// 添加模拟按键
$("#options").on('click', '#addKey', function () {
$("#addKey").text("▶ 录制中").addClass('record')
@@ -1471,6 +1500,7 @@
code = $("#code").val(),
tags = $('#tags').val(),
rule = $('#rule').val(),
+ charset = $('#charset').data()
cmd = window.editor.getValue();
if (tags && tags.includes("默认") && !isDev()) return
if (type != "window" && !rule) return quickcommand.showMessageBox(`${$('#ruleWord').text().replace(" ", "")} 不能留空!`, 'error')
@@ -1554,7 +1584,8 @@
cmd: cmd,
output: output,
hasSubInput: hasSubInput,
- scptarg: scptarg
+ scptarg: scptarg,
+ charset: charset
}
if (extraInfo) {
Object.assign(pushData, extraInfo)
@@ -1568,8 +1599,7 @@
pushData.customOptions = {
"bin": $('#custombin').val(),
"argv": $('#customarg').val(),
- "ext": $('#customext').val(),
- 'codec': $('#customcodec').val()
+ "ext": $('#customext').val()
}
}
putDB(pushData, QC_PREFIX + code);
@@ -1690,10 +1720,10 @@
if (program == "custom") option = {
"bin": $('#custombin').val(),
"argv": $('#customarg').val(),
- "ext": $('#customext').val(),
- 'codec': $('#customcodec').val()
+ "ext": $('#customext').val()
}
option.scptarg = $('#scptarg').val()
+ option.charset = $('#charset').data()
runCodeFile(cmd, option, terminal, (stdout, stderr) => {
if (terminal) return
if (stderr) return showRunResult(stderr, raw, false)
@@ -1731,9 +1761,9 @@
-
运 行
+ 编码设置
格式化
@@ -1766,7 +1796,6 @@
$('#custombin').val(custom.custombin)
$('#customarg').val(custom.customarg)
$('#customext').val(custom.customext)
- $('#customcodec').val(custom.customcodec)
}
}
programCheck()
diff --git a/src/preload.js b/src/preload.js
index 5cce4dc..4f0f01c 100644
--- a/src/preload.js
+++ b/src/preload.js
@@ -776,10 +776,11 @@ runCodeFile = (cmd, option, terminal, callback) => {
var bin = option.bin,
argv = option.argv,
ext = option.ext,
+ charset = option.charset,
scptarg = option.scptarg || "";
let script = getQuickCommandScriptFile(ext)
// 批处理和 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);
// var argvs = [script]
// if (argv) {
@@ -828,12 +829,12 @@ runCodeFile = (cmd, option, terminal, callback) => {
// err_chunks = [];
console.log('running: ' + cmdline);
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)
// chunks.push(chunk)
})
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())
// err_chunks.push(err_chunk)
})