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-27 提交的版本,查看 最新版本

  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.0
  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.  
  19.  
  20. function parseRGB(RGB) {
  21. var rgba = RGB.replace(/[rgba\(\)]/g, '');
  22. var bg = rgba.split(",");
  23. if (bg < 3) return[255, 255, 255];
  24. return [parseInt(bg[0]), parseInt(bg[1]), parseInt(bg[2])];
  25. }
  26.  
  27. function adjust(color, amount) {
  28. return '#' + color.replace(/^#/, '').replace(/../g, color => ('0'+Math.min(255, Math.max(0, parseInt(color, 16) + Math.floor(amount))).toString(16)).substr(-2));
  29. }
  30.  
  31. function realBackgroundColor(elem) {
  32. var transparent = 'rgba(0, 0, 0, 0)';
  33. if (!elem) return transparent;
  34.  
  35. var computed = getComputedStyle(elem);
  36. if (computed.backgroundImage !== "none") {
  37. return undefined;
  38. }
  39.  
  40. var bg = computed.backgroundColor;
  41. if (bg === transparent) {
  42. return realBackgroundColor(elem.parentElement);
  43. } else {
  44. return bg;
  45. }
  46. }
  47.  
  48.  
  49.  
  50. function changeElementsColor() {
  51. var allTags = document.getElementsByTagName("*");
  52. var classes = [];
  53. var colors = []
  54. for (var i = 0; i < allTags.length; i++) {
  55. try{
  56. if (allTags[i].style.backgroundColor == color) {
  57. continue;
  58. }
  59. var rbgcolor = realBackgroundColor(allTags[i]);
  60. var RGB = parseRGB(rbgcolor);
  61. if (RGB[0] > Rth && RGB[1] > Gth && RGB[2] > Bth) {
  62. //console.log(Math.max(-10, (RGB[0] + RGB[1] + RGB[2]) / 3 - 255));
  63. var acolor = adjust(color, Math.max(-10, (RGB[0] + RGB[1] + RGB[2]) / 3 - 255));
  64. allTags[i].style.backgroundColor = acolor;
  65. //console.log(allTags[i].classList.map(x=>`.${x}{background-color: #FDF6E3}\n`))
  66. for (var j = 0; j < allTags[i].classList.length; ++j){
  67. classes.push(`.${allTags[i].classList[j]}{background-color: ${acolor}}\n`);
  68. }
  69. }
  70. }catch(err) {
  71. console.log(err);
  72. }
  73. }
  74. console.log(classes);
  75. //GM_addStyle(classes.join(""));
  76. }
  77.  
  78. function handleAutoPage() {
  79. var bodyHeight = document.body.clientHeight;
  80. var observer = new window.MutationObserver(function(mutations) {
  81. if (mutations[0].addedNodes) {
  82. if (document.body.clientHeight > bodyHeight) {
  83. setTimeout(function() {
  84. changeElementsColor();
  85. }, 200);
  86. bodyHeight = document.body.clientHeight;
  87. }
  88. }
  89. });
  90. observer.observe(document, {
  91. childList: true,
  92. subtree: true
  93. });
  94. }
  95.  
  96.  
  97. (function() {
  98. changeElementsColor();
  99. handleAutoPage();
  100.  
  101. })();
  102.