护眼模式

护眼模式,兼容所有网站,自动开启护眼模式,支持自定义颜色

  1. // ==UserScript==
  2. // @name eye-protection
  3. // @name:zh-CN 护眼模式
  4. // @noframes true
  5. // @namespace https://github.com/jackdizhu
  6. // @version 0.2.11
  7. // @description:zh-CN 护眼模式,兼容所有网站,自动开启护眼模式,支持自定义颜色
  8. // @description:en Eye protection mode, compatible with all websites, automatically open eye protection mode, support custom colors
  9. // @author jackdizhu
  10. // @match *
  11. // @include *
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_addStyle
  15. // @grant GM_info
  16. // @grant GM_registerMenuCommand
  17. // @run-at document-idle
  18. // @description 护眼模式,兼容所有网站,自动开启护眼模式,支持自定义颜色
  19. // ==/UserScript==
  20. // 好评记得收藏一下,哈 If you think this script is good, please bookmark it, thanks!
  21. (function() {
  22. 'use strict';
  23. function getLang (key) {
  24. var $lang = navigator.language.toLowerCase().indexOf('zh') !== -1 ? 'zh' : 'en'
  25. var langObj = {
  26. 'tips-zh': '护眼模式自定义颜色,如:rgb(204, 232, 207) 或者 #cddc39',
  27. 'tips-en': 'Custom color for eye-protection, such as rgb(204, 232, 207) or #cddc39',
  28. 'btn-save-zh': '保存',
  29. 'btn-save-en': 'save',
  30. 'btn-reset-zh': '重置',
  31. 'btn-reset-en': 'reset',
  32. 'btn-close-zh': '关闭',
  33. 'btn-close-en': 'close',
  34. 'btn-close-tips-zh': '如果无法关闭 请刷新界面',
  35. 'btn-close-tips-en': 'If you cannot close, please refresh the interface',
  36. 'btn-add-whitelist-zh': '添加白名单',
  37. 'btn-add-whitelist-en': 'add whitelist',
  38. 'btn-remove-whitelist-zh': '移出白名单',
  39. 'btn-remove-whitelist-en': 'remove whitelist',
  40. 'btn-menu-open-zh': '自定义颜色',
  41. 'btn-menu-open-en': 'custom color',
  42. }
  43. return langObj[key + '-' + $lang] || ''
  44. }
  45. var dataKey = {
  46. color: 'eye-protection-color',
  47. whitelist: 'eye-protection-whitelist'
  48. }
  49. var defColor = 'rgb(204, 232, 207)';
  50. var curColor = defColor;
  51. var $el = document.createElement('div');
  52. var inWhitelist = false
  53. $el.style = `
  54. position: fixed;
  55. pointer-events: none;
  56. width: 100%;
  57. height: 100%;
  58. left: 0;
  59. top: 0;
  60. background: ${getDbColor()};
  61. opacity: 0.2;
  62. z-index: 999999999;
  63. `;
  64. // 从数据库取配置数据
  65. function getDbColor () {
  66. var color = GM_getValue(dataKey.color) || defColor;
  67. return color;
  68. }
  69. function getWhitelist () {
  70. var whitelist = GM_getValue(dataKey.whitelist) || '';
  71. return whitelist;
  72. }
  73. function saveWhitelist (data) {
  74. GM_setValue(dataKey.whitelist, data);
  75. }
  76. // 关闭菜单
  77. function closeMenu() {
  78. var oldEditBox = document.querySelector('#eye-protection-setMenu');
  79. if (oldEditBox) {
  80. oldEditBox.parentNode.removeChild(oldEditBox);
  81. }
  82. $el.style.background = curColor
  83. }
  84. // 保存选项
  85. function saveSetting() {
  86. curColor = document.querySelector('#eye-protection-setMenuTextArea').value;
  87. curColor = curColor.replace(/(^\s)|(\s$)/, '');
  88. GM_setValue(dataKey.color, curColor);
  89. closeMenu();
  90. }
  91. // 重置
  92. function reset() {
  93. curColor = defColor
  94. GM_setValue(dataKey.color, curColor);
  95. closeMenu();
  96. }
  97. // 打开菜单
  98. function openMenu() {
  99. var oldEditBox = document.querySelector('#eye-protection-setMenu');
  100. if (oldEditBox) {
  101. oldEditBox.parentNode.removeChild(oldEditBox);
  102. return;
  103. }
  104. var color = getDbColor();
  105. var $dom = document.createElement('div');
  106. $dom.id = 'eye-protection-setMenu';
  107. $dom.style.cssText = `
  108. position: fixed;
  109. top: 100px;
  110. left: 50px;
  111. padding: 10px;
  112. background: #fff;
  113. border-radius: 4px;
  114. `;
  115. GM_addStyle(`
  116. #eye-protection-setMenu {
  117. font-family: Helvetica, 'Hiragino Sans GB', 'Microsoft Yahei', '微软雅黑', Arial, sans-serif;
  118. font-size: 14px;
  119. z-index: 999999999;
  120. border: 1px solid #dedede;
  121. }
  122. #eye-protection-setMenu .button {
  123. padding: 3px 6px;
  124. line-height: 16px;
  125. margin-right: 10px;
  126. display: inline-block;
  127. border: 1px solid #999;
  128. border-radius: 3px;
  129. display: inline-block;
  130. cursor: pointer;
  131. }
  132. #eye-protection-setMenu p {
  133. margin: 0;
  134. }
  135. #eye-protection-setMenu p + p {
  136. margin-top: 10px;
  137. }
  138. #eye-protection-setMenu textarea {
  139. border: 1px solid;
  140. padding: 4px;
  141. overflow: auto;
  142. border-radius: 4px;
  143. margin-bottom: 10px;
  144. margin-top: 10px;
  145. }
  146. #eye-protection-setMenu .input-color span {
  147. display: inline-block;
  148. line-height: 28px;
  149. vertical-align: bottom;
  150. }
  151. `);
  152. var inColor = '#cddc39'
  153. function getHtml (color) {
  154. color = color || curColor
  155. if (/^\#/.test(color)) {
  156. inColor = color
  157. }
  158. return `
  159. <p>${getLang('tips')}</P>
  160. <textarea id="eye-protection-setMenuTextArea" wrap='off' cols='45' rows='5' value="${color}">${color}</textarea>
  161. <p class="input-color">
  162. <input type="color" id="eye-protection-color-input" value="${inColor}">
  163. <span>${inColor}</span>
  164. </p>
  165. <p>
  166. <span class="button" id='eye-protection-setMenuSave'>${getLang('btn-save')}</span>
  167. <span class="button" id='eye-protection-setMenureset'>${getLang('btn-reset')}</span>
  168. <span class="button" id='eye-protection-setMenuClose' title='${getLang('btn-close-tips')}'>${getLang('btn-close')}</span>
  169. </p>
  170. <p>
  171. ${inWhitelist ?
  172. '<span class="button" id="eye-protection-removeWhitelist">' + getLang('btn-remove-whitelist') + '</span>' :
  173. '<span class="button" id="eye-protection-addWhitelist">' + getLang('btn-add-whitelist') + '</span>'
  174. }
  175. </p>
  176. <!--<p>
  177. <input type="text" id="eye-protection-customInput"/>
  178. <span class="button" id='eye-protection-customWhitelist'>自定义白名单</span>
  179. </p>-->
  180. `;
  181. }
  182. var innerH = getHtml()
  183. function colorChange (e) {
  184. inColor = e.target.value
  185. $dom.innerHTML = getHtml(inColor);
  186. }
  187. $dom.innerHTML = innerH;
  188. document.body.appendChild($dom);
  189.  
  190. function eventFn (event, fn, id) {
  191. if (event.target.id === id) {
  192. fn(event)
  193. }
  194. }
  195. function addWhitelist (domain) {
  196. var $whitelist = getWhitelist()
  197. var str = ',' + domain
  198. if ($whitelist.indexOf(str) === -1) {
  199. inWhitelist = true
  200. saveWhitelist($whitelist + str)
  201. }
  202. init()
  203. closeMenu()
  204. }
  205. function removeWhitelist (domain) {
  206. var $whitelist = getWhitelist()
  207. var str = ',' + domain
  208. if ($whitelist.indexOf(str) !== -1) {
  209. inWhitelist = false
  210. saveWhitelist($whitelist.replace(str, ''))
  211. }
  212. init()
  213. closeMenu()
  214. }
  215.  
  216. $dom.addEventListener('click', function (event) {
  217. eventFn(event, saveSetting, 'eye-protection-setMenuSave')
  218. }, false);
  219. $dom.addEventListener('click', function (event) {
  220. eventFn(event, reset, 'eye-protection-setMenureset')
  221. }, false);
  222. $dom.addEventListener('click', function (event) {
  223. eventFn(event, closeMenu, 'eye-protection-setMenuClose')
  224. }, false);
  225. $dom.addEventListener('change', function (event) {
  226. eventFn(event, colorChange, 'eye-protection-color-input')
  227. }, false);
  228. // 白名单
  229. $dom.addEventListener('click', function (event) {
  230. eventFn(event, function () {
  231. addWhitelist(document.domain)
  232. }, 'eye-protection-addWhitelist')
  233. }, false);
  234. $dom.addEventListener('click', function (event) {
  235. eventFn(event, function () {
  236. removeWhitelist(document.domain)
  237. }, 'eye-protection-removeWhitelist')
  238. }, false);
  239. }
  240. GM_registerMenuCommand(getLang('btn-menu-open'), openMenu); // 设置油猴插件的菜单
  241.  
  242. function init () {
  243. var $whitelist = getWhitelist()
  244. var domain = document.domain
  245. var tld = '|.com|.xyz|.net|.top|.tech|.org|.gov|.edu|.ink|.int|.mil|.pub|.cn|.com.cn|.net.cn|.gov.cn|.org.cn|.red|.ink|.biz|.cc|.tv|.info|.name|.pro|.museum|.coop|.aero|.mobi|.travel'
  246. var arr = domain.split('.')
  247.  
  248. var $tld = ''
  249. var name = ''
  250.  
  251. for (let i = arr.length; i > 0; i--) {
  252. let item = arr[i];
  253. let _tld = $tld
  254. if (_tld) {
  255. _tld = item + '.' + _tld
  256. } else {
  257. _tld = '.' + item
  258. }
  259. if (tld.indexOf('|' + _tld) !== -1) {
  260. $tld = _tld
  261. } else {
  262. name = arr[i - 1]
  263. break
  264. }
  265. }
  266. if ($whitelist.indexOf(',' + domain) !== -1 || $whitelist.indexOf(',*.' + name + $tld) !== -1) {
  267. inWhitelist = true
  268. $el.style.display = 'none'
  269. return false
  270. } else {
  271. inWhitelist = false
  272. $el.style.display = 'block'
  273. }
  274. document.body.appendChild($el)
  275. }
  276. init()
  277. })();

QingJ © 2025

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