quickcommandshowButtonBox(buttons)showInputBox(placeHolders)showSelectList(selects, options)updateSelectList(opt, id)showTextAera(placeholder)showMessageBox(message, icon, time)sleep(ms)setTimeout(callback, ms)htmlParse(html)downloadFile(url, defaultPath, showDialog)payloadkill(pid, signal)simulateCopy()simulatePaste()上下文一览
showButtonBox(buttons)callback: Function 回调函数
buttons: Array 每一个元素对应一个按钮
返回: Promise
显示一个按钮对话框,用来接收用户的输入
示例
x// then 写法quickcommand.showButtonBox(["按钮1", "按钮2", "按钮3"]).then(({ id, text }) => { console.log(`选择了第${id+1}个按钮`) console.log(`按钮的文本为${text}`)})// async 写法(async () =>{ let button = await quickcommand.showButtonBox(["按钮1", "按钮2", "按钮3"]) console.log(`选择了第${button.id+1}个按钮`) console.log(`按钮的文本为${button.text}`)})()// 捕获错误quickcommand.showButtonBox().catch(e => { console.log(e)})实例
xxxxxxxxxx// 截取自内置快捷命令: 通过 find 查找文件quickcommand.showButtonBox(['打开文件', '在文件管理器中定位', '复制文件路径']).then(x => { switch (x.id) { case 0: utools.shellOpenItem(file); break; case 1: utools.shellShowItemInFolder(file); break; case 2: utools.copyText(file); break; default: break; }})showInputBox(placeHolders)placeHolders: Array 每一个占位符对应一个输入框
返回: Promise
显示一个输入框界面,用来接用户的输入
示例
xxxxxxxxxxquickcommand.showInputBox(["输入框1", "输入框2", "输入框3"]).then(values => { console.log(`输入的内容分别为${values}`)})实例
xxxxxxxxxx// 截取自内置快捷命令: 文本替换quickcommand.showInputBox(["要替换的内容,两边加 / 使用正则", "替换为的内容"]).then(inputs => { var search = inputs[0] var repl = inputs[1] utools.hideMainWindow() quickcommand.sleep(300) quickcommand.simulateCopy() quickcommand.sleep(100) var source = electron.clipboard.readText() source = source.replace(search, repl) })showSelectList(selects, options)selects: Array 每一个元素对应一个列表选项
options: Array | undefined 列表的选项
plaintext、html、json三种,默认为plaintext返回: Promise
optionType为json时,对应json里的每一个属性 显示一个支持搜索的且可以动态更新的选项列表
当指定optionType为json时,类似于插件开发的列表模式,title、description和icon分别表示标题、描述和图标,其中title为必备属性
示例
xxxxxxxxxx// plaintextvar opt = []for (var i = 0; i < 15; i++) { // 每一个选项为文本格式 opt.push(`选项` + i)}quickcommand.showSelectList(opt).then(choise => { console.log(`选择的选项为${choise.text}`)})// jsonvar opt = []for (var i = 0; i < 15; i++) { // 每一个选项为 json 格式 opt.push({title: `选项${i}`, description: `选项${i}的描述`, abcd: `选项${i}的自定义属性`})}quickcommand.showSelectList(opt, {optionType: 'json'}).then(choise => { console.log(`选择的选项为${choise.title}`)})// htmlvar opt = []for (var i = 0; i < 15; i++) { // 每一个选项为 html 格式 opt.push(`<div style="color: red">选项${i}</div>`)}quickcommand.showSelectList(opt, {optionType: 'html'}).then(choise => { console.log(`选择的选项为${quickcommand.htmlParse(choise.text).body.innerText}`)})实例
xxxxxxxxxx// 截取自内置快捷命令: 文本处理let textManipulation = [ ]let text = quickcommand.payloadlet options = textManipulation.map(t => { return { title: t.name, description: t.func(text) }})quickcommand.showSelectList(options, { optionType: 'json' }) .then(choise => { console.log(choise.description) utools.copyText(choise.description) })axios.post('http://fy.iciba.com/ajax.php?a=fy', `f=auto&t=auto&w=${text}`) .then(res => { let content = res.data.content let trans = content.out ? content.out : content.word_mean let opt = textManipulation[0] opt.description = trans quickcommand.updateSelectList(opt, 0) }) updateSelectList(opt, id)动态更新当前的选项列表的选项。
示例
xxxxxxxxxx// 初始状态只有 1、2、3 三个选项quickcommand.showSelectList(['1','2','3']).then(x=>{ console.log(x)})// 1s 后追加一个选项quickcommand.setTimeout(()=>{ quickcommand.updateSelectList('4')}, 1000)// 2s 后更新第二个选项的值quickcommand.setTimeout(()=>{ quickcommand.updateSelectList('updated', 1)}, 2000)showTextAera(placeholder)placeholder: String | undefined 文本框占位符
返回: Promise
显示一个文本框界面,用来接用户的输入
示例
xxxxxxxxxxquickcommand.showTextAera("请输入文本").then(text=>{ console.log(`输入的文本为${text}`)})实例
xxxxxxxxxx// 截取自内置快捷命令: vscode代码片段生成器var snippet = {}quickcommand.showTextAera("请输入代码片段").then(code => { snippet.body = code.split("\n") quickcommand.showInputBox(["代码片段的描述", "触发代码片段的关键词"]) .then(inputs => { snippet.prefix = inputs[1] snippet.description = inputs[0] var result = `"${inputs[0]}": ` + JSON.stringify(snippet, null, '\t') console.log(result) utools.copyText(result) quickcommand.showMessageBox('已复制') })})showMessageBox(message, icon, time)success、error、warning、info、question,默认为success3000显示一个自动消失的提示框
示例
xxxxxxxxxxquickcommand.showMessageBox("这是一段3s后自动消失的成功提示")quickcommand.showMessageBox("这是一段3s后自动消失的失败提示", "error")sleep(ms)由于setTimeout在electron中存在限制,在隐藏到后台时不会被执行,在vm2中也有bug,所以在quickcommand的环境下被禁用了,但对于模拟按键之类的场景,延迟是不可缺少的,所以提供了sleep函数来解决这个问题
示例
xxxxxxxxxxutools.simulateKeyboardTap('d', 'alt')quickcommand.sleep(200)utools.simulateKeyboardTap('c', 'ctrl')setTimeout(callback, ms)用法和setTimeout一样,但实现原理不一样,sleep的异步版本
示例
xxxxxxxxxxquickcommand.setTimeout(()=>{ console.log('2000毫秒后执行')}, 2000)htmlParse(html)html文本DOM对象将给定的html字符串解析为DOM对象,用于快速编写爬虫脚本
示例
xxxxxxxxxxvar html = `<a href="https://u.tools/">uTools</a>`var href = quickcommand.htmlParse(html).querySelector('a').hrefconsole.log(`解析出来的a标签地址为${href}`)downloadFile(url, defaultPath, showDialog)url: String 地址
defaultPath: String | undefined 当showDialog为false时,表示下载文件的绝对路径,当showDialog为true时,表示对话框默认显示的文件名
showDialog: Boolean | undefined 是否弹出对话框 ,默认为false
返回: Promise
Buffer下载文件,也可单纯用于http请求,无论defaultPath是否定义,都将得到响应内容的Buffer,当showDialog为false且定义了defaultPath时,会下载文件为`defaultPath,当showDialog为true时,会弹出保存文件对话框,defaultPath为对话框默认显示的文件名
xxxxxxxxxx// 返回http响应内容quickcommand.downloadFile('https://www.baidu.com').then(r=>{ console.log(r.toString())})// 下载文件到D:/quickcommand.downloadFile('https://res.u-tools.cn/currentversion/uTools-1.1.3.exe', 'D:/')// 下载文件,并弹出对话框询问保存路径quickcommand.downloadFile('https://res.u-tools.cn/currentversion/uTools-1.1.3.exe', 'uTools.exe', true)payloadutools.onPluginEnter的 payload 当匹配模式为关键字时,返回进入插件的关键字;为正则时,返回匹配的文本;为窗口时,返回匹配的窗口信息;为文件时,返回匹配的文件信息
示例
xxxxxxxxxx// 匹配模式为正则/划词时var text = quickcommand.payloadconsole.log(`主输入框匹配的文本为${text}`)kill(pid, signal)'SIGTERM' 将 signal 发送给 pid 标识的进程 , 默认为关闭进程,同process.kill
示例
xxxxxxxxxxquickcommand.kill(16084)simulateCopy()模拟复制操作
simulatePaste()模拟粘贴操作
nodejs 文档
electron 文档
utools 文档
quickcommand