Skip to content

Commit 8cd5e5b

Browse files
committed
新增 [暗黑模式跟随浏览器] 功能
1 parent 2088a56 commit 8cd5e5b

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

DarkMode.user.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 护眼模式
3-
// @version 1.2.5
3+
// @version 1.2.6
44
// @author X.I.U
55
// @description 简单有效的全网通用护眼模式、夜间模式、暗黑模式
66
// @match *://*/*
@@ -29,6 +29,7 @@
2929
var menu_ALL = [
3030
['menu_disable', '✅ 已启用 (点击对当前网站禁用)', '❌ 已禁用 (点击对当前网站启用)', []],
3131
['menu_runDuringTheDay', '白天保持开启 (比晚上亮一点点)', '白天保持开启', true],
32+
['menu_darkModeAuto', '护眼模式跟随浏览器', '护眼模式跟随浏览器', false],
3233
['menu_autoRecognition', '智能排除自带暗黑模式的网页 (beta)', '智能排除自带暗黑模式的网页 (beta)', true],
3334
['menu_forcedToEnable', '✅ 已强制当前网站启用护眼模式 (👆)', '❌ 未强制当前网站启用护眼模式 (👆)', []],
3435
['menu_darkModeType', '点击切换模式', '点击切换模式', 1],
@@ -56,6 +57,10 @@
5657
menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][2]}`, function(){menu_disable('del')});
5758
return
5859
} else {
60+
if (GM_getValue('menu_darkModeAuto') && !window.matchMedia('(prefers-color-scheme: dark)').matches) {
61+
menu_ID[i] = GM_registerMenuCommand(`❌ 当前浏览器为白天模式 (点击关闭 [护眼模式跟随浏览器])`, function(){GM_setValue('menu_darkModeAuto', false);location.reload();});
62+
return
63+
}
5964
menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][1]}`, function(){menu_disable('add')});
6065
}
6166
}
@@ -327,9 +332,9 @@
327332
// 为了避免 body 还没加载导致无法检查是否设置背景颜色
328333
let timer = setInterval(function(){ // 每 5 毫秒检查一下 body 是否已存在
329334
if (document.body) {
330-
clearInterval(timer); // 取消定时器(每 10 毫秒一次的)
335+
clearInterval(timer); // 取消定时器(每 5 毫秒一次的)
331336
setTimeout(function(){ // 为了避免太快 body 的 CSS 还没加载上,先延迟 150 毫秒(缺点就是可能会出现短暂一闪而过的暗黑滤镜)
332-
console.log('html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
337+
console.log('[护眼模式] html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
333338
if (window.getComputedStyle(document.body).backgroundColor === 'rgba(0, 0, 0, 0)' && window.getComputedStyle(document.lastChild).backgroundColor === 'rgba(0, 0, 0, 0)') {
334339
// 如果 body 没有 CSS 背景颜色,那就需要添加一个背景颜色,否则影响滤镜效果
335340
let style_Add2 = document.createElement('style');
@@ -341,7 +346,7 @@
341346
for (let i=0;i<websiteList.length;i++){ // 这些网站强制启用护眼模式滤镜
342347
if (websiteList[i] === location.host) return
343348
}
344-
console.log('检测到当前网页自带暗黑模式,停用本脚本滤镜...')
349+
console.log('[护眼模式] 检测到当前网页自带暗黑模式,停用本脚本滤镜...')
345350
document.getElementById('XIU2DarkMode').remove();
346351
remove = true;
347352
}
@@ -350,15 +355,15 @@
350355

351356
// 用来解决一些 CSS 加载缓慢的网站,可能会出现没有正确排除的问题,在没有找到更好的办法之前,先这样凑活着用
352357
setTimeout(function(){
353-
console.log('html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
358+
console.log('[护眼模式] html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
354359
if (window.getComputedStyle(document.body).backgroundColor === 'rgb(0, 0, 0)' || getColorValue(document.body) > 0 && getColorValue(document.body) < 898989 || getColorValue(document.lastChild) > 0 && getColorValue(document.lastChild) < 898989 || window.getComputedStyle(document.body).backgroundColor === 'rgba(0, 0, 0, 0)' && window.getComputedStyle(document.lastChild).backgroundColor === 'rgb(0, 0, 0)') {
355360
// 如果是黑色 (等于0,0,0) 或深色 (小于 89,89,89),就停用本脚本滤镜
356361
if (menu_value('menu_autoRecognition')) { // 排除自带暗黑模式的网页 (beta)
357362
for (let i=0;i<websiteList.length;i++){ // 这些网站强制启用护眼模式滤镜
358363
if (websiteList[i] === location.host) return
359364
}
360365
if (remove) return
361-
console.log('检测到当前网页自带暗黑模式,停用本脚本滤镜...')
366+
console.log('[护眼模式] 检测到当前网页自带暗黑模式,停用本脚本滤镜...')
362367
if (document.getElementById('XIU2DarkMode')) document.getElementById('XIU2DarkMode').remove();
363368
if (document.getElementById('XIU2DarkMode2')) document.getElementById('XIU2DarkMode2').remove();
364369
}
@@ -369,15 +374,15 @@
369374

370375
// 用来解决一些 CSS 加载缓慢的网站,可能会出现没有正确排除的问题,在没有找到更好的办法之前,先这样凑活着用
371376
/*setTimeout(function(){
372-
console.log('html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
377+
console.log('[护眼模式] html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
373378
if (window.getComputedStyle(document.body).backgroundColor === 'rgb(0, 0, 0)' || getColorValue(document.body) > 0 && getColorValue(document.body) < 898989 || getColorValue(document.lastChild) > 0 && getColorValue(document.lastChild) < 898989) {
374379
// 如果是黑色 (等于0,0,0) 或深色 (小于 89,89,89),就停用本脚本滤镜
375380
if (menu_value('menu_autoRecognition')) { // 排除自带暗黑模式的网页 (beta)
376381
for (let i=0;i<websiteList.length;i++){ // 这些网站强制启用护眼模式滤镜
377382
if (websiteList[i] === location.host) return
378383
}
379384
if (remove) return
380-
console.log('检测到当前网页自带暗黑模式,停用本脚本滤镜...')
385+
console.log('[护眼模式] 检测到当前网页自带暗黑模式,停用本脚本滤镜...')
381386
if (document.getElementById('XIU2DarkMode')) document.getElementById('XIU2DarkMode').remove();
382387
if (document.getElementById('XIU2DarkMode2')) document.getElementById('XIU2DarkMode2').remove();
383388
}

0 commit comments

Comments
 (0)