护眼模式

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

目前为 2020-07-17 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name eye-protection
  3. // @name:zh-CN 护眼模式
  4. // @noframes true
  5. // @namespace https://github.com/jackdizhu
  6. // @version 0.1.3
  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_registerMenuCommand
  16. // @run-at document-end
  17. // @description 护眼模式,兼容所有网站,自动开启护眼模式,支持自定义颜色
  18. // ==/UserScript==
  19. // 好评记得收藏一下,哈
  20. (function() {
  21. 'use strict';
  22. var dataKey = {
  23. color: 'eye-protection-color'
  24. }
  25. var defColor = 'rgb(204, 232, 207)';
  26. var curColor = defColor;
  27. var $el = document.createElement('div');
  28. $el.style = `
  29. position: fixed;
  30. pointer-events: none;
  31. width: 100%;
  32. height: 100%;
  33. left: 0;
  34. top: 0;
  35. background: ${getDbColor()};
  36. opacity: 0.2;
  37. z-index: 999999999;
  38. `;
  39. // 从数据库取配置数据
  40. function getDbColor () {
  41. var color = GM_getValue(dataKey.color) || defColor;
  42. return color;
  43. }
  44. // 关闭菜单
  45. function closeMenu() {
  46. var oldEditBox = document.querySelector('#eye-protection-setMenu');
  47. if (oldEditBox) {
  48. oldEditBox.parentNode.removeChild(oldEditBox);
  49. }
  50. $el.style.background = curColor
  51. }
  52. // 保存选项
  53. function saveSetting() {
  54. curColor = document.querySelector('#eye-protection-setMenuTextArea').value;
  55. curColor = curColor.replace(/(^\s)|(\s$)/, '');
  56. GM_setValue(dataKey.color, curColor);
  57. closeMenu();
  58. }
  59. // 重置
  60. function reset() {
  61. curColor = defColor
  62. GM_setValue(dataKey.color, curColor);
  63. closeMenu();
  64. }
  65. // 打开菜单
  66. function openMenu() {
  67. var oldEditBox = document.querySelector('#eye-protection-setMenu');
  68. if (oldEditBox) {
  69. oldEditBox.parentNode.removeChild(oldEditBox);
  70. return;
  71. }
  72. var color = getDbColor();
  73. var $dom = document.createElement('div');
  74. $dom.id = 'eye-protection-setMenu';
  75. $dom.style.cssText = `
  76. position: fixed;
  77. top: 100px;
  78. left: 50px;
  79. padding: 10px;
  80. background: #fff;
  81. border-radius: 4px;
  82. `;
  83. GM_addStyle(`
  84. #eye-protection-setMenu {
  85. font-family: Helvetica, 'Hiragino Sans GB', 'Microsoft Yahei', '微软雅黑', Arial, sans-serif;
  86. font-size: 14px;
  87. z-index: 999999999;
  88. border: 1px solid #dedede;
  89. }
  90. #eye-protection-setMenu .button {
  91. padding: 3px 6px;
  92. line-height: 16px;
  93. margin-right: 10px;
  94. display: inline-block;
  95. border: 1px solid #999;
  96. border-radius: 3px;
  97. display: inline-block;
  98. }
  99. #eye-protection-setMenu p {
  100. margin: 0;
  101. }
  102. #eye-protection-setMenu textarea {
  103. border: 1px solid;
  104. padding: 4px;
  105. overflow: auto;
  106. border-radius: 4px;
  107. margin-bottom: 10px;
  108. margin-top: 10px;
  109. }
  110. #eye-protection-setMenu .input-color {
  111. padding-bottom: 10px;
  112. }
  113. #eye-protection-setMenu .input-color span {
  114. display: inline-block;
  115. line-height: 28px;
  116. vertical-align: bottom;
  117. }
  118. `);
  119. var inColor = '#cddc39'
  120. function getHtml (color) {
  121. color = color || curColor
  122. if (/^\#/.test(color)) {
  123. inColor = color
  124. }
  125. return `
  126. <p>护眼模式自定义颜色,如:rgb(204, 232, 207) 或者 #cddc39</P>
  127. <textarea id="eye-protection-setMenuTextArea" wrap='off' cols='45' rows='5' value="${color}">${color}</textarea>
  128. <p class="input-color">
  129. <input type="color" id="eye-protection-color-input" value="${inColor}">
  130. <span>${inColor}</span>
  131. </p>
  132. <p>
  133. <span class="button" id='eye-protection-setMenuSave'>保存</span>
  134. <span class="button" id='eye-protection-setMenureset'>重置</span>
  135. <span class="button" id='eye-protection-setMenuClose' title='如果无法关闭 请刷新界面'>关闭</span>
  136. </p>
  137. `;
  138. }
  139. var innerH = getHtml()
  140. function colorChange (e) {
  141. inColor = e.target.value
  142. $dom.innerHTML = getHtml(inColor);
  143. }
  144. $dom.innerHTML = innerH;
  145. document.body.appendChild($dom);
  146.  
  147. function eventFn (event, fn, id) {
  148. if (event.target.id === id) {
  149. fn(event)
  150. }
  151. }
  152.  
  153. $dom.addEventListener('click', function (event) {
  154. eventFn(event, saveSetting, 'eye-protection-setMenuSave')
  155. }, false);
  156. $dom.addEventListener('click', function (event) {
  157. eventFn(event, reset, 'eye-protection-setMenureset')
  158. }, false);
  159. $dom.addEventListener('click', function (event) {
  160. eventFn(event, closeMenu, 'eye-protection-setMenuClose')
  161. }, false);
  162. $dom.addEventListener('change', function (event) {
  163. eventFn(event, colorChange, 'eye-protection-color-input')
  164. }, false);
  165. }
  166. GM_registerMenuCommand('自定义颜色', openMenu); // 设置油猴插件的菜单
  167.  
  168. document.body.appendChild($el)
  169. })();

QingJ © 2025

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