NoBrighter

Change element's background color that is too bright to a light green.

  1. // ==UserScript==
  2. // @name NoBrighter
  3. // @namespace https://github.com/henix/userjs/NoBrighter
  4. // @description Change element's background color that is too bright to a light green.
  5. // @author henix
  6. // @version 20160608.1
  7. // @include http://*
  8. // @include https://*
  9. // @exclude http://boards.4chan.org/*
  10. // @exclude https://boards.4chan.org/*
  11. // @license MIT License
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. /**
  16. * ChangeLog:
  17. *
  18. * see https://github.com/henix/userjs/commits/master/NoBrighter.js
  19. *
  20. * 2013-12-4 henix
  21. * changeTransparent should be called on <html> tag, because it can set background-color. fix #1
  22. * Provided other colors, you can uncomment them to use. The number after them is brightness.
  23. *
  24. * 2013-6-17 henix
  25. * The latest version of TamperMonkey don't support "*", change to "http://*" and "https://*"
  26. *
  27. * 2012-8-16 henix
  28. * Change transparent body only when in top frame.
  29. *
  30. * There could be a transparent iframe in a dark parent frame, in which case the old logic will do wrong.
  31. *
  32. * 2012-7-19 henix
  33. * Remove prependSheet because it may clash with <body bgcolor="XX">
  34. *
  35. * 2012-7-15 henix
  36. * Exclude boards.4chan.org
  37. *
  38. * Because users could choose their own style from 4chan which loads after NoBrighter
  39. *
  40. * 2012-7-14 henix
  41. * Add changeTransparent()
  42. *
  43. * 2012-7-14 henix
  44. * Use css stylesheet to set body's default background-color
  45. *
  46. * 2012-7-12 henix
  47. * Version 0.1
  48. */
  49.  
  50. // ========== Config ========== //
  51.  
  52. // Uncomment to use, number after is its brightness
  53.  
  54. /* Green */
  55. // var targetColor = '#C7EDCC'; // 93
  56. var targetColor = '#C1E6C6'; // 90
  57.  
  58. /* Wheat */
  59. // var targetColor = '#E6D6B8'; // 90
  60. // var targetColor = '#E3E1D1'; // 89
  61.  
  62. var Brightness_Threshold = 0.94; // a number between 0 and 1
  63.  
  64. // For websites updating their contents via ajax, NoBrighter can run in background and convert background color periodically.
  65. var longRunSites = [
  66. 'mail.google.com',
  67. 'docs.google.com',
  68. 'plus.google.com',
  69. 'groups.google.com',
  70.  
  71. 'twitter.com',
  72. 'github.com',
  73.  
  74. 'www.coursera.org',
  75. 'class.coursera.org',
  76.  
  77. 'weibo.com',
  78. 'www.weibo.com',
  79. 'www.renren.com',
  80.  
  81. 'feedly.com',
  82. 'reader.aol.com',
  83. ];
  84.  
  85. var $minHeight = 6;
  86.  
  87. // ========== End of config ========== //
  88.  
  89. function isTransparent(color) {
  90. return color === 'transparent' || color.replace(/ /g, '') === 'rgba(0,0,0,0)';
  91. }
  92.  
  93. function changeBgcolor(elem) {
  94. if (elem.nodeType !== Node.ELEMENT_NODE) {
  95. return;
  96. }
  97. var bgcolor = window.getComputedStyle(elem, null).backgroundColor;
  98. if (bgcolor && !isTransparent(bgcolor) && elem.clientHeight >= $minHeight) {
  99. var arRGB = bgcolor.match(/\d+/g);
  100. var r = parseInt(arRGB[0], 10);
  101. var g = parseInt(arRGB[1], 10);
  102. var b = parseInt(arRGB[2], 10);
  103.  
  104. // we adopt HSL's lightness definition, see http://en.wikipedia.org/wiki/HSL_and_HSV
  105. var brightness = (Math.max(r, g, b) + Math.min(r, g, b)) / 255 / 2;
  106.  
  107. if (brightness > Brightness_Threshold) {
  108. elem.style.backgroundColor = targetColor;
  109. }
  110. return true;
  111. } else {
  112. return false;
  113. }
  114. }
  115.  
  116. function changeTransparent(elem) {
  117. var bgcolor = window.getComputedStyle(elem, null).backgroundColor;
  118. if (!bgcolor || isTransparent(bgcolor)) {
  119. elem.style.backgroundColor = targetColor;
  120. }
  121. }
  122.  
  123. var alltags = document.getElementsByTagName("*");
  124.  
  125. var bodyChanged = false;
  126.  
  127. function changeAll() {
  128. var len = alltags.length;
  129. for (var i = 0; i < len; i++) {
  130. var changed = changeBgcolor(alltags[i]);
  131. var tagName = alltags[i].tagName.toUpperCase();
  132. if (changed && (tagName === "BODY" || tagName === "HTML")) {
  133. bodyChanged = true;
  134. }
  135. }
  136. }
  137. changeAll();
  138.  
  139. if (window.top == window) {
  140. // change transparent only when in top frame
  141. if (!bodyChanged) {
  142. changeTransparent(document.body.parentNode);
  143. }
  144. }
  145.  
  146. for (var i = 0; i < longRunSites.length; i++) {
  147. if (location.hostname === longRunSites[i]) {
  148. console.info('make NoBrighter runs forever...');
  149. setInterval(changeAll, 2000); // convert every 2s
  150. break;
  151. }
  152. }
  153.  
  154. /*document.body.addEventListener('DOMNodeInserted', function(e) {
  155. changeBgcolor(e.target);
  156. }, false);*/

QingJ © 2025

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