增加读写剪贴板的接口

This commit is contained in:
fofolee 2022-04-29 00:38:07 +08:00
parent 54790b5ea5
commit f0e4022e3b
2 changed files with 37 additions and 17 deletions

View File

@ -174,6 +174,14 @@ window.quickcommand = {
process.execPath process.execPath
child_process.exec(uToolsPath, () => {}) child_process.exec(uToolsPath, () => {})
}, },
readClipboard: function() {
return electron.clipboard.readText()
},
writeClipboard: function(text) {
electron.clipboard.writeText(text)
}
} }
// 运行vbs脚本 // 运行vbs脚本
@ -511,7 +519,7 @@ window.runCodeFile = (cmd, option, terminal, callback) => {
charset = option.charset, charset = option.charset,
scptarg = option.scptarg || ""; scptarg = option.scptarg || "";
let script = getQuickcommandTempFile(ext) let script = getQuickcommandTempFile(ext)
// 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题 // 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题
if (charset.scriptCode) cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), charset.scriptCode); 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]
@ -533,27 +541,27 @@ window.runCodeFile = (cmd, option, terminal, callback) => {
// 在终端中输出 // 在终端中输出
if (terminal) cmdline = getCommandToLaunchTerminal(cmdline) if (terminal) cmdline = getCommandToLaunchTerminal(cmdline)
child = child_process.spawn(cmdline, { child = child_process.spawn(cmdline, {
encoding: 'buffer', encoding: 'buffer',
shell: true shell: true
}) })
// var chunks = [], // var chunks = [],
// err_chunks = []; // err_chunks = [];
console.log('running: ' + cmdline); console.log('running: ' + cmdline);
child.stdout.on('data', chunk => { child.stdout.on('data', chunk => {
if (charset.outputCode) chunk = iconv.decode(chunk, charset.outputCode) 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 (charset.outputCode) stderr = iconv.decode(stderr, charset.outputCode) 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)
}) })
// child.on('close', code => { // child.on('close', code => {
// let stdout = chunks.join(""); // let stdout = chunks.join("");
// let stderr = err_chunks.join(""); // let stderr = err_chunks.join("");
// callback(stdout, stderr) // callback(stdout, stderr)
// }) // })
return child return child
} }
@ -591,7 +599,7 @@ window.quickcommandHttpServer = () => {
req.on('end', () => { req.on('end', () => {
let parsedParams let parsedParams
let params = data.join("").toString() let params = data.join("").toString()
// 先尝试作为 json 解析 // 先尝试作为 json 解析
try { try {
parsedParams = JSON.parse(params) parsedParams = JSON.parse(params)
} catch (error) { } catch (error) {

View File

@ -352,6 +352,18 @@ interface quickcommandApi {
*/ */
simulatePaste(); simulatePaste();
/**
*
*/
readClipboard(): text<string>;
/**
*
*
* @param text
*/
writeClipboard(text: string);
/** /**
* uTools * uTools
* *