护眼模式

最简单的全网通用护眼模式、夜间模式、暗黑模式

目前為 2021-06-22 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name 护眼模式
  3. // @version 1.0.9
  4. // @author X.I.U
  5. // @description 最简单的全网通用护眼模式、夜间模式、暗黑模式
  6. // @match *://*/*
  7. // @exclude *://v.qq.com/*
  8. // @exclude *://*.iqiyi.com/*
  9. // @exclude *://*.youku.com/*
  10. // @exclude *://*.mgtv.com/*
  11. // @exclude *://tv.cctv.com/*
  12. // @icon https://i.loli.net/2021/03/07/rdijeYm83pznxWq.png
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_unregisterMenuCommand
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_notification
  18. // @noframes
  19. // @license GPL-3.0 License
  20. // @run-at document-start
  21. // @namespace https://github.com/XIU2/UserScript
  22. // ==/UserScript==
  23.  
  24. (function() {
  25. var menu_ALL = [
  26. ['menu_runDuringTheDay', '白天保持开启 (比晚上亮一点点)', '白天保持开启', true],
  27. ['menu_autoRecognition', '排除自带暗黑模式的网页 (beta)', '排除自带暗黑模式的网页 (beta)', true],
  28. ['menu_darkModeType', '点击切换模式', '点击切换模式', 1]
  29. ], menu_ID = [];
  30. for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
  31. if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
  32. }
  33. registerMenuCommand();
  34. addStyle();
  35.  
  36.  
  37. // 注册(不可用)脚本菜单
  38. function registerMenuCommand() {
  39. if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
  40. for (let i=0;i<menu_ID.length;i++){
  41. GM_unregisterMenuCommand(menu_ID[i]);
  42. }
  43. }
  44. for (let i=0;i<menu_ALL.length;i++){ // 循环注册(不可用)脚本菜单
  45. menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
  46. if (menu_ALL[i][0] === 'menu_darkModeType') {
  47. if (menu_ALL[i][3] > 3){ // 避免在减少 raw 数组后,用户储存的数据大于数组而报错
  48. menu_ALL[i][3] = 1;
  49. GM_setValue('menu_darkModeType', menu_ALL[i][3]);
  50. }
  51. menu_ID[i] = GM_registerMenuCommand(`🔄 [ ${menu_ALL[i][3]} ] ${menu_ALL[i][1]}`, function(){menu_toggle(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`)});
  52. } else {
  53. 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]}`)});
  54. }
  55. }
  56. 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://gf.qytechs.cn/zh-CN/scripts/412212/feedback', {active: true,insert: true,setParent: true});});
  57. }
  58.  
  59.  
  60. // 切换暗黑模式
  61. function menu_toggle(menu_status, Name) {
  62. menu_status = parseInt(menu_status)
  63. if (menu_status >= 3){
  64. menu_status = 1;
  65. } else {
  66. menu_status += 1;
  67. }
  68. GM_setValue(`${Name}`, menu_status);
  69. location.reload(); // 刷新网页
  70. };
  71.  
  72.  
  73. // 菜单开关
  74. function menu_switch(menu_status, Name, Tips) {
  75. if (menu_status == 'true'){
  76. GM_setValue(`${Name}`, false);
  77. GM_notification({text: `已关闭 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500});
  78. }else{
  79. GM_setValue(`${Name}`, true);
  80. GM_notification({text: `已开启 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500});
  81. }
  82. registerMenuCommand(); // 重新注册(不可用)脚本菜单
  83. };
  84.  
  85.  
  86. // 返回菜单值
  87. function menu_value(menuName) {
  88. for (let menu of menu_ALL) {
  89. if (menu[0] == menuName) {
  90. return menu[3]
  91. }
  92. }
  93. }
  94.  
  95.  
  96. // 添加样式
  97. function addStyle() {
  98. let style_Add = document.createElement('style'),
  99. hours = new Date().getHours(),
  100. style = ``,
  101. style_00 = `html {background-color: #ffffff;}`,
  102. style_11 = `html {filter: brightness(80%) !important;}`,
  103. style_11_firefox = `html {filter: brightness(80%) !important; background-image: url();}`,
  104. style_12 = `html {filter: brightness(70%) !important;}`,
  105. style_12_firefox = `html {filter: brightness(70%) !important; background-image: url();}`,
  106. style_21 = `html {filter: brightness(80%) sepia(20%) !important;}`,
  107. style_21_firefox = `html {filter: brightness(80%) sepia(20%) !important; background-image: url();}`,
  108. style_22 = `html {filter: brightness(70%) sepia(30%) !important;}`,
  109. style_22_firefox = `html {filter: brightness(70%) sepia(30%) !important; background-image: url();}`,
  110. style_31 = `html {filter: invert(80%) !important;} img, video {filter: invert(1) !important;}`,
  111. style_31_firefox = `html {filter: invert(80%) !important;} img, video {filter: invert(1) !important; background-image: url();}`;
  112.  
  113. // Firefox 浏览器需要特殊对待
  114. if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
  115. style_11 = style_11_firefox
  116. style_12 = style_12_firefox
  117. style_21 = style_21_firefox
  118. style_22 = style_22_firefox
  119. style_31 = style_31_firefox
  120. }
  121.  
  122. // 白天(7点到19点)
  123. if (hours > 6 && hours < 19) {
  124. if (menu_value('menu_runDuringTheDay')) {
  125. style_12 = style_11
  126. style_22 = style_21
  127. } else {
  128. style_12 = style_22 = ''
  129. }
  130. }
  131.  
  132. switch(menu_value('menu_darkModeType')) {
  133. case 1:
  134. style += style_12;
  135. break;
  136. case 2:
  137. style += style_22;
  138. break;
  139. case 3:
  140. style += style_31;
  141. break;
  142. }
  143. style_Add.id = 'XIU2DarkMode';
  144. document.lastChild.appendChild(style_Add).textContent = style;
  145.  
  146. // 为了避免 body 还没加载导致无法检查是否设置背景颜色
  147. let timer = setInterval(function(){ // 每 10 毫秒检查一下 body 是否已存在
  148. if (document.body) {
  149. clearInterval(timer); // 取消定时器(每 10 毫秒一次的)
  150. setTimeout(function(){ // 为了避免太快 body 的 CSS 还没加载上,先延迟 100 毫秒(缺点就是可能会出现短暂一闪而过的暗黑滤镜)
  151. console.log('html:', window.getComputedStyle(document.lastChild).backgroundColor, 'body:', window.getComputedStyle(document.body).backgroundColor)
  152. if (window.getComputedStyle(document.body).backgroundColor === 'rgba(0, 0, 0, 0)' && window.getComputedStyle(document.lastChild).backgroundColor === 'rgba(0, 0, 0, 0)') {
  153. // 如果 body 没有 CSS 背景颜色,那就需要添加一个背景颜色,否则影响滤镜效果
  154. document.lastChild.appendChild(document.createElement('style')).textContent = style_00;
  155. } else if (window.getComputedStyle(document.body).backgroundColor === 'rgb(0, 0, 0)' || window.getComputedStyle(document.body).backgroundColor.replace(/rgb|rgba|\(|\)|,| /g, '') < 898989 || window.getComputedStyle(document.lastChild).backgroundColor.replace(/rgb|rgba|\(|\)|,| /g, '') < 898989) {
  156. // 如果是黑色 (等于0,0,0) 或深色 (小于 88,88,88),就停用本脚本滤镜
  157. if (menu_value('menu_autoRecognition')) { // 排除自带暗黑模式的网页 (beta)
  158. console.log('检测到当前网页自带暗黑模式,停用本脚本滤镜...')
  159. document.getElementById('XIU2DarkMode').remove();
  160. }
  161. }
  162. }, 100);
  163. }
  164. }, 10);
  165. }
  166. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址