护眼模式

简单有效的全网通用护眼模式(夜间模式、暗黑模式、深色模式)

当前为 2021-12-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 护眼模式
  3. // @version 1.3.2
  4. // @author X.I.U
  5. // @description 简单有效的全网通用护眼模式(夜间模式、暗黑模式、深色模式)
  6. // @match *://*/*
  7. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAALfElEQVRYhX2Xe3Bd1XXGf3vvc859X+nq6nUlW5Yly7JlI2xsYzAwtnk4ATpAxkNTHm0mnaTT/gHTTvrIBDLTpp1JUoZppqHQls5AKTR2INOWJJQSXF4x2BhsJCRZ8kuWZckPSVf3/Trn7N3RVTFpQrNn1l97n7O/vda31reWMKMPcmUJA9U8vrwHGdqCHn4HPzePaIxhVSoYbYRXrn7BeMVbCUduF6kVUXHwvQP+6amDaqDnoIompmQytaBnTmB8H5lowrjgFss48SBeI/hUEEZeudLi1ywhJEIJdL6Q8rzal/1a5SGC4XZrYBvWwEZobMdLdH6RH+z/Io1taEeh52fe8tOZbysl/ouWFvANYP7fSz4DgAEBBIL4xiS8ubmnVcTZK68aRK29Dtm8dgnZJydRW+/E2nrnp19nz+7U77+60zt0qMz07J/KxuQTwrIw4rMBCDP6wC+FIIcO34eudDdXf/7jD52Opi772lugY3AZr++hp06gz48j+waRqTWYmVHcS+chEMFeuw1hBzBzY7g/fQE9fmqBYPzBQKrpVa/R4OkCAnXlSvXnX9sIllk220BE4Z8OdHoj54YCK6Od1i2/iUmuRyDRk6NUn3+M0pv/hnf0AE40jEjEqP3oe6Rf/CGOWUTNjFKby2MP7EBtugURFWFxfOhB4+o4yfhrGAdZsxHaqZt6dNce9KXYFSPfGWS68JFqTXSqO7+MCaTqETGTwxSeeoRCOoPT2YUIhFC2jbQF/uwUatU6rPbVUM5T+OfHUO3dWKv6kSsGUIOD6PEPr+fswnanpecFZYVQhFAyjPS9Tj4xw2rcU+pJApEutWsvRjaBW8NUShilkE1JIq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+4LK2jtcuLqK9t7ALjZXzH1uVM/mb/7BTRnjZEex+idQ2EAnjjR/BdF1PIoV2Bae/F/fh9dL5IYE0XUmpMNo+ev4AINSJWb0UPHa63Z7SvRN3/CHJpFHj2G4iZzAuBbTu+GWpIEGxrQ/3RnlvxG5M4kYa3Lc2u4uyFblnIE+xfAbF2RMtqZGsn/uTYUgHH+dz9eFMnqYx8gO+CEwkjhcDPF/DLHurSaeTm3RiWMsDHeuDrSFvBv3+bi08+N1Vwuj/vZWtu+dwclZlFrESoeZmZysJXoTsCjnVk8dDQBuN+n8RDD9fbcdG7leBX1iOkQjhBOHmQfKGAHW3CS6fR+QClmYuYMyPY265DtPVgtfctiw4V/GcfYfGNQzj3/u4DqlormUoZEQwuc0I//eSnY4G0wPJjmdGhw+mh0fWxNZ0037cXccPd1MXgk+GjNE/tR3+Le+YEyg5TnT6LXlwgcu1u7C99HZlsv3K2+vjvkTs2RvDe3747tm7Dy1SKvzSYPP/MLwxFAkIKEzEye2zswOXX395lJyK0fOFzRHfuglQfqPin59Nn0RNDuJk09vprkd0brmx5H7xCbfgQbrZ83OkfvN1pj0+ZmkBYNhjzCwCeffr/AsBDJyU6kqBwbPzRuTcP/GXV82lYlaJpfQ+BjVdhXX0tNKTAjvzKqKXPTeC++1NKY0c9kVz1SGzb9X8tjIdfzSFCDfUw/noA0scPuxBrwcvmmH3rQIvMm3/y0XdJS4JfIpiIUG9g+wcg6KDnZpGZNDQ0Ii5cSAtlPRHYct13axWvJNwadiiAli4iEP8VAJ89HQtR129TLiG1nos0Nt8dSOi12qi9lRq3utVqT/lirql24hW3vLBQi3d3XUqu73+PZOonBNR/WnbYSMeGTO5/Xf6ZtwDwPwtFRezQVs+sAAAAAElFTkSuQmCC
  8. // @grant GM_registerMenuCommand
  9. // @grant GM_unregisterMenuCommand
  10. // @grant GM_openInTab
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_notification
  14. // @noframes
  15. // @license GPL-3.0 License
  16. // @run-at document-start
  17. // @namespace https://github.com/XIU2/UserScript
  18. // @supportURL https://github.com/XIU2/UserScript
  19. // @homepageURL https://github.com/XIU2/UserScript
  20. // ==/UserScript==
  21.  
  22. (function() {
  23. 'use strict';
  24. var menu_ALL = [
  25. ['menu_disable', '✅ 已启用 (点击对当前网站禁用)', '❌ 已禁用 (点击对当前网站启用)', []],
  26. ['menu_runDuringTheDay', '白天保持开启 (比晚上亮一点点)', '白天保持开启', true],
  27. ['menu_darkModeAuto', '护眼模式跟随浏览器', '护眼模式跟随浏览器', false],
  28. ['menu_autoRecognition', '智能排除自带暗黑模式的网页 (beta)', '智能排除自带暗黑模式的网页 (beta)', true],
  29. ['menu_forcedToEnable', '✅ 已强制当前网站启用护眼模式 (👆)', '❌ 未强制当前网站启用护眼模式 (👆)', []],
  30. ['menu_darkModeType', '点击切换模式', '点击切换模式', 2],
  31. ['menu_customMode', '自定义当前模式', '自定义当前模式', true], ['menu_customMode1',,,'80|70'], ['menu_customMode2',,,'80|20|70|30'], ['menu_customMode3',,,'80'],
  32. ['menu_customTime', '自定义昼夜时间', '自定义昼夜时间', '6|18'],
  33. ['menu_autoSwitch', '晚上自动切换模式', '晚上自动切换模式', ''],
  34. ], menu_ID = [];
  35. for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
  36. if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
  37. }
  38. registerMenuCommand();
  39. if (menu_ID.length > 1) {addStyle();}
  40.  
  41.  
  42. // 注册脚本菜单
  43. function registerMenuCommand() {
  44. if (menu_ID.length != []){
  45. for (let i=0;i<menu_ID.length;i++){
  46. GM_unregisterMenuCommand(menu_ID[i]);
  47. }
  48. }
  49. for (let i=0;i<menu_ALL.length;i++){ // 循环注册脚本菜单
  50. menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
  51. if (menu_ALL[i][0] === 'menu_disable')
  52. { // 启用/禁用护眼模式 (当前网站)
  53. if (menu_disable('check')) { // 当前网站是否已存在禁用列表中
  54. menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][2]}`, function(){menu_disable('del')});
  55. return
  56. } else {
  57. if (GM_getValue('menu_darkModeAuto') && !window.matchMedia('(prefers-color-scheme: dark)').matches) {
  58. menu_ID[i] = GM_registerMenuCommand(`❌ 当前浏览器为白天模式 (点击关闭 [护眼模式跟随浏览器])`, function(){GM_setValue('menu_darkModeAuto', false);location.reload();});
  59. return
  60. }
  61. menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][1]}`, function(){menu_disable('add')});
  62. }
  63. }
  64. else if (menu_ALL[i][0] === 'menu_darkModeType')
  65. { // 点击切换模式
  66. if (menu_ALL[i][3] > 3) { // 避免在减少 raw 数组后,用户储存的数据大于数组而报错
  67. menu_ALL[i][3] = 1;
  68. GM_setValue(menu_ALL[i][0], menu_ALL[i][3]);
  69. }
  70. let menu_newMode = getAutoSwitch();
  71. menu_ID[i] = GM_registerMenuCommand(`${menu_num(menu_newMode)} ${menu_ALL[i][1]}`, function(){menu_toggle(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`)});
  72. }
  73. else if (menu_ALL[i][0] === 'menu_customMode')
  74. { // 自定义当前模式
  75. GM_setValue(menu_ALL[i][0], menu_ALL[i][3]);
  76. menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){menu_customMode()});
  77. }
  78. else if (menu_ALL[i][0] === 'menu_customTime')
  79. { // 自定义昼夜时间
  80. GM_setValue(menu_ALL[i][0], menu_ALL[i][3]);
  81. menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){menu_customTime()});
  82. }
  83. else if (menu_ALL[i][0] === 'menu_customMode1' || menu_ALL[i][0] === 'menu_customMode2' || menu_ALL[i][0] === 'menu_customMode3')
  84. { // 当前模式值
  85. GM_setValue(menu_ALL[i][0], menu_ALL[i][3]);
  86. }
  87. else if (menu_ALL[i][0] === 'menu_autoSwitch')
  88. { // 晚上自动切换模式
  89. menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){menu_customAutoSwitch()});
  90. }
  91. else if (menu_ALL[i][0] === 'menu_forcedToEnable')
  92. { // 强制当前网站启用护眼模式
  93. if (menu_value('menu_autoRecognition')) { // 自动排除自带暗黑模式的网页 (beta)
  94. if (menu_forcedToEnable('check')) { // 当前网站是否已存在列表中
  95. menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][1]}`, function(){menu_forcedToEnable('del')});
  96. } else {
  97. menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][2]}`, function(){menu_forcedToEnable('add')});
  98. }
  99. }
  100. }
  101. else
  102. {
  103. menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][3]?'✅':'❌'} ${menu_ALL[i][1]}`, function(){menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)});
  104. }
  105. }
  106. menu_ID[menu_ID.length] = GM_registerMenuCommand('💬 反馈 & 建议', function () {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/412212/feedback', {active: true,insert: true,setParent: true});});
  107. }
  108.  
  109.  
  110. // 菜单数字图标
  111. function menu_num(num) {
  112. return ['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','🔟'][num]
  113. }
  114.  
  115.  
  116. // 晚上自动切换模式
  117. function menu_customAutoSwitch() {
  118. let newAutoSwitch = prompt('白天、晚上使用不同模式,修改后立即生效~\n格式:白天模式|晚上模式\n例如:1|3(即白天模式 1 晚上模式 3)\n默认:留空(即关闭该功能)', GM_getValue('menu_autoSwitch'));
  119. if (newAutoSwitch === '') {
  120. GM_setValue('menu_autoSwitch', '');
  121. } else if (newAutoSwitch != null) {
  122. if (newAutoSwitch.split('|').length == 2) {
  123. GM_setValue('menu_autoSwitch', newAutoSwitch);
  124. } else {
  125. alert(`填入内容格式错误...`);
  126. }
  127. }
  128. registerMenuCommand(); // 重新注册脚本菜单
  129. if (document.getElementById('XIU2DarkMode')) {
  130. document.getElementById('XIU2DarkMode').remove(); // 即时修改样式
  131. addStyle();
  132. }
  133. }
  134. // 获取当前模式
  135. function getAutoSwitch() {
  136. let darkModeType = GM_getValue('menu_darkModeType'), hours = new Date().getHours(), time = GM_getValue('menu_customTime').split('|').map(Number);
  137. if (GM_getValue('menu_autoSwitch') != '') { // 晚上自动切换模式
  138. if (hours > time[0] && hours < time[1]) { // 白天
  139. darkModeType = GM_getValue('menu_autoSwitch').split('|')[0];
  140. } else { // 晚上
  141. darkModeType = GM_getValue('menu_autoSwitch').split('|')[1];
  142. }
  143. }
  144. return parseInt(darkModeType)
  145. }
  146.  
  147.  
  148. // 自定义当前模式
  149. function menu_customMode() {
  150. let newMods, tip, defaults, name;
  151. switch(getAutoSwitch()) {
  152. case 1:
  153. tip = '自定义 [模式 1],修改后立即生效 (部分网页可能需要刷新)~\n格式:亮度 (白天)|亮度 (晚上)\n默认:80|70(均为百分比 1~100,不需要 % 符号)';
  154. defaults = '80|70';
  155. name = 'menu_customMode1';
  156. break;
  157. case 2:
  158. tip = '自定义 [模式 2],修改后立即生效 (部分网页可能需要刷新)~\n格式:亮度 (白天)|暖色 (白天)|亮度 (晚上)|暖色 (晚上)\n默认:80|20|70|30(均为百分比 1~100,不需要 % 符号)';
  159. defaults = '80|20|70|30';
  160. name = 'menu_customMode2';
  161. break;
  162. case 3:
  163. tip = '自定义 [模式 3],修改后立即生效 (部分网页可能需要刷新)~\n格式:反色\n默认:80(均为百分比 50~100,不需要 % 符号)';
  164. defaults = '80';
  165. name = 'menu_customMode3';
  166. break;
  167. }
  168. newMods = prompt(tip, GM_getValue(`${name}`));
  169. if (newMods === '') {
  170. GM_setValue(`${name}`, defaults);
  171. registerMenuCommand(); // 重新注册脚本菜单
  172. } else if (newMods != null) {
  173. GM_setValue(`${name}`, newMods);
  174. registerMenuCommand(); // 重新注册脚本菜单
  175. }
  176. if (document.getElementById('XIU2DarkMode')) {
  177. document.getElementById('XIU2DarkMode').remove(); // 即时修改样式
  178. addStyle();
  179. }
  180. }
  181.  
  182.  
  183. // 自定义昼夜时间
  184. function menu_customTime() {
  185. let newMods = prompt('自定义脚本内和白天/晚上相关的时间,修改后刷新网页生效~\n格式:6|18 (即 6:00 ~ 18:00 之间是白天时间)', GM_getValue('menu_customTime'));
  186. if (newMods === '') {
  187. GM_setValue('menu_customTime', '6|18');
  188. registerMenuCommand(); // 重新注册脚本菜单
  189. } else if (newMods != null) {
  190. GM_setValue('menu_customTime', newMods);
  191. registerMenuCommand(); // 重新注册脚本菜单
  192. }
  193. }
  194.  
  195.  
  196. // 强制当前网站启用护眼模式
  197. function menu_forcedToEnable(type) {
  198. switch(type) {
  199. case 'check':
  200. if(check()) return true
  201. return false
  202. break;
  203. case 'add':
  204. add();
  205. break;
  206. case 'del':
  207. del();
  208. break;
  209. }
  210.  
  211. function check() { // 存在返回真,不存在返回假
  212. let websiteList = menu_value('menu_forcedToEnable'); // 读取网站列表
  213. if (websiteList.indexOf(location.host) === -1) return false // 不存在返回假
  214. return true
  215. }
  216.  
  217. function add() {
  218. if (check()) return
  219. let websiteList = menu_value('menu_forcedToEnable'); // 读取网站列表
  220. websiteList.push(location.host); // 追加网站域名
  221. GM_setValue('menu_forcedToEnable', websiteList); // 写入配置
  222. location.reload(); // 刷新网页
  223. }
  224.  
  225. function del() {
  226. if (!check()) return
  227. let websiteList = menu_value('menu_forcedToEnable'), // 读取网站列表
  228. index = websiteList.indexOf(location.host);
  229. websiteList.splice(index, 1); // 删除网站域名
  230. GM_setValue('menu_forcedToEnable', websiteList); // 写入配置
  231. location.reload(); // 刷新网页
  232. }
  233. }
  234.  
  235.  
  236. // 启用/禁用护眼模式 (当前网站)
  237. function menu_disable(type) {
  238. switch(type) {
  239. case 'check':
  240. if(check()) return true
  241. return false
  242. break;
  243. case 'add':
  244. add();
  245. break;
  246. case 'del':
  247. del();
  248. break;
  249. }
  250.  
  251. function check() { // 存在返回真,不存在返回假
  252. let websiteList = menu_value('menu_disable'); // 读取网站列表
  253. if (websiteList.indexOf(location.host) === -1) return false // 不存在返回假
  254. return true
  255. }
  256.  
  257. function add() {
  258. if (check()) return
  259. let websiteList = menu_value('menu_disable'); // 读取网站列表
  260. websiteList.push(location.host); // 追加网站域名
  261. GM_setValue('menu_disable', websiteList); // 写入配置
  262. location.reload(); // 刷新网页
  263. }
  264.  
  265. function del() {
  266. if (!check()) return
  267. let websiteList = menu_value('menu_disable'), // 读取网站列表
  268. index = websiteList.indexOf(location.host);
  269. websiteList.splice(index, 1); // 删除网站域名
  270. GM_setValue('menu_disable', websiteList); // 写入配置
  271. location.reload(); // 刷新网页
  272. }
  273. }
  274.  
  275.  
  276. // 切换暗黑模式
  277. function menu_toggle(menu_status, Name) {
  278. menu_status = parseInt(menu_status)
  279. if (menu_status >= 3){
  280. menu_status = 1;
  281. } else {
  282. menu_status += 1;
  283. }
  284. GM_setValue(`${Name}`, menu_status);
  285. registerMenuCommand(); // 重新注册脚本菜单
  286. if (document.getElementById('XIU2DarkMode')) {
  287. document.getElementById('XIU2DarkMode').remove(); // 即时修改样式
  288. addStyle();
  289. }
  290. //location.reload(); // 刷新网页
  291. };
  292.  
  293.  
  294. // 菜单开关
  295. function menu_switch(menu_status, Name, Tips) {
  296. if (menu_status == 'true'){
  297. GM_setValue(`${Name}`, false);
  298. GM_notification({text: `已关闭 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
  299. }else{
  300. GM_setValue(`${Name}`, true);
  301. GM_notification({text: `已开启 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
  302. }
  303. if (Name === 'menu_autoRecognition') {
  304. location.reload(); // 刷新网页
  305. }
  306. registerMenuCommand(); // 重新注册脚本菜单
  307. };
  308.  
  309.  
  310. // 返回菜单值
  311. function menu_value(menuName) {
  312. for (let menu of menu_ALL) {
  313. if (menu[0] == menuName) {
  314. return menu[3]
  315. }
  316. }
  317. }
  318.  
  319.  
  320. // 添加样式
  321. function addStyle() {
  322. let remove = false, style_Add = document.createElement('style'),
  323. hours = new Date().getHours(),
  324. time = GM_getValue('menu_customTime').split('|').map(Number),
  325. style_10 = menu_value('menu_customMode1').split('|'),
  326. style_20 = menu_value('menu_customMode2').split('|'),
  327. style_30 = menu_value('menu_customMode3').split('|'),
  328. style = ``,
  329. style_00 = `html, body {background-color: #ffffff;}`,
  330. style_11 = `html {filter: brightness(${style_10[0]}%) !important;}`,
  331. style_11_firefox = `html {filter: brightness(${style_10[0]}%) !important; background-image: url();}`,
  332. style_12 = `html {filter: brightness(${style_10[1]}%) !important;}`,
  333. style_12_firefox = `html {filter: brightness(${style_10[1]}%) !important; background-image: url();}`,
  334. style_21 = `html {filter: brightness(${style_20[0]}%) sepia(${style_20[1]}%) !important;}`,
  335. style_21_firefox = `html {filter: brightness(${style_20[0]}%) sepia(${style_20[1]}%) !important; background-image: url();}`,
  336. style_22 = `html {filter: brightness(${style_20[2]}%) sepia(${style_20[3]}%) !important;}`,
  337. style_22_firefox = `html {filter: brightness(${style_20[2]}%) sepia(${style_20[3]}%) !important; background-image: url();}`,
  338. style_31 = `html {filter: invert(${style_30[0]}%) !important;} img, video, [style*="background"][style*="url"] {filter: invert(1) !important;} .mode-fullscreen video, img[alt="[公式]"] {filter: none !important;}`,
  339. style_31_firefox = `html {filter: invert(${style_30[0]}%) !important; background-image: url();} img, video, [style*="background"][style*="url"] {filter: invert(1) !important;} .mode-fullscreen video, img[alt="[公式]"] {filter: none !important;}`;
  340.  
  341. // Firefox 浏览器需要特殊对待
  342. if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
  343. style_11 = style_11_firefox
  344. style_12 = style_12_firefox
  345. style_21 = style_21_firefox
  346. style_22 = style_22_firefox
  347. style_31 = style_31_firefox
  348. }
  349.  
  350. // 白天
  351. if (hours > time[0] && hours < time[1]) {
  352. if (menu_value('menu_runDuringTheDay')) {
  353. style_12 = style_11
  354. style_22 = style_21
  355. } else {
  356. style_12 = style_22 = ''
  357. }
  358. }
  359.  
  360.  
  361. let darkModeType = getAutoSwitch();
  362.  
  363. switch(darkModeType) {
  364. case 1:
  365. style += style_12;
  366. break;
  367. case 2:
  368. style += style_22;
  369. break;
  370. case 3:
  371. style += style_31;
  372. break;
  373. }
  374. style_Add.id = 'XIU2DarkMode';
  375. style_Add.type = 'text/css';
  376. //console.log(document,document.lastElementChild,document.querySelector('html'))
  377. if (document.lastElementChild) {
  378. document.lastElementChild.appendChild(style_Add).textContent = style;
  379. } else { // 发现个别网站速度太慢的话,就会出现脚本运行太早,连 html 标签都还没加载。。。
  380. let timer1 = setInterval(function(){ // 每 5 毫秒检查一下 html 是否已存在
  381. if (document.lastElementChild) {
  382. clearInterval(timer1); // 取消定时器
  383. document.lastElementChild.appendChild(style_Add).textContent = style;
  384. }
  385. });
  386. }
  387.  
  388. let websiteList = [];
  389. if (menu_value('menu_autoRecognition')) { // 智能排除自带暗黑模式的网页 (beta)
  390. websiteList = menu_value('menu_forcedToEnable'); // 强制当前网站启用护眼模式
  391. }
  392.  
  393. // 为了避免 body 还没加载导致无法检查是否设置背景颜色
  394. let timer = setInterval(function(){ // 每 5 毫秒检查一下 body 是否已存在
  395. if (document.body) {
  396. clearInterval(timer); // 取消定时器(每 5 毫秒一次的)
  397. setTimeout(function(){ // 为了避免太快 body 的 CSS 还没加载上,先延迟 150 毫秒(缺点就是可能会出现短暂一闪而过的暗黑滤镜)
  398. console.log('[护眼模式] html:', window.getComputedStyle(document.lastElementChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
  399. if (window.getComputedStyle(document.body).backgroundColor === 'rgba(0, 0, 0, 0)' && window.getComputedStyle(document.lastElementChild).backgroundColor === 'rgba(0, 0, 0, 0)') {
  400. // 如果 body 没有 CSS 背景颜色,那就需要添加一个背景颜色,否则影响滤镜效果
  401. let style_Add2 = document.createElement('style');
  402. style_Add2.id = 'XIU2DarkMode2';
  403. document.lastElementChild.appendChild(style_Add2).textContent = style_00;
  404. } else if (window.getComputedStyle(document.body).backgroundColor === 'rgb(0, 0, 0)' || getColorValue(document.body) > 0 && getColorValue(document.body) < 898989 || getColorValue(document.lastElementChild) > 0 && getColorValue(document.lastElementChild) < 898989 || window.getComputedStyle(document.body).backgroundColor === 'rgba(0, 0, 0, 0)' && window.getComputedStyle(document.lastElementChild).backgroundColor === 'rgb(0, 0, 0)') {
  405. // 如果是黑色 (等于0,0,0) 或深色 (小于 89,89,89),就停用本脚本滤镜
  406. if (menu_value('menu_autoRecognition')) { // 排除自带暗黑模式的网页 (beta)
  407. for (let i=0;i<websiteList.length;i++){ // 这些网站强制启用护眼模式滤镜
  408. if (websiteList[i] === location.host) return
  409. }
  410. console.log('[护眼模式] 检测到当前网页自带暗黑模式,停用本脚本滤镜...')
  411. document.getElementById('XIU2DarkMode').remove();
  412. remove = true;
  413. }
  414. }
  415. }, 150);
  416.  
  417. // 用来解决一些 CSS 加载缓慢的网站,可能会出现没有正确排除的问题,在没有找到更好的办法之前,先这样凑活着用
  418. setTimeout(function(){
  419. console.log('[护眼模式] html:', window.getComputedStyle(document.lastElementChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
  420. if (window.getComputedStyle(document.body).backgroundColor === 'rgb(0, 0, 0)' || getColorValue(document.body) > 0 && getColorValue(document.body) < 898989 || getColorValue(document.lastElementChild) > 0 && getColorValue(document.lastElementChild) < 898989 || window.getComputedStyle(document.body).backgroundColor === 'rgba(0, 0, 0, 0)' && window.getComputedStyle(document.lastElementChild).backgroundColor === 'rgb(0, 0, 0)') {
  421. // 如果是黑色 (等于0,0,0) 或深色 (小于 89,89,89),就停用本脚本滤镜
  422. if (menu_value('menu_autoRecognition')) { // 排除自带暗黑模式的网页 (beta)
  423. for (let i=0;i<websiteList.length;i++){ // 这些网站强制启用护眼模式滤镜
  424. if (websiteList[i] === location.host) return
  425. }
  426. if (remove) return
  427. console.log('[护眼模式] 检测到当前网页自带暗黑模式,停用本脚本滤镜...')
  428. if (document.getElementById('XIU2DarkMode')) document.getElementById('XIU2DarkMode').remove();
  429. if (document.getElementById('XIU2DarkMode2')) document.getElementById('XIU2DarkMode2').remove();
  430. }
  431. }
  432. }, 1500);
  433. }
  434. });
  435.  
  436. // 解决远景论坛会清理掉前面插入的 CSS 样式的问题
  437. if (location.hostname === 'bbs.pcbeta.com') {
  438. let timer1 = setInterval(function(){
  439. if (!document.getElementById('XIU2DarkMode')) {
  440. document.lastElementChild.appendChild(style_Add).textContent = style;
  441. clearInterval(timer1);
  442. }
  443. });
  444. }
  445. }
  446.  
  447. // 获取背景颜色值
  448. function getColorValue(e) {
  449. let rgbValueArry = window.getComputedStyle(e).backgroundColor.replace(/rgba|rgb|\(|\)| /g, '').split (',')
  450. return parseInt(rgbValueArry[0] + rgbValueArry[1] + rgbValueArry[2])
  451. }
  452. })();