diff --git a/src/assets/options.js b/src/assets/options.js index a24e14e..8f4fdb5 100644 --- a/src/assets/options.js +++ b/src/assets/options.js @@ -1,1124 +1,1133 @@ -let getDB = id => { - var db = utools.db.get(id), - dbData = db ? db.data : {}; - return dbData; -} - -let putDB = (code, pushData, id) => { - var db = utools.db.get(id); - if (db) { - var rev = db._rev - var data = db.data - data[code] = pushData; - utools.db.put({ _id: id, data: data, _rev: rev }); - } else { - var data = {}; - data[code] = pushData; - utools.db.put({ _id: id, data: data }); +!function () { + getDB = id => { + var db = utools.db.get(id), + dbData = db ? db.data : {}; + return dbData; } -} - -// 导入 -let importCommand = file => { - var options = file ? { type: 'file', argvs: file } : { type: 'dialog', argvs: { filters: [{ name: 'json', extensions: ['json'] }] } } - options.readfile = true - var fileinfo = getFileInfo(options) - if (!fileinfo) return - try { - var pushData = JSON.parse(fileinfo.data); - } catch (error) { - Swal.fire({ icon: 'error', title: '啊嘞?!', text: '格式错误!', }) - return - } - // 单个命令导入 - if (typeof(pushData.features) == 'object') { - var code = pushData.features.code; - putDB(code, pushData, 'customFts'); - showOptions(); - // 多个命令导入 - } else { - if (typeof(Object.values(pushData)[0].features) == 'object') { - for (var code of Object.keys(pushData)) { - putDB(code, pushData[code], 'customFts'); - } - showOptions(); + + putDB = (code, pushData, id) => { + var db = utools.db.get(id); + if (db) { + var rev = db._rev + var data = db.data + data[code] = pushData; + utools.db.put({ _id: id, data: data, _rev: rev }); } else { + var data = {}; + data[code] = pushData; + utools.db.put({ _id: id, data: data }); + } + } + + // 导入 + let importCommand = file => { + var options = file ? { type: 'file', argvs: file } : { type: 'dialog', argvs: { filters: [{ name: 'json', extensions: ['json'] }] } } + options.readfile = true + var fileinfo = getFileInfo(options) + if (!fileinfo) return + try { + var pushData = JSON.parse(fileinfo.data); + } catch (error) { Swal.fire({ icon: 'error', title: '啊嘞?!', text: '格式错误!', }) + return } - } -} - -let exportAll = () => { - json = utools.db.get('customFts').data, - options = { - title: '选择保存位置', - defaultPath: 'quickCommand', - filters: [ - { name: 'json', extensions: ['json'] }, - ] - }; - window.saveFile(options, JSON.stringify(json)); -} - - -let clearAll = () => { - Swal.fire({ - text: '将会清空所有命令,请确认!', - icon: 'warning', - showCancelButton: true, - confirmButtonColor: '#3085d6', - cancelButtonColor: '#d33', - confirmButtonText: '确定!', - cancelButtonText: '手抖...', - }).then((result) => { - if (result.value) { - utools.db.remove('customFts'); - clearAllFeatures(); + // 单个命令导入 + if (typeof(pushData.features) == 'object') { + var code = pushData.features.code; + putDB(code, pushData, 'customFts'); showOptions(); - } - }) -} - -let programs = { - shell: { - bin: 'bash', - argv: '', - ext: 'sh', - color: '#89e051' - }, - applescript: { - bin: 'osascript', - argv: '', - ext: 'scpt', - color: '#101F1F' - }, - cmd: { - bin: '', - argv: '', - ext: 'bat', - codec: 'gbk', - color: '#C1F12E' - }, - powershell: { - bin: 'powershell', - argv: '-NoProfile -File', - ext: 'ps1', - codec: utools.isWindows() ? 'gbk' : '', - color: '#012456' - }, - python: { - bin: 'python', - argv: '-u', - ext: 'py', - codec: utools.isWindows() ? 'gbk' : '', - color: '#3572A5' - }, - javascript: { - bin: 'node', - argv: '', - ext: 'js', - color: '#f1e05a' - }, - ruby: { - bin: 'ruby', - argv: '', - ext: 'rb', - color: '#701516' - }, - php: { - bin: 'php', - argv: '', - ext: 'php', - color: '#4F5D95' - }, - c: { - bin: 'gcc', - argv: '-o', - ext: 'c', - codec: utools.isWindows() ? 'gbk' : '', - color: '#555555' - }, - csharp: { - bin: 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe', - argv: '/Nologo', - ext: 'cs', - codec: 'gbk', - color: '#178600' - }, - lua: { - bin: 'lua', - argv: '', - ext: 'lua', - color: '#000080' - }, - perl: { - bin: 'perl', - argv: '', - ext: 'pl', - color: '#0298c3' - }, - custom: { - bin: '', - argv: '', - ext: '', - codec: '', - color: '#438eff' + // 多个命令导入 + } else { + if (typeof(Object.values(pushData)[0].features) == 'object') { + for (var code of Object.keys(pushData)) { + putDB(code, pushData[code], 'customFts'); + } + showOptions(); + } else { + Swal.fire({ icon: 'error', title: '啊嘞?!', text: '格式错误!', }) + } + } } -} - -let showOptions = (tag = "默认") => { - $("#options").empty().fadeIn(); - var currentFts = utools.getFeatures(), - customFts = getDB('customFts'); - var allTags = ["默认"] - var featureList = ` -
|
+ ${features.explain}
+
+
+ ●
+ ${fts.program} | ${platformIcons.join('')}
+
+ |
+ ${cmds} | ++ + | +
+ | `
+ })
+ featureList += `
+ 模 式 + + 关键字
+说 明
+
+
+ 环 境 + + 标 签 + + +
++ 变 量 + + 输 出 + +
++ 脚 本 + + + + + + + + + ﹢动作 + ﹢按键 + ?帮助 + 格式化 + +
+ +
+
+
+
+ ${(readonly && !fofoCommon.isDev()) ? '' : ''}
+
+
+
- 模 式 -
-说 明
-
-
- 环 境 + + 加引号 + ` + Swal.fire({ + title: "预设动作", + onBeforeOpen: () => { + $('#actionType').change(function () { + $('#actionArgs').attr('placeholder', $(this).find(`[value=${$(this).val().replace('.', '\\.')}]`).attr('args')) + }) + }, + html: html, + showCancelButton: true, + preConfirm: () => { + var actionType = $("#actionType").val() + var actionArgs = $("#actionArgs").val() + if ($("#isString").is(':checked')) actionArgs = "String.raw\`" + actionArgs + "\`" + var action = `${actionType}(${actionArgs})` + if (actionType == 'utools.ubrowser.goto') action += `.run()` + window.editor.replaceSelection(`${action}\n`) + } + }) + }) + + // 导出 + $("#options").on('click', '.exportBtn', function () { + var code = $(this).attr('code'), + json = getDB('customFts')[code], + options = { + title: '选择保存位置', + filters: [ + { name: 'json', extensions: ['json'] }, + ] + }; + window.saveFile(options, JSON.stringify(json)); + }) + + // 删除 + $("#options").on('click', '.delBtn', function () { + Swal.fire({ + text: '删除这个快捷命令', + icon: 'warning', + showCancelButton: true, + confirmButtonColor: '#3085d6', + cancelButtonColor: '#d33', + confirmButtonText: '确定!', + cancelButtonText: '手抖...', + }).then((result) => { + if (result.value) { + var code = $(this).attr('code'), + db = utools.db.get("customFts"), + data = db.data; + delete data[code]; + utools.removeFeature(code); + utools.db.put({ _id: "customFts", data: data, _rev: db._rev }); + var currentTag = $('.currentTag').text() + if ($('#featureList tr').length == 2) currentTag = "默认" + showOptions(currentTag); + } + }) + }) + + // 选择图标 + $("#options").on('click', '#icon, #iconame', function () { + var options = { + buttonLabel: '选择', + filters: [{ + name: 'Images', + extensions: ['png'] + }, ] + } + var file = getFileInfo({ type: 'dialog', argvs: options, readfile: false }) + if (file) { + $("#iconame").val(file.name); + $("#icon").attr('src', file.path); + } + }) + + let SaveCurrentCommand = async () => { + if ($('#tags').is(":parent")) { + var type = $('#type').val(), + code = $("#code").val(), + tags = $('#tags').val(), + rule = $('#rule').val(), + cmd = window.editor.getValue(); + if (tags && tags.includes("默认") && !fofoCommon.isDev()) return + if (type != "window" && !rule) return swal.fire(`${$('#ruleWord').text().replace(" ", "")} 不能留空!`) + if (!cmdCheck(type, cmd)) return + if (!code) { + // 生成唯一code + var uid = Number(Math.random().toString().substr(3, 3) + Date.now()).toString(36); + var code = `${type}_${uid}`; + } + var output = $('#output').val(), + scptarg = $('#scptarg').val(), + program = $('#program').val(), + desc = $('#desc').val(), + iconame = $("#iconame").val(), + iconpath = $("#icon").attr('src'), + icon, + base64ico, + hasSubInput; + if (!desc) desc = ' '; + // 选择了图标的情况下 + if (iconame) { + base64ico = window.getBase64Ico(iconpath); + icon = "data:image/png;base64," + base64ico; + // 未自定义使用默认 + } else { + icon = iconpath; + } + if (type == 'key') { + cmds = rule.split(",").map(x => x.trim()) + } else if (type == 'regex') { + if (!/^\/.*?\/[igm]*$/.test(rule)) { + rule = "/" + rule + "/" + } + cmds = [{ + "label": desc, + "type": "regex", + "match": rule, + "minNum": 1 + }]; + } else if (type == 'window') { + var cmdOfWin = { + "label": desc, + "type": "window" + } + if (rule) { + try { + cmdOfWin.match = JSON.parse(rule) + } catch (error) { + cmdOfWin.match = { + "app": rule.split(',') + } + } + } + cmds = [cmdOfWin]; + } else if (type == 'files') { + if (!/^\/.*?\/[igm]*$/.test(rule)) { + rule = "/" + rule + "/" + } + cmds = [{ + "label": desc, + "type": "files", + "match": rule, + "minNum": 1 + }]; + } + // 需要子输入框 + if (cmd.includes('{{subinput}}')) { + hasSubInput = true; + } else { + hasSubInput = false; + } + // platform + var platform = [] + $('.platform').not('.disabled').each(function() { platform.push($(this).attr('id')) }) + // 添加特性 + pushData = { + features: { + "code": code, + "explain": desc, + "cmds": cmds, + "icon": icon, + "platform": platform + }, + program: program, + cmd: cmd, + output: output, + hasSubInput: hasSubInput, + scptarg: scptarg + } + if (tags) pushData.tags = tags + if (program == 'custom') { + pushData.customOptions = { + "bin": $('#custombin').val(), + "argv": $('#customarg').val(), + "ext": $('#customext').val(), + 'codec': $('#customcodec').val() + } + } + putDB(code, pushData, 'customFts'); + $("#customize").animate({ top: '100%' }, () => { + // 保存后标签跳转处理 + var redirectTag, currentTag = $('.currentTag').text() + if (tags.length) { + if (pushData.tags.includes(currentTag)) { + redirectTag = currentTag + } else { + redirectTag = pushData.tags[0] + } + } else { + redirectTag = "未分类" + } + showOptions(redirectTag); + $("#customize").empty() + $(`#${code}`).click(); + if (!$(`#${code}`).is(':checked')) { + $(`#${code}`).click(); + } + }); + } + } + + // 显示运行结果 + let showRunResult = (content, raw, success) => { + var options, position, showClass, hideClass, maxlength = 100000 + if ($("#vars").is(":parent")) { + position = 'top' + showClass = 'fadeInDownWindow' + hideClass = 'fadeOutUpWindow' + } else { + position = 'bottom' + showClass = 'fadeInUpWindow' + hideClass = 'fadeOutDownWindow' + } + var preView = () => { + var result = $('#swal2-content').text() + result = htmlEncode(result, raw) + $(".swal2-content").css("width", "100%") + $('#swal2-content').html(`
${result}`)
+ $('.swal2-popup').addClass('swal2-toast')
+ }
+ var contlength = content.length
+ if (contlength > maxlength) content = content.slice(0, maxlength - 100) + `\n\n...\n${contlength - maxlength - 100} 字省略\n...\n\n` + content.slice(contlength - 100)
+ content += '\n'
+ var outputchannel = $("#swal2-content > pre")
+ if (outputchannel.is(":parent")) {
+ outputchannel.append(htmlEncode(content, raw))
+ } else {
+ options = {
+ onBeforeOpen: preView,
+ icon: success ? "success" : "error",
+ text: content,
+ position: position,
+ width: 800,
+ showConfirmButton: true,
+ showClass: { popup: showClass },
+ hideClass: { popup: hideClass }
+ }
+ success ? swalOneByOne(options) : Swal.fire(options)
+ }
+ }
+
+ let runCurrentCommand = async () => {
+ if ($("#customize").is(":parent")) {
+ var cmd = window.editor.getValue()
+ cmd = special(cmd)
+ var requireInputVal = ['{{input}}', '{{subinput}}', '{{pwd}}', '{{SelectFile}}']
+ .filter(x => cmd.includes(x));
+ if (requireInputVal.length) {
+ var html = requireInputVal
+ .map(r => ``)
+ .join("")
+ await Swal.fire({
+ title: "需要临时为以下变量赋值",
+ html: html,
+ focusConfirm: false,
+ preConfirm: () => {
+ requireInputVal.forEach(r => {
+ cmd = cmd.replace(new RegExp(r, 'g'), document.getElementById(r).value)
+ })
+ }
+ })
+ }
+ var program = $("#program").val()
+ var output = $("#output").val()
+ var terminal = false
+ var raw = true
+ switch (output) {
+ case "html":
+ raw = false
+ break;
+ case "terminal":
+ terminal = true
+ break;
+ case "ignore":
+ utools.hideMainWindow()
+ break;
+ }
+ if (program == "quickcommand") {
+ runCodeInVm(cmd, (stdout, stderr) => {
+ if (stderr) return showRunResult(stderr, raw, false)
+ showRunResult(stdout, raw, true)
+ });
+ } else {
+ var option = programs[program]
+ if (program == "custom") option = {
+ "bin": $('#custombin').val(),
+ "argv": $('#customarg').val(),
+ "ext": $('#customext').val(),
+ 'codec': $('#customcodec').val()
+ }
+ option.scptarg = $('#scptarg').val()
+ runCodeFile(cmd, option, terminal, (stdout, stderr) => {
+ if (terminal) return
+ if (stderr) return showRunResult(stderr, raw, false)
+ showRunResult(stdout, raw, true)
+ })
+ }
+ }
+ }
+
+ let killCurrentCommand = () => {
+ }
+
+ let quitCurrentCommand = () => {
+ if ($("#customize").is(":parent") && $("#featureList").is(":parent")) {
+ $("#customize").animate({ top: '100%' });
+ $("#customize").empty()
+ }
+ }
+
+ let highlightIfKnown = ext => {
+ var lang = Object.keys(programs).filter(p => programs[p].ext == ext)
+ if (lang.length) {
+ if (lang[0] == 'python') getPythonMods()
+ window.editor.setOption("mode", lang[0])
+ return lang[0]
+ }
+ }
+
+ showCodeEditor = file => {
+ let options = ``
+ var customWindow = `
+
+
- 变 量
-
- 脚 本 - + - + 运 行 + + + 格式化 ﹢动作 ﹢按键 ?帮助 - 格式化 -
- -
-
-
-
- ${readonly ? '' : ''}
-
-
-
${result}`)
- $('.swal2-popup').addClass('swal2-toast')
- }
- var contlength = content.length
- if (contlength > maxlength) content = content.slice(0, maxlength - 100) + `\n\n...\n${contlength - maxlength - 100} 字省略\n...\n\n` + content.slice(contlength - 100)
- content += '\n'
- var outputchannel = $("#swal2-content > pre")
- if (outputchannel.is(":parent")) {
- outputchannel.append(htmlEncode(content, raw))
- } else {
- options = {
- onBeforeOpen: preView,
- icon: success ? "success" : "error",
- text: content,
- position: position,
- width: 800,
- showConfirmButton: true,
- showClass: { popup: showClass },
- hideClass: { popup: hideClass }
- }
- success ? swalOneByOne(options) : Swal.fire(options)
- }
-}
-
-let runCurrentCommand = async () => {
- if ($("#customize").is(":parent")) {
- var cmd = window.editor.getValue()
- cmd = special(cmd)
- var requireInputVal = ['{{input}}', '{{subinput}}', '{{pwd}}', '{{SelectFile}}']
- .filter(x => cmd.includes(x));
- if (requireInputVal.length) {
- var html = requireInputVal
- .map(r => ``)
- .join("")
- await Swal.fire({
- title: "需要临时为以下变量赋值",
- html: html,
- focusConfirm: false,
- preConfirm: () => {
- requireInputVal.forEach(r => {
- cmd = cmd.replace(new RegExp(r, 'g'), document.getElementById(r).value)
- })
- }
- })
- }
- var program = $("#program").val()
- var output = $("#output").val()
- var terminal = false
- var raw = true
- switch (output) {
- case "html":
- raw = false
- break;
- case "terminal":
- terminal = true
- break;
- case "ignore":
- utools.hideMainWindow()
- break;
- }
- if (program == "quickcommand") {
- runCodeInVm(cmd, (stdout, stderr) => {
- if (stderr) return showRunResult(stderr, raw, false)
- showRunResult(stdout, raw, true)
- });
- } else {
- var option = programs[program]
- if (program == "custom") option = {
- "bin": $('#custombin').val(),
- "argv": $('#customarg').val(),
- "ext": $('#customext').val(),
- 'codec': $('#customcodec').val()
- }
- option.scptarg = $('#scptarg').val()
- runCodeFile(cmd, option, terminal, (stdout, stderr) => {
- if (terminal) return
- if (stderr) return showRunResult(stderr, raw, false)
- showRunResult(stdout, raw, true)
- })
- }
- }
-}
-
-let killCurrentCommand = () => {
-}
-
-let quitCurrentCommand = () => {
- if ($("#customize").is(":parent") && $("#featureList").is(":parent")) {
- $("#customize").animate({ top: '100%' });
- $("#customize").empty()
- }
-}
-
-let highlightIfKnown = ext => {
- var lang = Object.keys(programs).filter(p => programs[p].ext == ext)
- if (lang.length) {
- if (lang[0] == 'python') getPythonMods()
- window.editor.setOption("mode", lang[0])
- return lang[0]
- }
-}
-
-showCodeEditor = file => {
- let options = ``
- var customWindow = `
-