grayBackgroundColor

将网页背景色改为护眼灰

  1. // ==UserScript==
  2. // @name grayBackgroundColor
  3. // @namespace https://github.com/sakuyaa/gm_scripts
  4. // @author sakuyaa
  5. // @description 将网页背景色改为护眼灰
  6. // @include *
  7. // @inject-into auto
  8. // @version 2024.4.1
  9. // @compatible firefox 74
  10. // @grant GM_addStyle
  11. // @note 配合browser.display.background_color;#DCDCDC使用
  12. // @run-at document-end
  13. // ==/UserScript==
  14. (function() {
  15. let grayValue = 225;
  16. let sleep = time => {
  17. return new Promise(resolve => setTimeout(resolve, time));
  18. };
  19. let grayElem = async elem => { //将元素变灰
  20. await sleep(0);
  21. let rgbaValues = window.getComputedStyle(elem)?.getPropertyValue('background-color')?.match(/\d+(\.\d+)?/g);
  22. if (rgbaValues) {
  23. let [red, green, blue, alpha] = rgbaValues;
  24. if (red <= grayValue || green <= grayValue || blue <= grayValue || alpha == 0) {
  25. return;
  26. }
  27. //从225-255压缩到215-225
  28. elem.style.setProperty('background-color', (alpha ? 'rgba(' : 'rgb(') +
  29. Math.floor((red - grayValue) / 3 + grayValue - 10) + ', ' +
  30. Math.floor((green - grayValue) / 3 + grayValue - 10) + ', ' +
  31. Math.floor((blue - grayValue) / 3 + grayValue - 10) +
  32. (alpha ? (', ' + alpha + ')') : ')'), 'important');
  33. }
  34. }
  35.  
  36. let grayBackgroundColor = async () => {
  37. await sleep(0);
  38. for (let elem of document.getElementsByTagName('*')) {
  39. grayElem(elem);
  40. }
  41. }
  42.  
  43. let fixNotGray = () => { //去除一些背景为空白图的网站
  44. switch (window.location.hostname) {
  45. case 'www.w3school.com.cn':
  46. GM_addStyle('#wrapper {background: #dcdcdc none !important;}');
  47. return;
  48. }
  49. let herf = window.location.href;
  50. if (/^https?:\/\/tieba\.baidu\.com\/f.+/i.test(herf)) {
  51. GM_addStyle('.forum_content {background: #dcdcdc none !important;}');
  52. }
  53. }
  54.  
  55. grayBackgroundColor();
  56. let count = 0;
  57. let intervalId = setInterval(() => {
  58. for (let elem of document.getElementsByTagName('*')) {
  59. if (++count > 9) {
  60. break; //次数过多也不再循环处理
  61. }
  62. let rgbaValues = window.getComputedStyle(elem)?.getPropertyValue('background-color')?.match(/\d+(\.\d+)?/g);
  63. if (rgbaValues) {
  64. let [red, green, blue, alpha] = rgbaValues;
  65. if (red > grayValue && green > grayValue && blue > grayValue && alpha != 0) {
  66. grayBackgroundColor();
  67. return; //存在需要处理的元素,则处理后继续循环
  68. }
  69. }
  70. }
  71. clearInterval(intervalId); //没有需要处理的元素,则不再循环处理
  72. }, 1234);
  73. (new MutationObserver(async mutations => {
  74. for (let mutation of mutations) {
  75. for (let elem of mutation.addedNodes) {
  76. if (elem.nodeType == 1) { //元素节点
  77. grayElem(elem);
  78. for (let childNode of elem.getElementsByTagName('*')) { //遍历所有子节点
  79. grayElem(childNode);
  80. }
  81. }
  82. }
  83. }
  84. })).observe(document.body, {
  85. childList: true,
  86. subtree: true
  87. });
  88. fixNotGray();
  89. })();

QingJ © 2025

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