forked from MUKAPP/LiteLoaderQQNT-MSpring-Theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
215 lines (187 loc) · 7.03 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const fs = require("fs");
const path = require("path");
const { BrowserWindow, ipcMain } = require("electron");
// 防抖函数
function debounce(fn, time) {
let timer = null;
return function (...args) {
timer && clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, time);
}
}
// 辅助函数 - 将十六进制颜色值转换为 RGB 值
function hexToRGB(hex) {
var r = parseInt(hex.slice(1, 3), 16);
var g = parseInt(hex.slice(3, 5), 16);
var b = parseInt(hex.slice(5, 7), 16);
return [r, g, b];
}
// 辅助函数 - 将 RGB 值转换为十六进制颜色值
function RGBToHex(rgb) {
var r = rgb[0].toString(16).padStart(2, '0');
var g = rgb[1].toString(16).padStart(2, '0');
var b = rgb[2].toString(16).padStart(2, '0');
return '#' + r + g + b;
}
// 辅助函数 - 混合两个颜色的 RGB 值
function blendColors(color1, color2, ratio) {
var blendedColor = [];
for (var i = 0; i < 3; i++) {
blendedColor[i] = Math.round(color1[i] * (1 - ratio) + color2[i] * ratio);
}
return blendedColor;
}
// 更新样式
function updateStyle(webContents, settingsPath) {
// 读取settings.json
const data = fs.readFileSync(settingsPath, "utf-8");
const config = JSON.parse(data);
const themeColor = config.themeColor;
// 将themeTagColor设置成具有透明度的themeColor
const themeTagColor = themeColor + "3f";
// 将themeColorDark1设置成themeColor和10%的黑色的混合色
const themeColorDark1 = RGBToHex(blendColors(hexToRGB(themeColor), [0, 0, 0], 0.1));
// 将themeColorDark2设置成themeColor和20%的黑色的混合色
const themeColorDark2 = RGBToHex(blendColors(hexToRGB(themeColor), [0, 0, 0], 0.2));
const backgroundOpacity = config.backgroundOpacity;
// 将backgroundOpacity(是个0-100的整数值)转为两位hex值作为RGBA的透明度(注意不要出现小数)
const backgroundOpacityHex = Math.round(backgroundOpacity * 2.55).toString(16).padStart(2, "0");
const csspath = path.join(__dirname, "style.css");
fs.readFile(csspath, "utf-8", (err, data) => {
if (err) {
return;
}
let preloadString = `:root {
--theme-color: ${themeColor};
--theme-color-dark1: ${themeColorDark1};
--theme-color-dark2: ${themeColorDark2};
--background-color-light: #FFFFFF${backgroundOpacityHex};
--background-color-dark: #171717${backgroundOpacityHex};
--theme-tag-color: ${themeTagColor};
}`
webContents.send(
"LiteLoader.mspring_theme.updateStyle",
// 将主题色插入到style.css中
preloadString + "\n\n" + data
);
});
}
// 监听CSS修改-开发时候用的
function watchCSSChange(webContents, settingsPath) {
const filepath = path.join(__dirname, "style.css");
fs.watch(filepath, "utf-8", debounce(() => {
updateStyle(webContents, settingsPath);
}, 100));
}
// 监听配置文件修改
function watchSettingsChange(webContents, settingsPath) {
fs.watch(settingsPath, "utf-8", debounce(() => {
updateStyle(webContents, settingsPath);
}, 100));
}
// 加载插件时触发
function onLoad(plugin) {
const pluginDataPath = plugin.path.data;
const settingsPath = path.join(pluginDataPath, "settings.json");
// fs判断插件路径是否存在,如果不存在则创建(同时创建父目录(如果不存在的话))
if (!fs.existsSync(pluginDataPath)) {
fs.mkdirSync(pluginDataPath, { recursive: true });
}
// 判断settings.json是否存在,如果不存在则创建
if (!fs.existsSync(settingsPath)) {
fs.writeFileSync(settingsPath, JSON.stringify({
"themeColor": "#cb82be",
"backgroundOpacity": "70",
"heti": false,
"tglike": false,
}));
} else {
// 判断后来加入的backgroundOpacity是否存在,如果不存在则添加
const data = fs.readFileSync(settingsPath, "utf-8");
const config = JSON.parse(data);
if (!config.backgroundOpacity) {
config.backgroundOpacity = "70";
fs.writeFileSync(settingsPath, JSON.stringify(config));
}
// 判断后来加入的heti是否存在,如果不存在则添加
if (!config.heti) {
config.heti = false;
fs.writeFileSync(settingsPath, JSON.stringify(config));
}
// 判断后来加入的tglike是否存在,如果不存在则添加
if (!config.tglike) {
config.tglike = false;
fs.writeFileSync(settingsPath, JSON.stringify(config));
}
}
ipcMain.on(
"LiteLoader.mspring_theme.rendererReady",
(event, message) => {
const window = BrowserWindow.fromWebContents(event.sender);
updateStyle(window.webContents, settingsPath);
}
);
// 监听渲染进程的updateStyle事件
ipcMain.on(
"LiteLoader.mspring_theme.updateStyle",
(event, settingsPath) => {
const window = BrowserWindow.fromWebContents(event.sender);
updateStyle(window.webContents, settingsPath);
});
// 监听渲染进程的watchCSSChange事件
ipcMain.on(
"LiteLoader.mspring_theme.watchCSSChange",
(event, settingsPath) => {
const window = BrowserWindow.fromWebContents(event.sender);
watchCSSChange(window.webContents, settingsPath);
});
// 监听渲染进程的watchSettingsChange事件
ipcMain.on(
"LiteLoader.mspring_theme.watchSettingsChange",
(event, settingsPath) => {
const window = BrowserWindow.fromWebContents(event.sender);
watchSettingsChange(window.webContents, settingsPath);
});
ipcMain.handle(
"LiteLoader.mspring_theme.getSettings",
(event, message) => {
try {
const data = fs.readFileSync(settingsPath, "utf-8");
const config = JSON.parse(data);
return config;
} catch (error) {
console.log(error);
return {};
}
}
);
ipcMain.handle(
"LiteLoader.mspring_theme.setSettings",
(event, content) => {
try {
const new_config = JSON.stringify(content);
fs.writeFileSync(settingsPath, new_config, "utf-8");
} catch (error) {
alert(error);
}
}
);
}
// 创建窗口时触发
function onBrowserWindowCreated(window, plugin) {
const pluginDataPath = plugin.path.data;
const settingsPath = path.join(pluginDataPath, "settings.json");
window.on("ready-to-show", () => {
const url = window.webContents.getURL();
if (url.includes("app://./renderer/index.html")) {
watchCSSChange(window.webContents, settingsPath);
watchSettingsChange(window.webContents, settingsPath);
}
});
}
module.exports = {
onLoad,
onBrowserWindowCreated,
}