From 34caef0f72195ffe51fd551f0e1349b80c97be4c Mon Sep 17 00:00:00 2001 From: xiyuesaves Date: Fri, 20 Oct 2023 20:18:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8A=A0=E8=BD=BD=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 2 +- src/main.js | 74 +++------------------------- src/main_modules/defaultOptions.json | 46 +++++++++++++++++ src/main_modules/loadOptions.js | 44 +++++++++++++++++ 4 files changed, 98 insertions(+), 68 deletions(-) create mode 100644 src/main_modules/defaultOptions.json create mode 100644 src/main_modules/loadOptions.js diff --git a/manifest.json b/manifest.json index ad99561d..dc3bcadf 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "name": "轻量工具箱", "slug": "lite_tools", "description": "优化QQNT使用体验。功能列表:消息显示发送时间,自定义消息后缀,划词搜索,图片搜索,防撤回,自定义背景图片,小程序分享转链接,自定义大部分界面功能,模拟tg消息样式", - "version": "1.1.34", + "version": "1.1.35", "thumbnail": "./icon.png", "author": { "name": "曦月", diff --git a/src/main.js b/src/main.js index 5a5322b3..8fd5b6ae 100644 --- a/src/main.js +++ b/src/main.js @@ -4,64 +4,15 @@ const http = require("http"); const https = require("https"); const path = require("path"); const fs = require("fs"); -let mainMessage, options, recordMessageRecallIdList, messageRecallPath, messageRecallJson; + +// 本地模块 +let loadOptions = require("./main_modules/loadOptions"); + +let mainMessage, recordMessageRecallIdList, messageRecallPath, messageRecallJson; let log = function (...args) { console.log(...args); }; -// 默认配置文件 -const defaultOptions = { - spareInitialization: true, // 默认使用setTimeout循环初始化,observer经测试有很大概率无法正常工作 - debug: false, // debug开关 - // 划词搜索 - wordSearch: { - enabled: false, - searchUrl: "https://www.bing.com/search?q=%search%", - }, - // 以图搜图 - imageSearch: { - enabled: false, - searchUrl: "https://saucenao.com/search.php?url=%search%", - }, - // 侧边栏功能开关 - sidebar: { - top: [], - bottom: [], - }, - // 媒体预览分类 - imageViewer: { - quickClose: false, // 快速关闭图片预览 - }, - // 聊天界面分类 - message: { - disabledSticker: false, // 禁用弹出贴纸 - disabledHotGIF: false, // 禁用GIF热图 - disabledBadge: false, // 禁用小红点 - disabledSlideMultipleSelection: false, // 禁用滑动多选消息 - convertMiniPrgmArk: false, // 小程序分享转url卡片 - showMsgTime: false, // 显示消息发送时间 - autoOpenURL: false, // 自动打开来自手机的链接 - switchReplace: false, // 复读按钮 - preventMessageRecall: false, // 防撤回 - removeReplyAt: false, // 移除回复时的@标记 - mergeMessage: false, // 合并消息 - avatarSticky: { - enabled: false, - toBottom: false, - }, - }, - tail: { - enabled: false, // 消息后缀 - newLine: false, // 换行显示 - content: "", // 消息后缀内容 - }, - textAreaFuncList: [], // 输入框上方功能 - chatAreaFuncList: [], // 消息框上方功能 - background: { - enabled: false, // 背景图 - url: "", // 背景图地址 - }, -}; // 自定义limitMap,在达到指定数量后清空最后一条记录 class LimitedMap { @@ -241,11 +192,6 @@ function onLoad(plugin) { fs.mkdirSync(pluginDataPath, { recursive: true }); } - // 初始化配置文件 - if (!fs.existsSync(settingsPath)) { - fs.writeFileSync(settingsPath, JSON.stringify(defaultOptions, null, 4)); - } - // 初始化撤回消息列表文件路径 if (!fs.existsSync(messageRecallPath)) { fs.mkdirSync(messageRecallPath, { recursive: true }); @@ -270,14 +216,8 @@ function onLoad(plugin) { } }); - // 获取本地配置文件 - fileOptions = JSON.parse(fs.readFileSync(settingsPath, "utf-8")); - // 兼容性调整 - if (typeof fileOptions.message.avatarSticky === "boolean") { - fileOptions.message.avatarSticky = defaultOptions.message.avatarSticky; - } - // 保存配置和默认配置执行一次合并,以适配新增功能 - options = Object.assign(defaultOptions, fileOptions); + // 使用配置加载模块解决插件不同版本配置文件差异 + options = loadOptions(settingsPath); if (options.debug) { try { diff --git a/src/main_modules/defaultOptions.json b/src/main_modules/defaultOptions.json new file mode 100644 index 00000000..a0ea084f --- /dev/null +++ b/src/main_modules/defaultOptions.json @@ -0,0 +1,46 @@ +{ + "debug": false, + "wordSearch": { + "enabled": false, + "searchUrl": "https://www.bing.com/search?q=%search%" + }, + "imageSearch": { + "enabled": false, + "searchUrl": "https://saucenao.com/search.php?url=%search%" + }, + "sidebar": { + "top": [], + "bottom": [] + }, + "imageViewer": { + "quickClose": false + }, + "message": { + "disabledSticker": false, + "disabledHotGIF": false, + "disabledBadge": false, + "disabledSlideMultipleSelection": false, + "convertMiniPrgmArk": false, + "showMsgTime": false, + "autoOpenURL": false, + "switchReplace": false, + "preventMessageRecall": false, + "removeReplyAt": false, + "mergeMessage": false, + "avatarSticky": { + "enabled": false, + "toBottom": false + } + }, + "tail": { + "enabled": false, + "newLine": false, + "content": "" + }, + "textAreaFuncList": [], + "chatAreaFuncList": [], + "background": { + "enabled": false, + "url": "" + } +} diff --git a/src/main_modules/loadOptions.js b/src/main_modules/loadOptions.js new file mode 100644 index 00000000..39a8227d --- /dev/null +++ b/src/main_modules/loadOptions.js @@ -0,0 +1,44 @@ +const fs = require("fs"); +const defaultOptions = require("./defaultOptions.json"); +function loadOptions(optionsPath) { + try { + // 判断是否存在配置文件 + if (!fs.existsSync(optionsPath)) { + // 没有传入配置文件地址则直接返回默认配置文件并初始化 + fs.writeFileSync(optionsPath, JSON.stringify(defaultOptions, null, 4)); + return defaultOptions; + } else { + const fileOptions = JSON.parse(fs.readFileSync(optionsPath, "utf-8")); + const options = recursiveAssignment(fileOptions, defaultOptions); + fs.writeFileSync(optionsPath, JSON.stringify(options, null, 4)); + return options; + } + } catch (err) { + console.error("读取配置文件出错", err); + return defaultOptions; + } +} + +function recursiveAssignment(fileOptions, defaultOptions) { + if (!fileOptions) { + return defaultOptions; + } + let obj = {}; + for (const key in defaultOptions) { + if (Object.hasOwnProperty.call(defaultOptions, key)) { + // 如果键值是对象则递归处理 + if (Object.prototype.toString.call(defaultOptions[key]) === "[object Object]") { + obj[key] = recursiveAssignment(fileOptions[key], defaultOptions[key]); + } + // 判断两者同一个键的数据类型是否一致 + else if (Object.prototype.toString.call(fileOptions[key]) === Object.prototype.toString.call(defaultOptions[key])) { + obj[key] = fileOptions[key]; + } else { + obj[key] = defaultOptions[key]; + } + } + } + return obj; +} + +module.exports = loadOptions;