Browser Background Color Controller

Change the background color in all web pages to protect your eyes. we map different original color to different new colors automatically to make it looks better than others which only replace the original color with only one color.

当前为 2022-06-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Browser Background Color Controller
  3. // @namespace Background Color
  4. // @description Change the background color in all web pages to protect your eyes. we map different original color to different new colors automatically to make it looks better than others which only replace the original color with only one color.
  5. // @version 2.3
  6. // @include http*
  7. // @include ftp
  8. // @exclude
  9. // @license MIT
  10. // @grant GM_addStyle
  11. // ==/UserScript==
  12.  
  13. // Threshold
  14. var Rth = 230;
  15. var Gth = 230;
  16. var Bth = 230;
  17. var color = "#FDF6E3" //New color
  18. var renderClasses = ["nH"];
  19.  
  20.  
  21. function parseRGB(RGB) {
  22. if (RGB == undefined) return[0, 0, 0, 0];
  23. var rgba = RGB.replace(/[rgba\(\)]/g, '');
  24. var bg = rgba.split(",");
  25. if (bg < 3) return[0, 0, 0, 0];
  26. return [parseInt(bg[0]), parseInt(bg[1]), parseInt(bg[2]), parseInt(bg[3])];
  27. }
  28.  
  29. function adjust(color, amount) {
  30. return '#' + color.replace(/^#/, '').replace(/../g, color => ('0'+Math.min(255, Math.max(0, parseInt(color, 16) + Math.floor(amount))).toString(16)).substr(-2));
  31. }
  32.  
  33. function realBackgroundColor(elem) {
  34. var transparent = 'rgba(0, 0, 0, 0)';
  35. if (!elem) return transparent;
  36.  
  37. var computed = getComputedStyle(elem);
  38. if (computed.backgroundImage !== "none") {
  39. return undefined;
  40. }
  41.  
  42. var bg = computed.backgroundColor;
  43. if (bg === transparent) {
  44. return realBackgroundColor(elem.parentElement);
  45. } else {
  46. return bg;
  47. }
  48. }
  49.  
  50.  
  51.  
  52. function changeElementsColor() {
  53. var allTags = document.getElementsByTagName("*");
  54. var classes = [];
  55. var colors = []
  56. var hover = [].slice.call(document.querySelectorAll( ":hover" ));
  57. for (var i = 0; i < allTags.length; i++) {
  58. try{
  59. if (allTags[i].style.backgroundColor == color || allTags[i].style.bgColorAlreadySet == true) {
  60. continue;
  61. }
  62. allTags[i].style.bgColorAlreadySet = true;
  63. var rbgcolor = realBackgroundColor(allTags[i]);
  64. var RGB = parseRGB(rbgcolor);
  65. if (RGB[0] > Rth && RGB[1] > Gth && RGB[2] > Bth || allTags[i].tagName == "BODY") {
  66. //console.log(Math.max(-10, (RGB[0] + RGB[1] + RGB[2]) / 3 - 255));
  67. var acolor = adjust(color, Math.max(-10, (RGB[0] + RGB[1] + RGB[2]) / 3 - 255));
  68. allTags[i].style.backgroundColor = acolor;
  69. //console.log(allTags[i].classList.map(x=>`.${x}{background-color: #FDF6E3}\n`))
  70. //for (var j = 0; j < allTags[i].classList.length; ++j){
  71. // classes.push(`.${allTags[i].classList[j]}{background-color: ${acolor}}\n`);
  72. //}
  73. }
  74. }catch(err) {
  75. console.log(err);
  76. }
  77. }
  78. //console.log(classes);
  79. //GM_addStyle(classes.join(""));
  80. //GM_addStyle(renderClasses.map(x=>`.${x}{background-color: ${color}}\n`).join(""));
  81. }
  82.  
  83. function handleAutoPage() {
  84. var observer = new window.MutationObserver(function(mutations) {
  85. var currentLen = document.getElementsByTagName("*").length;
  86. if (mutations[0].addedNodes) {
  87. setTimeout(function() {
  88. if(document.getElementsByTagName("*").length == currentLen){
  89. console.log(document.getElementsByTagName("*").length)
  90. changeElementsColor();
  91. }
  92. }, 50);
  93. }
  94. });
  95. observer.observe(document, {
  96. childList: true,
  97. subtree: true
  98. });
  99. }
  100.  
  101.  
  102. (function() {
  103. changeElementsColor();
  104. handleAutoPage();
  105.  
  106. })();
  107.  

QingJ © 2025

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