diff --git a/src/assets/index.js b/src/assets/index.js index 29c7ba9..a6164ec 100644 --- a/src/assets/index.js +++ b/src/assets/index.js @@ -7,10 +7,34 @@ } else { $('#darkmode').length && $('#darkmode, #darkswal').remove() } + // 禁用危险函数 + let utoolsFull = utools + if (!isDev()) utools = getuToolsLite() + // 数据库函数封装 + let getDB = id => { + var db = utoolsFull.db.get(id), + dbData = db ? db.data : {}; + return dbData; + } + + let putDB = (key, value, id) => { + var db = utoolsFull.db.get(id); + if (db) { + var rev = db._rev + var data = db.data + data[key] = value; + utoolsFull.db.put({ _id: id, data: data, _rev: rev }); + } else { + var data = {}; + data[key] = value; + utoolsFull.db.put({ _id: id, data: data }); + } + } + // 进入插件 utools.onPluginEnter(async ({ code, type, payload }) => { - if (fofoCommon.isRunningAtFirstTime()) { - fofoCommon.showChangeLog() + if (isRunningAtFirstTime()) { + showChangeLog() importDefaultCommands() oldVersionFix() } @@ -28,7 +52,7 @@ customext: $('#customext').val(), customcodec: $('#customcodec').val() } - fofoCommon.putDB('history', { cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, 'codeHistory') + putDB('history', { cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, 'codeHistory') } // 初始化 $("#options, #out").empty() @@ -51,7 +75,7 @@ $('body').css({overflow: 'auto'}) utools.setExpendHeight(0); $("#options").hide(); - var db = utools.db.get('customFts').data[code], + var db = getDB('customFts')[code], cmd = db.cmd; if (db.program == "custom") { option = db.customOptions; @@ -65,9 +89,9 @@ // 正则 if (type == 'regex') cmd = cmd.replace(/\{\{input\}\}/mg, payload); // 文件 - if (type == 'files') { + if (type == 'files' && cmd.includes('{{MatchedFiles')) { let MatchedFiles = payload - let Matched = cmd.match(/\{\{MatchedFiles.*?\}\}/g) + let Matched = cmd.match(/\{\{MatchedFiles(\[\d+\]){0,1}(\.\w{1,11}){0,1}\}\}/g) Matched && Matched.forEach(m => { repl = eval(m.slice(2, -2)) typeof repl == 'object' && (repl = JSON.stringify(repl)) @@ -90,10 +114,10 @@ // 获取窗口信息 if (cmd.includes('{{WindowInfo')) { let WindowInfo = payload - let Matched = cmd.match(/\{\{WindowInfo.*?\}\}/g) + let Matched = cmd.match(/\{\{WindowInfo(\.\w{1,7}){0,1}\}\}/g) Matched && Matched.forEach(m => { repl = eval(m.slice(2, -2)) - typeof repl == 'Object' && (repl = JSON.stringify(repl)) + typeof repl == 'object' && (repl = JSON.stringify(repl)) cmd = cmd.replace(m, repl) }) } @@ -213,7 +237,7 @@ // 替换上个版本弃用的功能 let oldVersionFix = () => { utools.showNotification('第一次更新会对老版本命令做兼容处理,如插件显示空白请稍候', 'warning') - var customFts = fofoCommon.getDB('customFts'); + var customFts = getDB('customFts'); Object.keys(customFts).forEach((x, i) => { // 旧版的 program if (customFts[x].program == 'simulation') customFts[x].program = 'quickcommand'; @@ -223,7 +247,7 @@ let code = customFts[x].features.code if (!/^(window|key|regex|files|default)_/.test(code)) { console.log(code); - utools.removeFeature(code) + utoolsFull.removeFeature(code) let uid = Number(Math.random().toString().substr(3, 3) + (Date.now() + i * 10000)).toString(36) let type = customFts[x].features.cmds[0].type type || (type = 'key') @@ -233,13 +257,25 @@ delete customFts[x] customFts[newCode] = newFts } - fofoCommon.putDB(x, customFts[x], 'customFts'); + putDB(x, customFts[x], 'customFts'); }) } + let showChangeLog = () => { + putDB('version', pluginInfo().version, 'plugin') + utools.createBrowserWindow('./helps/CHANGELOG.html', {width: 1280, height: 920}) + } + + let isRunningAtFirstTime = () => { + var historyVersion = getDB('plugin').version + if (!historyVersion) return 'init' + if (pluginInfo().version > historyVersion) return 'update' + return false + } + // 导入默认命令 let importDefaultCommands = () => { - let customFts = fofoCommon.getDB('customFts') + let customFts = getDB('customFts') let qc = Object.keys(customFts) let defaultCommands = getDefaultCommands() Object.keys(defaultCommands).forEach(d => { @@ -281,11 +317,11 @@ // 单个命令导入 if (pushData.single) { var code = pushData.qc.features.code; - fofoCommon.putDB(code, pushData.qc, 'customFts'); + putDB(code, pushData.qc, 'customFts'); // 多个命令导入 } else { for (var code of Object.keys(pushData.qc)) { - fofoCommon.putDB(code, pushData.qc[code], 'customFts'); + putDB(code, pushData.qc[code], 'customFts'); } } return true @@ -293,7 +329,7 @@ // 全部导出 let exportAll = () => { - let jsonQc = utools.db.get('customFts').data, + let jsonQc = getDB('customFts'), options = { title: '选择保存位置', defaultPath: 'quickCommand', @@ -301,7 +337,7 @@ { name: 'json', extensions: ['json'] }, ] }; - if (!fofoCommon.isDev()) Object.keys(jsonQc).filter(k => jsonQc[k].tags && jsonQc[k].tags.includes('默认')).map(k => delete jsonQc[k]) + if (!isDev()) Object.keys(jsonQc).filter(k => jsonQc[k].tags && jsonQc[k].tags.includes('默认')).map(k => delete jsonQc[k]) window.saveFile(options, JSON.stringify(jsonQc)); } @@ -309,7 +345,7 @@ let clearAll = () => { quickcommand.showConfirmBox('将会清空所有自定义命令,请确认!').then(x => { if (!x) return - utools.db.remove('customFts'); + utoolsFull.db.remove('customFts'); importDefaultCommands(); clearAllFeatures(); showOptions(); @@ -408,7 +444,7 @@ let showOptions = (tag = "默认") => { $("#options").empty().fadeIn(); var currentFts = utools.getFeatures(), - customFts = fofoCommon.getDB('customFts'); + customFts = getDB('customFts'); var allTags = ["默认"] var featureList = `