Skip to content

Commit

Permalink
优化配置文件加载方法
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyuesaves committed Oct 20, 2023
1 parent 06da1db commit 34caef0
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 68 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "轻量工具箱",
"slug": "lite_tools",
"description": "优化QQNT使用体验。功能列表:消息显示发送时间,自定义消息后缀,划词搜索,图片搜索,防撤回,自定义背景图片,小程序分享转链接,自定义大部分界面功能,模拟tg消息样式",
"version": "1.1.34",
"version": "1.1.35",
"thumbnail": "./icon.png",
"author": {
"name": "曦月",
Expand Down
74 changes: 7 additions & 67 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 });
Expand All @@ -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 {
Expand Down
46 changes: 46 additions & 0 deletions src/main_modules/defaultOptions.json
Original file line number Diff line number Diff line change
@@ -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": ""
}
}
44 changes: 44 additions & 0 deletions src/main_modules/loadOptions.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 34caef0

Please sign in to comment.