Skip to content

Commit 3ab2ad8

Browse files
committed
优化 [排除自带暗黑模式的网页 (beta)] 功能
1 parent 58f3765 commit 3ab2ad8

1 file changed

Lines changed: 36 additions & 13 deletions

File tree

DarkMode.user.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 护眼模式
3-
// @version 1.0.10
3+
// @version 1.1.0
44
// @author X.I.U
55
// @description 简单有效的全网通用护眼模式、夜间模式、暗黑模式
66
// @match *://*/*
@@ -26,7 +26,7 @@
2626
['menu_runDuringTheDay', '白天保持开启 (比晚上亮一点点)', '白天保持开启', true],
2727
['menu_autoRecognition', '排除自带暗黑模式的网页 (beta)', '排除自带暗黑模式的网页 (beta)', true],
2828
['menu_darkModeType', '点击切换模式', '点击切换模式', 1]
29-
], menu_ID = [];
29+
], menu_ID = [], websiteList = ['rarbgprx.org','fitgirl-repacks.site','masquerade.site'];
3030
for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
3131
if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
3232
}
@@ -95,7 +95,7 @@
9595

9696
// 添加样式
9797
function addStyle() {
98-
let style_Add = document.createElement('style'),
98+
let remove = false, style_Add = document.createElement('style'),
9999
hours = new Date().getHours(),
100100
style = ``,
101101
style_00 = `html {background-color: #ffffff;}`,
@@ -147,26 +147,49 @@
147147
let timer = setInterval(function(){ // 每 10 毫秒检查一下 body 是否已存在
148148
if (document.body) {
149149
clearInterval(timer); // 取消定时器(每 10 毫秒一次的)
150-
setTimeout(function(){ // 为了避免太快 body 的 CSS 还没加载上,先延迟 100 毫秒(缺点就是可能会出现短暂一闪而过的暗黑滤镜)
150+
setTimeout(function(){ // 为了避免太快 body 的 CSS 还没加载上,先延迟 150 毫秒(缺点就是可能会出现短暂一闪而过的暗黑滤镜)
151151
console.log('html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
152-
/*let rgbValueArry1 = window.getComputedStyle(document.body).backgroundColor.replace(/rgba|rgb|\(|\)| /g, '').split (','),
153-
rgbValueArry2 = window.getComputedStyle(document.lastChild).backgroundColor.replace(/rgba|rgb|\(|\)| /g, '').split (','),
154-
grayLevelBody = parseInt(rgbValueArry1[0]) + parseInt(rgbValueArry1[1]) + parseInt(rgbValueArry1[2]),
155-
grayLevelHTML = parseInt(rgbValueArry2[0]) + parseInt(rgbValueArry2[1]) + parseInt(rgbValueArry2[2]);
156-
console.log('html:', window.getComputedStyle(document.lastChild).backgroundColor, rgbValueArry2, grayLevelHTML, 'body:', window.getComputedStyle(document.body).backgroundColor, grayLevelBody)*/
157-
158152
if (window.getComputedStyle(document.body).backgroundColor === 'rgba(0, 0, 0, 0)' && window.getComputedStyle(document.lastChild).backgroundColor === 'rgba(0, 0, 0, 0)') {
159153
// 如果 body 没有 CSS 背景颜色,那就需要添加一个背景颜色,否则影响滤镜效果
160-
document.lastChild.appendChild(document.createElement('style')).textContent = style_00;
161-
} else if (window.getComputedStyle(document.body).backgroundColor === 'rgb(0, 0, 0)' || window.getComputedStyle(document.body).backgroundColor != 'rgba(0, 0, 0, 0)' && parseInt(window.getComputedStyle(document.body).backgroundColor.replace(/rgba|rgb|\(|\)|,| /g, '')) < 898989 || window.getComputedStyle(document.lastChild).backgroundColor != 'rgba(0, 0, 0, 0)' && parseInt(window.getComputedStyle(document.lastChild).backgroundColor.replace(/rgba|rgb|\(|\)|,| /g, '')) < 898989) {
162-
// 如果是黑色 (等于0,0,0) 或深色 (小于 88,88,88),就停用本脚本滤镜
154+
let style_Add2 = document.createElement('style');
155+
style_Add2.id = 'XIU2DarkMode2';
156+
document.lastChild.appendChild(style_Add2).textContent = style_00;
157+
} else 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) {
158+
// 如果是黑色 (等于0,0,0) 或深色 (小于 89,89,89),就停用本脚本滤镜
163159
if (menu_value('menu_autoRecognition')) { // 排除自带暗黑模式的网页 (beta)
160+
for (let i=0;i<websiteList.length;i++){ // 这些网站强制启用护眼模式滤镜
161+
if (websiteList[i] === location.host) return
162+
}
164163
console.log('检测到当前网页自带暗黑模式,停用本脚本滤镜...')
165164
document.getElementById('XIU2DarkMode').remove();
165+
remove = true;
166166
}
167167
}
168168
}, 150);
169169
}
170170
}, 10);
171+
172+
// 在没有找到更好的办法之前,先这样凑活着用
173+
setTimeout(function(){
174+
console.log('html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
175+
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) {
176+
// 如果是黑色 (等于0,0,0) 或深色 (小于 89,89,89),就停用本脚本滤镜
177+
if (menu_value('menu_autoRecognition')) { // 排除自带暗黑模式的网页 (beta)
178+
for (let i=0;i<websiteList.length;i++){ // 这些网站强制启用护眼模式滤镜
179+
if (websiteList[i] === location.host) return
180+
}
181+
if (remove) return
182+
console.log('检测到当前网页自带暗黑模式,停用本脚本滤镜...')
183+
if (document.getElementById('XIU2DarkMode')) document.getElementById('XIU2DarkMode').remove();
184+
if (document.getElementById('XIU2DarkMode2')) document.getElementById('XIU2DarkMode2').remove();
185+
}
186+
}
187+
}, 3000);
188+
}
189+
190+
// 获取背景颜色值
191+
function getColorValue(e) {
192+
let rgbValueArry = window.getComputedStyle(e).backgroundColor.replace(/rgba|rgb|\(|\)| /g, '').split (',')
193+
return parseInt(rgbValueArry[0] + rgbValueArry[1] + rgbValueArry[2])
171194
}
172195
})();

0 commit comments

Comments
 (0)