mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 14:34:13 +08:00
增加模拟按键的功能
This commit is contained in:
parent
01697fb9c3
commit
54a976f112
69
src/assets/index.js
Executable file → Normal file
69
src/assets/index.js
Executable file → Normal file
@ -10,18 +10,14 @@ utools.onPluginEnter( async ({ code, type, payload }) => {
|
||||
$("#out").show().text('');
|
||||
var db = utools.db.get('customFts').data[code],
|
||||
cmd = db.cmd;
|
||||
if (db.robotjs) {
|
||||
utools.setExpendHeight(0);
|
||||
utools.hideMainWindow();
|
||||
eval(cmd);
|
||||
utools.outPlugin();
|
||||
return;
|
||||
}
|
||||
if (db.program == "custom") {
|
||||
option = db.customOptions;
|
||||
} else {
|
||||
} else if(db.program == "simulation"){
|
||||
option = "simulation";
|
||||
}else{
|
||||
option = programs[db.program];
|
||||
}
|
||||
cmd = await special(cmd);
|
||||
// 正则
|
||||
if (type == 'regex') cmd = cmd.replace(/\{\{input\}\}/mg, payload);
|
||||
// 窗口
|
||||
@ -58,7 +54,7 @@ utools.onPluginEnter( async ({ code, type, payload }) => {
|
||||
if (event.keyCode == 13) {
|
||||
$("#out").text('');
|
||||
var execmd = cmd.replace(/\{\{subinput\}\}/mg, subinput);
|
||||
runCmd(execmd, option, db.codec, db.output);
|
||||
runCmd(execmd, option, db.output);
|
||||
}
|
||||
};
|
||||
setSubInput();
|
||||
@ -68,40 +64,47 @@ utools.onPluginEnter( async ({ code, type, payload }) => {
|
||||
document.removeEventListener('keydown', handleEnter);
|
||||
})
|
||||
} else {
|
||||
runCmd(cmd, option, db.codec, db.output);
|
||||
runCmd(cmd, option, db.output);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function runCmd(cmd, option, codec, output) {
|
||||
function runCmd(cmd, option, output) {
|
||||
if (option == "simulation") {
|
||||
utools.setExpendHeight(0);
|
||||
// utools.hideMainWindow();
|
||||
eval(`(async () => {
|
||||
${cmd}
|
||||
})()`);
|
||||
utools.outPlugin();
|
||||
return;
|
||||
}
|
||||
// 不需要输出的,提前关闭窗口
|
||||
if (['ignore', 'clip', 'send', 'notice', 'terminal'].indexOf(output) !== -1) {
|
||||
utools.hideMainWindow()
|
||||
utools.setExpendHeight(0);
|
||||
utools.outPlugin();
|
||||
// utools.hideMainWindow();
|
||||
}
|
||||
var terminal = false;
|
||||
if(output == 'terminal') terminal = true;
|
||||
// 运行脚本
|
||||
window.run(cmd, option, codec, terminal, (stdout, stderr) => {
|
||||
window.run(cmd, option, terminal, (stdout, stderr) => {
|
||||
if (stderr) {
|
||||
// 报错
|
||||
utools.showMainWindow()
|
||||
Swal.fire({
|
||||
messageBox({
|
||||
type: 'error',
|
||||
title: '啊嘞?!',
|
||||
text: stderr,
|
||||
icon: 'error',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: '转至脚本目录',
|
||||
cancelButtonText: '退出',
|
||||
}).then((result) => {
|
||||
if (result.value) {
|
||||
open(resolve(tmpdir, `QuickCommandTempScript.${option.ext}`));
|
||||
}
|
||||
copyTo(stderr);
|
||||
utools.showNotification("已复制报错信息");
|
||||
utools.outPlugin();
|
||||
})
|
||||
icon: window.logo,
|
||||
message: stderr,
|
||||
buttons: ['转至脚本目录', '退出']
|
||||
}, index => {
|
||||
if (index == 0) {
|
||||
locate(resolve(tmpdir, `QuickCommandTempScript.${option.ext}`));
|
||||
}
|
||||
copyTo(stderr);
|
||||
message("已复制报错信息");
|
||||
utools.outPlugin();
|
||||
})
|
||||
} else if (stdout) {
|
||||
// 有输出
|
||||
switch (output) {
|
||||
@ -113,7 +116,6 @@ function runCmd(cmd, option, codec, output) {
|
||||
break;
|
||||
case "clip":
|
||||
copyTo(stdout);
|
||||
utools.outPlugin();
|
||||
break;
|
||||
case "send":
|
||||
// 暂存用户剪贴板
|
||||
@ -123,15 +125,12 @@ function runCmd(cmd, option, codec, output) {
|
||||
setTimeout(() => {
|
||||
restoreClip(historyData);
|
||||
}, 500);
|
||||
utools.outPlugin();
|
||||
break;
|
||||
case "notice":
|
||||
// 发送系统通知
|
||||
utools.showNotification(stdout);
|
||||
utools.outPlugin();
|
||||
message(stdout);
|
||||
break;
|
||||
case "ignore":
|
||||
utools.outPlugin();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
15
src/assets/options.css
Executable file → Normal file
15
src/assets/options.css
Executable file → Normal file
@ -180,18 +180,10 @@
|
||||
color: rgb(48, 21, 122);
|
||||
}
|
||||
|
||||
#options .keyword.key {
|
||||
color: rgb(122, 115, 21);
|
||||
}
|
||||
|
||||
#options .logo {
|
||||
width: 32px;
|
||||
}
|
||||
|
||||
#options #customize .robot{
|
||||
display: none;
|
||||
}
|
||||
|
||||
#options #customize .keys{
|
||||
width: 10%;
|
||||
}
|
||||
@ -291,6 +283,7 @@
|
||||
|
||||
#options #customize {
|
||||
position: fixed;
|
||||
z-index: 999;
|
||||
height: 100%;
|
||||
top: 100%;
|
||||
left: 0;
|
||||
@ -301,6 +294,10 @@
|
||||
background: white;
|
||||
}
|
||||
|
||||
#options .varoutput{
|
||||
display: none;
|
||||
}
|
||||
|
||||
#options #customize p {
|
||||
line-height: 1.25rem;
|
||||
}
|
||||
@ -399,7 +396,7 @@ input::-webkit-input-placeholder {
|
||||
transition: 0.5s;
|
||||
}
|
||||
|
||||
#options #customize select#vars {
|
||||
#options #customize select#vars,#action {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
|
198
src/assets/options.js
Executable file → Normal file
198
src/assets/options.js
Executable file → Normal file
@ -149,9 +149,10 @@ programs = {
|
||||
}
|
||||
|
||||
showOptions = () => {
|
||||
$("#featureList").remove();
|
||||
var currentFts = utools.getFeatures(),
|
||||
customFts = getCustomFts();
|
||||
let featureList = '<table><tr><td width="40"></td><td width="240">模式</td><td width="270">说明</td><td>启用</td></tr>';
|
||||
let featureList = '<div id="featureList"><table><tr><td width="40"></td><td width="240">模式</td><td width="270">说明</td><td>启用</td></tr>';
|
||||
for (var fts in customFts) {
|
||||
let features = customFts[fts].features;
|
||||
var cmds = '';
|
||||
@ -162,11 +163,7 @@ showOptions = () => {
|
||||
} else if (features.cmds[0].type == 'window') {
|
||||
var app = features.cmds[0].match.app
|
||||
if (app.length > 15) app = app.slice(0, 15) + '...';
|
||||
if (customFts[fts].robotjs) {
|
||||
cmds = `<span class="keyword key">按键: ${app}</span>`;
|
||||
} else {
|
||||
cmds = `<span class="keyword win">窗口: ${app}</span>`;
|
||||
}
|
||||
cmds = `<span class="keyword win">窗口: ${app}</span>`;
|
||||
} else {
|
||||
features.cmds.forEach(cmd => {
|
||||
cmds += `<span class="keyword">${cmd}</span>`;
|
||||
@ -199,8 +196,8 @@ showOptions = () => {
|
||||
<div id="disableAll" class="footBtn danger">全部禁用</div>
|
||||
<div id="enableAll" class="footBtn">全部启用</div>
|
||||
<div id="sample" class="footBtn">下载命令</div>
|
||||
</div>`
|
||||
$("#options").html(featureList);
|
||||
</div></div>`
|
||||
$("#options").append(featureList);
|
||||
}
|
||||
|
||||
showCustomize = () => {
|
||||
@ -213,19 +210,19 @@ showCustomize = () => {
|
||||
<option value="key">通过输入关键字进入插件</option>
|
||||
<option value="regex">通过正则匹配主输入框文本</option>
|
||||
<option value="window">通过呼出uTools前的活动窗口匹配</option>
|
||||
<option value="robotjs">匹配窗口后模拟按键</option>
|
||||
</select>
|
||||
<span class="word" id="ruleWord">关键字</span><input type="text" id="rule" placeholder="多个关键字用逗号隔开"></p>
|
||||
<p><span class="word">说 明</span><input type="text" id="desc" placeholder="命令功能的描述"></p>
|
||||
<p>
|
||||
<span class="word">类 型</span>
|
||||
<select id="program">
|
||||
<option value="simulation">模拟操作</option>
|
||||
${options}
|
||||
</select>
|
||||
<span class="word">图 标</span><input type="text" readonly id="iconame" placeholder="更改图标">
|
||||
<img id="icon" src="">
|
||||
</p>
|
||||
<p>
|
||||
<p class="varoutput">
|
||||
<span class="word">变 量</span>
|
||||
<select id="vars">
|
||||
<option value="" style="display:none">插入特殊变量</option>
|
||||
@ -251,9 +248,8 @@ showCustomize = () => {
|
||||
<option value="terminal">在终端显示</option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<span class="word">脚 本</span>
|
||||
<span>
|
||||
<p class="simulation">
|
||||
<span class="word">操 作</span>
|
||||
<select id="modifier1" class="robot keys">
|
||||
<option value=""></option>
|
||||
<option value="control">control</option>
|
||||
@ -268,15 +264,28 @@ showCustomize = () => {
|
||||
<option value="shift">shift</option>
|
||||
<option value="command">⌘/win</option>
|
||||
</select>
|
||||
<input type="text" id="presskey" class="robot keys" placeholder="按键">
|
||||
<span id="addKey" class="robot footBtn">添加</span>
|
||||
<input type="text" id="keydelay" class="robot keys" placeholder="延时">
|
||||
<span id="addDelay" class="robot footBtn">添加</span>
|
||||
</span>
|
||||
<input type="text" id="presskey" class="robot keys" placeholder="模拟按键">
|
||||
<span id="addKey" class="robot footBtn">﹢按键</span>
|
||||
<input type="text" id="keydelay" class="robot keys" placeholder="等待时间">
|
||||
<span id="addDelay" class="robot footBtn">﹢延时</span>
|
||||
<select id="action" class="robot keys">
|
||||
<option value="" style="display:none">预设动作</option>
|
||||
<option value="open">打开文件</option>
|
||||
<option value="visit">打开网址</option>
|
||||
<option value="locate">定位文件</option>
|
||||
<option value="system">执行命令</option>
|
||||
<option value="copyTo">写剪贴板</option>
|
||||
<option value="message">系统消息</option>
|
||||
<option value="alert">弹窗显示</option>
|
||||
</select>
|
||||
<span id="addAction" class="robot footBtn">﹢动作</span>
|
||||
</p>
|
||||
<p>
|
||||
<span class="word">脚 本</span>
|
||||
<span>
|
||||
<input type="text" id="custombin" class="customscript" placeholder="解释器绝对路径">
|
||||
<input type="text" id="customarg" class="customscript" placeholder="参数">
|
||||
<input type="text" id="customext" class="customscript" placeholder="脚本后缀,不含.">
|
||||
<input type="text" id="custombin" class="customscript" placeholder="解释器绝对路径">
|
||||
<input type="text" id="customarg" class="customscript" placeholder="参数">
|
||||
<input type="text" id="customext" class="customscript" placeholder="脚本后缀,不含.">
|
||||
</span>
|
||||
</p>
|
||||
<p><textarea id="cmd" placeholder="可以直接拖放脚本文件至此处"></textarea></p>
|
||||
@ -285,20 +294,12 @@ showCustomize = () => {
|
||||
<button class="cancelBtn">取消</button>
|
||||
</p>`
|
||||
$("#options").append(customWindow)
|
||||
if (window.isWin) {
|
||||
var shell = 'cmd',
|
||||
mode = 'powershell';
|
||||
} else {
|
||||
var shell = 'shell',
|
||||
mode = 'shell';
|
||||
}
|
||||
$("#program").val(shell);
|
||||
$("#icon").attr('src', `logo/${shell}.png`);
|
||||
$("#icon").attr('src', 'logo/simulation.png');
|
||||
window.editor = CodeMirror.fromTextArea(document.getElementById("cmd"), {
|
||||
lineNumbers: true,
|
||||
lineWrapping: true
|
||||
});
|
||||
window.editor.setOption("mode", mode);
|
||||
window.editor.setOption("mode", 'javascript');
|
||||
$("#customize").animate({ top: '0px' });
|
||||
}
|
||||
|
||||
@ -321,8 +322,10 @@ outputCheck = () => {
|
||||
// 检查模式选项
|
||||
typeCheck = () => {
|
||||
var type = $("#type").val();
|
||||
$("#output, #program, #vars").prop("disabled", false);
|
||||
$(".robot").hide()
|
||||
// $("#output, #program, #vars").prop("disabled", false);
|
||||
// $('.varoutput').show()
|
||||
// $(".simulation").hide()
|
||||
// $("#program").prop("disabled", false)
|
||||
switch (type) {
|
||||
case 'key':
|
||||
$("#ruleWord").html("关键字");
|
||||
@ -336,14 +339,6 @@ typeCheck = () => {
|
||||
$(".var.window").hide()
|
||||
$("#rule").prop("placeholder", '匹配的正则规则,如/\\w+/i');
|
||||
break;
|
||||
case 'robotjs':
|
||||
$("#ruleWord").html("进 程");
|
||||
$("#output, #program, #vars").prop("disabled", true).val('');
|
||||
$("#rule").prop("placeholder", '窗口的进程名,支持正则,如explorer.exe');
|
||||
$(".robot").show();
|
||||
$('.customscript').hide();
|
||||
window.editor.setOption("mode", 'javascript');
|
||||
break;
|
||||
case 'window':
|
||||
$("#ruleWord").html("进 程");
|
||||
$(".var.regex").hide()
|
||||
@ -400,13 +395,12 @@ $("#options").on('click', '.editBtn', function () {
|
||||
var code = $(this).attr('code');
|
||||
var data = utools.db.get("customFts").data[code];
|
||||
showCustomize();
|
||||
var robotjs = data.robotjs;
|
||||
var cmds = data.features.cmds[0]
|
||||
if (cmds.type == 'regex') {
|
||||
$('#type').val('regex')
|
||||
$('#rule').val(cmds.match);
|
||||
} else if (cmds.type == 'window') {
|
||||
robotjs ? $('#type').val('robotjs') : $('#type').val('window')
|
||||
$('#type').val('window');
|
||||
$('#rule').val(cmds.match.app);
|
||||
} else {
|
||||
$('#type').val('key')
|
||||
@ -423,12 +417,13 @@ $("#options").on('click', '.editBtn', function () {
|
||||
$('#customarg').show().val(data.customOptions.argv);
|
||||
$('#customext').show().val(data.customOptions.ext);
|
||||
}
|
||||
mode == 'applescript' && (mode = 'shell');
|
||||
mode == 'cmd' && (mode = 'powershell');
|
||||
window.editor.setOption("mode", mode);
|
||||
// mode == 'applescript' && (mode = 'shell');
|
||||
// mode == 'cmd' && (mode = 'powershell');
|
||||
// window.editor.setOption("mode", mode);
|
||||
window.editor.setValue(data.cmd);
|
||||
resetVars();
|
||||
typeCheck();
|
||||
programCheck();
|
||||
outputCheck();
|
||||
})
|
||||
|
||||
@ -468,7 +463,7 @@ $("#options").on('click', '#addKey', function () {
|
||||
$("#options").on('click', '#addDelay', function () {
|
||||
var t = $('#keydelay').val();
|
||||
if (/\d+/.test(t)) {
|
||||
window.editor.replaceSelection(`utools.robot.setKeyboardDelay(${t});\n`)
|
||||
window.editor.replaceSelection(`await sleep(${t});\n`)
|
||||
} else {
|
||||
Swal.fire({
|
||||
icon: 'warning',
|
||||
@ -477,6 +472,49 @@ $("#options").on('click', '#addDelay', function () {
|
||||
}
|
||||
})
|
||||
|
||||
// 添加动作
|
||||
$("#options").on('click', '#addAction', async function () {
|
||||
var a = $('#action').val();
|
||||
var text;
|
||||
switch (a) {
|
||||
case 'open':
|
||||
text = '要打开的文件';
|
||||
break;
|
||||
case 'visit':
|
||||
text = '要访问的网址';
|
||||
break;
|
||||
case 'locate':
|
||||
text = '要定位的文件'
|
||||
break;
|
||||
case 'system':
|
||||
text = '要执行的命令'
|
||||
break;
|
||||
case 'message':
|
||||
text = '要发送的消息'
|
||||
break;
|
||||
case 'copyTo':
|
||||
text = '要写入的内容'
|
||||
break;
|
||||
case 'alert':
|
||||
text = '要弹窗的消息'
|
||||
break;
|
||||
default:
|
||||
Swal.fire({
|
||||
icon: 'warning',
|
||||
text: '未选中任何动作',
|
||||
})
|
||||
return;
|
||||
}
|
||||
const { value: content } = await Swal.fire({
|
||||
title: text,
|
||||
input: 'text',
|
||||
showCancelButton: true,
|
||||
})
|
||||
if (content) {
|
||||
window.editor.replaceSelection(`${a}("${content.replace(/\\/g, '\\\\')}");\n`)
|
||||
}
|
||||
})
|
||||
|
||||
// 导出
|
||||
$("#options").on('click', '.exportBtn', function () {
|
||||
var code = $(this).attr('code'),
|
||||
@ -553,18 +591,11 @@ $("#options").on('click', '.saveBtn', function () {
|
||||
title: '啊嘞?!',
|
||||
text: '显示文本或html输出时无法使用{{SelectText}}!',
|
||||
})
|
||||
} else if (type == 'robotjs' && /\{\{.*?\}\}/.test(cmd)) {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: '啊嘞?!',
|
||||
text: '模拟按键模式无法使用特殊变量!',
|
||||
})
|
||||
}else {
|
||||
} else {
|
||||
var program = $('#program').val(),
|
||||
desc = $('#desc').val(),
|
||||
iconame = $("#iconame").val(),
|
||||
iconpath = $("#icon").attr('src'),
|
||||
robotjs = false,
|
||||
icon,
|
||||
base64ico,
|
||||
hasSubInput;
|
||||
@ -589,7 +620,7 @@ $("#options").on('click', '.saveBtn', function () {
|
||||
"minNum": 1
|
||||
}];
|
||||
}
|
||||
if (type == 'window' || type == 'robotjs') {
|
||||
if (type == 'window') {
|
||||
cmds = [{
|
||||
"label": desc,
|
||||
"type": "window",
|
||||
@ -605,11 +636,11 @@ $("#options").on('click', '.saveBtn', function () {
|
||||
hasSubInput = false;
|
||||
}
|
||||
$("#customize").animate({ top: '100%' });
|
||||
if (type == "robotjs") {
|
||||
program = "";
|
||||
output = "";
|
||||
robotjs = true;
|
||||
}
|
||||
// if (type == "robotjs") {
|
||||
// program = "";
|
||||
// output = "";
|
||||
// robotjs = true;
|
||||
// }
|
||||
// 添加特性
|
||||
pushData = {
|
||||
features: {
|
||||
@ -621,8 +652,8 @@ $("#options").on('click', '.saveBtn', function () {
|
||||
program: program,
|
||||
cmd: cmd,
|
||||
output: output,
|
||||
hasSubInput: hasSubInput,
|
||||
robotjs: robotjs
|
||||
hasSubInput: hasSubInput
|
||||
// robotjs: robotjs
|
||||
}
|
||||
if (program == 'custom') {
|
||||
pushData.customOptions = {
|
||||
@ -631,6 +662,9 @@ $("#options").on('click', '.saveBtn', function () {
|
||||
"ext": $('#customext').val()
|
||||
}
|
||||
}
|
||||
if (program == 'simulation') {
|
||||
$('#output').val('');
|
||||
}
|
||||
putCustomFts(code, pushData);
|
||||
showOptions();
|
||||
$(`#${code}`).click();
|
||||
@ -646,18 +680,32 @@ hasCustomIcon = () => {
|
||||
return /data:image\/png;base64,/.test(src) || iconame
|
||||
}
|
||||
|
||||
programCheck = () => {
|
||||
let mode = $('#program').val();
|
||||
if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`);
|
||||
switch (mode) {
|
||||
case 'custom':
|
||||
$('.customscript').show();
|
||||
break;
|
||||
case 'simulation':
|
||||
$('.varoutput').hide();
|
||||
$('.simulation').show();
|
||||
mode = 'javascript';
|
||||
break;
|
||||
default:
|
||||
$('.customscript').hide();
|
||||
$('.simulation').hide();
|
||||
$('.varoutput').show();
|
||||
break;
|
||||
}
|
||||
if('applescript') mode = 'shell';
|
||||
if('cmd') mode = 'powershell';
|
||||
window.editor.setOption("mode", mode);
|
||||
}
|
||||
|
||||
// 语言选项改变时
|
||||
$("#options").on('change', '#program', function () {
|
||||
let mode = $(this).val();
|
||||
if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`);
|
||||
if (mode == 'custom') {
|
||||
$('.customscript').show();
|
||||
} else {
|
||||
$('.customscript').hide();
|
||||
}
|
||||
mode == 'applescript' && (mode = 'shell');
|
||||
mode == 'cmd' && (mode = 'powershell');
|
||||
window.editor.setOption("mode", mode);
|
||||
programCheck()
|
||||
})
|
||||
|
||||
// 变量选项改变时
|
||||
@ -666,6 +714,10 @@ $("#options").on('change', '#vars', function () {
|
||||
window.editor.replaceSelection($("#vars").val());
|
||||
})
|
||||
|
||||
$("#options").on('change', '#action', function () {
|
||||
$("#action").css({ 'color': 'black' })
|
||||
})
|
||||
|
||||
// 输出选项改变时
|
||||
$("#options").on('change', '#output', function () {
|
||||
resetVars();
|
||||
|
Loading…
x
Reference in New Issue
Block a user