Skip to content

Commit f0c08a6

Browse files
committed
新增 [晚上自动切换模式] 功能; 优化 [点击切换模式] 功能(不再刷新)
1 parent fed35f4 commit f0c08a6

1 file changed

Lines changed: 52 additions & 6 deletions

File tree

DarkMode.user.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 护眼模式
3-
// @version 1.2.8
3+
// @version 1.2.9
44
// @author X.I.U
55
// @description 简单有效的全网通用护眼模式(夜间模式、暗黑模式、深色模式)
66
// @match *://*/*
@@ -30,7 +30,8 @@
3030
['menu_autoRecognition', '智能排除自带暗黑模式的网页 (beta)', '智能排除自带暗黑模式的网页 (beta)', true],
3131
['menu_forcedToEnable', '✅ 已强制当前网站启用护眼模式 (👆)', '❌ 未强制当前网站启用护眼模式 (👆)', []],
3232
['menu_darkModeType', '点击切换模式', '点击切换模式', 2],
33-
['menu_customMode', '自定义当前模式', '自定义当前模式', true], ['menu_customMode1',,,'80|70'], ['menu_customMode2',,,'80|20|70|30'], ['menu_customMode3',,,'80']
33+
['menu_customMode', '自定义当前模式', '自定义当前模式', true], ['menu_customMode1',,,'80|70'], ['menu_customMode2',,,'80|20|70|30'], ['menu_customMode3',,,'80'],
34+
['menu_autoSwitch', '晚上自动切换模式', '晚上自动切换模式', ''],
3435
], menu_ID = [];
3536
for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
3637
if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
@@ -67,7 +68,8 @@
6768
menu_ALL[i][3] = 1;
6869
GM_setValue(menu_ALL[i][0], menu_ALL[i][3]);
6970
}
70-
menu_ID[i] = GM_registerMenuCommand(`${menu_num(menu_ALL[i][3])} ${menu_ALL[i][1]}`, function(){menu_toggle(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`)});
71+
let menu_newMode = getAutoSwitch();
72+
menu_ID[i] = GM_registerMenuCommand(`${menu_num(menu_newMode)} ${menu_ALL[i][1]}`, function(){menu_toggle(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`)});
7173
}
7274
else if (menu_ALL[i][0] === 'menu_customMode')
7375
{ // 自定义当前模式
@@ -78,6 +80,10 @@
7880
{ // 当前模式值
7981
GM_setValue(menu_ALL[i][0], menu_ALL[i][3]);
8082
}
83+
else if (menu_ALL[i][0] === 'menu_autoSwitch')
84+
{ // 晚上自动切换模式
85+
menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){menu_customAutoSwitch()});
86+
}
8187
else if (menu_ALL[i][0] === 'menu_forcedToEnable')
8288
{ // 强制当前网站启用护眼模式
8389
if (menu_value('menu_autoRecognition')) { // 自动排除自带暗黑模式的网页 (beta)
@@ -103,10 +109,42 @@
103109
}
104110

105111

112+
// 晚上自动切换模式
113+
function menu_customAutoSwitch() {
114+
let newAutoSwitch = prompt('白天、晚上使用不同模式,修改后立即生效~\n格式:白天模式|晚上模式\n例如:1|3(即白天模式 1 晚上模式 2)\n默认:留空(即关闭该功能)', GM_getValue('menu_autoSwitch'));
115+
if (newAutoSwitch === '') {
116+
GM_setValue('menu_autoSwitch', '');
117+
} else if (newAutoSwitch != null) {
118+
if (newAutoSwitch.split('|').length == 2) {
119+
GM_setValue('menu_autoSwitch', newAutoSwitch);
120+
} else {
121+
alert(`填入内容格式错误...`);
122+
}
123+
}
124+
registerMenuCommand(); // 重新注册脚本菜单
125+
if (document.getElementById('XIU2DarkMode')) {
126+
document.getElementById('XIU2DarkMode').remove(); // 即时修改样式
127+
addStyle();
128+
}
129+
}
130+
// 获取当前模式
131+
function getAutoSwitch() {
132+
let darkModeType = GM_getValue('menu_darkModeType'), hours = new Date().getHours();
133+
if (GM_getValue('menu_autoSwitch') != '') { // 晚上自动切换模式
134+
if (hours > 6 && hours < 19) { // 白天
135+
darkModeType = GM_getValue('menu_autoSwitch').split('|')[0];
136+
} else { // 晚上
137+
darkModeType = GM_getValue('menu_autoSwitch').split('|')[1];
138+
}
139+
}
140+
return parseInt(darkModeType)
141+
}
142+
143+
106144
// 自定义当前模式
107145
function menu_customMode() {
108146
let newMods, tip, defaults, name;
109-
switch(menu_value('menu_darkModeType')) {
147+
switch(getAutoSwitch()) {
110148
case 1:
111149
tip = '自定义 [模式 1],修改后立即生效 (部分网页可能需要刷新)~\n格式:亮度 (白天)|亮度 (晚上)\n默认:80|70(均为百分比 1~100,不需要 % 符号)';
112150
defaults = '80|70';
@@ -227,7 +265,12 @@
227265
menu_status += 1;
228266
}
229267
GM_setValue(`${Name}`, menu_status);
230-
location.reload(); // 刷新网页
268+
registerMenuCommand(); // 重新注册脚本菜单
269+
if (document.getElementById('XIU2DarkMode')) {
270+
document.getElementById('XIU2DarkMode').remove(); // 即时修改样式
271+
addStyle();
272+
}
273+
//location.reload(); // 刷新网页
231274
};
232275

233276

@@ -296,7 +339,10 @@
296339
}
297340
}
298341

299-
switch(menu_value('menu_darkModeType')) {
342+
343+
let darkModeType = getAutoSwitch();
344+
345+
switch(darkModeType) {
300346
case 1:
301347
style += style_12;
302348
break;

0 commit comments

Comments
 (0)