diff --git a/src/assets/index.js b/src/assets/index.js index 3392544..6c412d8 100644 --- a/src/assets/index.js +++ b/src/assets/index.js @@ -10,25 +10,33 @@ // 禁用危险函数 let utoolsFull = utools if (!isDev()) utools = getuToolsLite() + // 数据库前缀 + const QC_PREFIX = 'qc_' + const CFG_PREFIX = 'cfg_' // 数据库函数封装 let getDB = id => { - var db = utoolsFull.db.get(id), - dbData = db ? db.data : {}; - return dbData; + let db = utoolsFull.db.get(id) + return db ? db.data : {} } - 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 }); - } + let putDB = (value, id) => { + let db = utoolsFull.db.get(id); + if (db) utoolsFull.db.put({ _id: id, data: value, _rev: db._rev }) + else utoolsFull.db.put({ _id: id, data: value }); + } + + let delDB = id => { + return utoolsFull.db.remove(id) + } + + let getDocs = key => { + return utoolsFull.db.allDocs(key) + } + // 获取所有 qc,等效于 1.6 版本 getDB('customFts') + window.getAllQuickCommands = () => { + let allQcs = {} + getDocs(QC_PREFIX).forEach(x => allQcs[x.data.features.code] = x.data) + return allQcs } // 进入插件 @@ -52,7 +60,7 @@ customext: $('#customext').val(), customcodec: $('#customcodec').val() } - putDB('history', { cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, 'codeHistory') + putDB({ cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, CFG_PREFIX + 'codeHistory') } // 初始化 $("#options, #out").empty() @@ -75,7 +83,7 @@ $('body').css({overflow: 'auto'}) utools.setExpendHeight(0); $("#options").hide(); - var db = getDB('customFts')[code], + var db = getDB(QC_PREFIX + code), cmd = db.cmd; if (db.program == "custom") { option = db.customOptions; @@ -239,49 +247,49 @@ // 替换上个版本弃用的功能 let oldVersionFix = () => { - utools.showNotification('第一次更新会对老版本命令做兼容处理,如插件显示空白请稍候', 'warning') var customFts = getDB('customFts'); - Object.keys(customFts).forEach((x, i) => { + let ftsKeys = Object.keys(customFts); + if (!ftsKeys.length) return; + utools.showNotification('正在对老版本命令做兼容处理,如插件显示空白请稍候', 'warning') + ftsKeys.forEach((x, i) => { + let fts = customFts[x] // 旧版的 program - if (customFts[x].program == 'simulation') customFts[x].program = 'quickcommand'; + if (fts.program == 'simulation') fts.program = 'quickcommand'; // 旧版的 sleep - if (customFts[x].cmd.includes('await sleep')) customFts[x].cmd = customFts[x].cmd.replace(/await sleep/g, 'quickcommand.sleep') + if (fts.cmd.includes('await sleep')) fts.cmd = fts.cmd.replace(/await sleep/g, 'quickcommand.sleep') // 不规范的 code - let code = customFts[x].features.code + let code = fts.features.code if (!/^(window|key|regex|files|default)_/.test(code)) { console.log(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 || 'key' - let newCode = type + '_' + uid - let newFts = customFts[x] - newFts.features.code = newCode - delete customFts[x] - customFts[newCode] = newFts + let type = fts.features.cmds[0].type || 'key' + code = type + '_' + uid + fts.features.code = code } - putDB(x, customFts[x], 'customFts'); + // 每一个命令一个 id + putDB(fts, QC_PREFIX + code) }) + delDB('customFts') } let showChangeLog = () => { - putDB('version', pluginInfo().version, 'plugin') + putDB(pluginInfo().version, CFG_PREFIX + 'version') utools.createBrowserWindow('./helps/CHANGELOG.html', {width: 1280, height: 920}) } let isRunningAtFirstTime = () => { - var historyVersion = getDB('plugin').version - if (!historyVersion) return 'init' + var historyVersion = getDB(CFG_PREFIX + 'version') + if (historyVersion instanceof Object) return 'init' if (pluginInfo().version > historyVersion) return 'update' return false } // 导入默认命令 let importDefaultCommands = () => { - let customFts = getDB('customFts') - let qc = Object.keys(customFts) let defaultCommands = getDefaultCommands() - Object.keys(defaultCommands).forEach(d => { - if (!qc.includes(d)) importCommand(defaultCommands[d]) + Object.values(defaultCommands).forEach(d => { + importCommand(d) }) } @@ -319,11 +327,11 @@ // 单个命令导入 if (pushData.single) { var code = pushData.qc.features.code; - putDB(code, pushData.qc, 'customFts'); + putDB(pushData.qc, QC_PREFIX + code); // 多个命令导入 } else { for (var code of Object.keys(pushData.qc)) { - putDB(code, pushData.qc[code], 'customFts'); + putDB(pushData.qc[code], QC_PREFIX + code); } } return true @@ -331,23 +339,25 @@ // 全部导出 let exportAll = () => { - let jsonQc = getDB('customFts'), - options = { + let allQcs = getAllQuickCommands() + let options = { title: '选择保存位置', defaultPath: 'quickCommand', filters: [ { name: 'json', extensions: ['json'] }, ] }; - if (!isDev()) Object.keys(jsonQc).filter(k => jsonQc[k].tags && jsonQc[k].tags.includes('默认')).map(k => delete jsonQc[k]) - window.saveFile(JSON.stringify(jsonQc), options); + if (!isDev()) Object.keys(allQcs).forEach(k => { + if (k.includes('default_')) delete allQcs[k] + }) + window.saveFile(JSON.stringify(allQcs), options); } // 清空 let clearAll = () => { quickcommand.showConfirmBox('将会清空所有自定义命令,请确认!').then(x => { if (!x) return - utoolsFull.db.remove('customFts'); + getDocs(QC_PREFIX).map(x => x._id).forEach(y => delDB(y)) importDefaultCommands(); clearAllFeatures(); showOptions(); @@ -446,7 +456,7 @@ let showOptions = (tag = "默认") => { $("#options").empty().fadeIn(); var currentFts = utools.getFeatures(), - customFts = getDB('customFts'); + customFts = getAllQuickCommands(); var allTags = ["默认"] var featureList = `