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.

当前为 2023-05-20 提交的版本,查看 最新版本

  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.5
  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 rgbToHex(rgb) {
  34. function componentToHex(c) {
  35. var hex = parseInt(c).toString(16);
  36. return hex.length == 1 ? "0" + hex : hex;
  37. }
  38. rgb = rgb.replace(/[^\d,]/g, '').split(',');
  39. return "#" + componentToHex(rgb[0]) + componentToHex(rgb[1]) + componentToHex(rgb[2]);
  40. }
  41.  
  42. function hexToRgb(hex) {
  43. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  44. return result ? [
  45. parseInt(result[1], 16),
  46. parseInt(result[2], 16),
  47. parseInt(result[3], 16)
  48. ] : null;
  49. }
  50.  
  51.  
  52. function realBackgroundColor(elem) {
  53. var transparent = 'rgba(0, 0, 0, 0)';
  54. if (!elem) return transparent;
  55.  
  56. var computed = getComputedStyle(elem);
  57. if (computed.backgroundImage !== "none") {
  58. return undefined;
  59. }
  60.  
  61. var bg = computed.backgroundColor;
  62. if (bg === transparent) {
  63. return realBackgroundColor(elem.parentElement);
  64. } else {
  65. return bg;
  66. }
  67. }
  68.  
  69.  
  70.  
  71. function changeElementsColor() {
  72. var allTags = document.getElementsByTagName("*");
  73. var classes = [];
  74. var colors = []
  75. var hover = [].slice.call(document.querySelectorAll( ":hover" ));
  76. for (var i = 0; i < allTags.length; i++) {
  77. try{
  78. var txtcolor = window.getComputedStyle(allTags[i], null).getPropertyValue('color').replace(/[^\d,]/g, '').split(',');
  79. var should_change_txt = (txtcolor[0] > 230 && txtcolor[1] > 230 && txtcolor[2] > 230);
  80. if ((allTags[i].style.backgroundColor == color || allTags[i].style.bgColorAlreadySet == true) && !should_change_txt) {
  81. continue;
  82. }
  83. allTags[i].style.bgColorAlreadySet = true;
  84. var rbgcolor = realBackgroundColor(allTags[i]);
  85. var RGB = parseRGB(rbgcolor);
  86. var acolor = adjust(color, Math.max(-10, (RGB[0] + RGB[1] + RGB[2]) / 3 - 255));
  87. var set_color_this_time = false;
  88. if (RGB[0] > Rth && RGB[1] > Gth && RGB[2] > Bth || allTags[i].tagName == "BODY") {
  89. //if (RGB[0] + RGB[1] + RGB[2] > Rth * 3 || allTags[i].tagName == "BODY") {
  90. allTags[i].style.backgroundColor = acolor;
  91. set_color_this_time = true;
  92. //if (allTags[i].innerText != undefined && allTags[i].innerText.startsWith("Start a new repository\n\nA"))
  93. // console.log(allTags[i], allTags[i].style.backgroundColor, acolor)
  94. //console.log(Math.max(-10, (RGB[0] + RGB[1] + RGB[2]) / 3 - 255));
  95. //console.log(allTags[i].classList.map(x=>`.${x}{background-color: #FDF6E3}\n`))
  96. //for (var j = 0; j < allTags[i].classList.length; ++j){
  97. // classes.push(`.${allTags[i].classList[j]}{background-color: ${acolor}}\n`);
  98. //}
  99. //console.log( allTags[i]);
  100. //if (txtcolor[0] > 230 && txtcolor[1] > 230 && txtcolor[2] > 230){
  101. // allTags[i].style.color = "#073642";
  102. //}
  103. }
  104. var aRGB = hexToRgb(acolor);
  105. var margin = 12
  106. if ((Math.abs(RGB[0] - aRGB[0]) < margin && Math.abs(RGB[1] - aRGB[1]) < margin && Math.abs(RGB[2] - aRGB[2]) < margin || set_color_this_time) && should_change_txt){
  107. allTags[i].style.color = "#073642";
  108. }
  109. }catch(err) {
  110. console.log(err);
  111. }
  112. }
  113. //console.log(classes);
  114. //GM_addStyle(classes.join(""));
  115. //GM_addStyle(renderClasses.map(x=>`.${x}{background-color: ${color}}\n`).join(""));
  116. }
  117.  
  118. function handleAutoPage() {
  119. var observer = new window.MutationObserver(function(mutations) {
  120. var currentLen = document.getElementsByTagName("*").length;
  121. if (mutations[0].addedNodes) {
  122. setTimeout(function() {
  123. if(document.getElementsByTagName("*").length == currentLen){
  124. //console.log(document.getElementsByTagName("*").length)
  125. changeElementsColor();
  126. }
  127. }, 50);
  128. }
  129. });
  130. observer.observe(document, {
  131. childList: true,
  132. subtree: true
  133. });
  134. }
  135.  
  136.  
  137. (function() {
  138. changeElementsColor();
  139. handleAutoPage();
  140.  
  141. })();
  142.  

QingJ © 2025

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