Greasy Fork镜像 支持简体中文。

护眼模式

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

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

  1. // ==UserScript==
  2. // @name 护眼模式
  3. // @version 1.0.5
  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. // @noframes
  18. // @license GPL-3.0 License
  19. // @run-at document-start
  20. // @namespace https://github.com/XIU2/UserScript
  21. // ==/UserScript==
  22.  
  23. (function() {
  24. var menu_ALL = [
  25. ['menu_runDuringTheDay', '白天保持开启 (比晚上亮一点点)', '白天保持开启', true],
  26. ['menu_darkModeType', '点击切换模式', '点击切换模式', 1]
  27. ], menu_ID = [];
  28. for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
  29. if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
  30. }
  31. registerMenuCommand();
  32. addStyle();
  33.  
  34.  
  35. // 注册(不可用)脚本菜单
  36. function registerMenuCommand() {
  37. if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
  38. for (let i=0;i<menu_ID.length;i++){
  39. GM_unregisterMenuCommand(menu_ID[i]);
  40. }
  41. }
  42. for (let i=0;i<menu_ALL.length;i++){ // 循环注册(不可用)脚本菜单
  43. menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
  44. if (menu_ALL[i][0] === 'menu_darkModeType') {
  45. if (menu_ALL[i][3] > 3){ // 避免在减少 raw 数组后,用户储存的数据大于数组而报错
  46. menu_ALL[i][3] = 1;
  47. GM_setValue('menu_darkModeType', menu_ALL[i][3]);
  48. }
  49. menu_ID[i] = GM_registerMenuCommand(`🔄 [ ${menu_ALL[i][3]} ] ${menu_ALL[i][1]}`, function(){menu_toggle(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`)});
  50. } else {
  51. 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]}`)});
  52. }
  53. }
  54. 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});});
  55. }
  56.  
  57.  
  58. // 切换暗黑模式
  59. function menu_toggle(menu_status, Name) {
  60. menu_status = parseInt(menu_status)
  61. if (menu_status >= 3){
  62. menu_status = 1;
  63. } else {
  64. menu_status += 1;
  65. }
  66. GM_setValue(`${Name}`, menu_status);
  67. location.reload(); // 刷新网页
  68. };
  69.  
  70.  
  71. // 菜单开关
  72. function menu_switch(menu_status, Name, Tips) {
  73. if (menu_status == 'true'){
  74. GM_setValue(`${Name}`, false);
  75. GM_notification({text: `已关闭 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500});
  76. }else{
  77. GM_setValue(`${Name}`, true);
  78. GM_notification({text: `已开启 [${Tips}] 功能\n(刷新网页后生效)`, timeout: 3500});
  79. }
  80. registerMenuCommand(); // 重新注册(不可用)脚本菜单
  81. };
  82.  
  83.  
  84. // 返回菜单值
  85. function menu_value(menuName) {
  86. for (let menu of menu_ALL) {
  87. if (menu[0] == menuName) {
  88. return menu[3]
  89. }
  90. }
  91. }
  92.  
  93.  
  94. // 添加样式
  95. function addStyle() {
  96. //document.documentElement.style.filter = 'brightness(80%) sepia(20%)';
  97. let grayLevel,rgbValueArry,
  98. 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. // 判断网页是否没有设置背景颜色(没有背景颜色会导致滤镜对背景颜色无效)
  114. /*if (document.body) {
  115. console.log(window.getComputedStyle(document.body).backgroundColor)
  116. rgbValueArry = window.getComputedStyle(document.body).backgroundColor.replace ('rgb(', '').replace ('rgba(', '').replace (')', '').split (', ');
  117. grayLevel = rgbValueArry [0] + rgbValueArry [1] + rgbValueArry [2];
  118. if (grayLevel === "000") style += style_00
  119. }*/
  120.  
  121. // Firefox 浏览器需要特殊对待
  122. if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
  123. style_11 = style_11_firefox
  124. style_12 = style_12_firefox
  125. style_21 = style_21_firefox
  126. style_22 = style_22_firefox
  127. style_31 = style_31_firefox
  128. }
  129.  
  130. // 白天(7点到19点)
  131. if (hours > 6 && hours < 19) {
  132. if (menu_value('menu_runDuringTheDay')) {
  133. style_12 = style_11
  134. style_22 = style_21
  135. } else {
  136. style_12 = style_22 = ''
  137. }
  138. }
  139.  
  140. switch(menu_value('menu_darkModeType')) {
  141. case 1:
  142. style += style_12;
  143. break;
  144. case 2:
  145. style += style_22;
  146. break;
  147. case 3:
  148. style += style_31;
  149. break;
  150. }
  151. //style_Add.innerHTML = style;
  152. document.lastChild.appendChild(document.createElement("style")).textContent = style;
  153. /*if (document.head) {
  154. document.head.appendChild(style_Add);
  155. } else { // 为了避免脚本运行的时候 head 还没加载导致报错
  156. let timer = setInterval(function(){
  157. if (document.head) {
  158. document.head.appendChild(style_Add);
  159. clearInterval(timer);
  160. }
  161. }, 1);
  162. }*/
  163.  
  164. // 为了避免 body 还没加载导致无法检查是否设置背景颜色的备用措施
  165. //if (!grayLevel) {
  166. setTimeout(function(){
  167. if (document.body) {
  168. console.log(window.getComputedStyle(document.body).backgroundColor)
  169. let rgbValueArry = window.getComputedStyle(document.body).backgroundColor.replace ('rgb(', '').replace ('rgba(', '').replace (')', '').split (', ');
  170. //style_Add1 = document.createElement('style');
  171. if (rgbValueArry [0] + rgbValueArry [1] + rgbValueArry [2] === "000") {
  172. //style_Add1.innerHTML = style_00;
  173. //document.head.appendChild(style_Add1);
  174. document.lastChild.appendChild(document.createElement("style")).textContent = style_00;
  175. }
  176. }
  177. }, 100);
  178. //}
  179. }
  180. })();

QingJ © 2025

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