NoBrighter

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

目前为 2014-05-15 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name NoBrighter
  3. // @namespace https://github.com/henix/userjs/NoBrighter.js
  4. // @description Change element's background color that is too bright to a light green.
  5. // @author henix
  6. // @version 20140512-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.  
  69. 'twitter.com',
  70. 'github.com',
  71.  
  72. 'www.coursera.org',
  73. 'class.coursera.org',
  74.  
  75. 'weibo.com',
  76. 'www.weibo.com',
  77. 'www.renren.com',
  78.  
  79. 'feedly.com',
  80. 'reader.aol.com',
  81. ];
  82.  
  83. // ========== End of config ========== //
  84.  
  85. /**
  86. * String -> Bool
  87. */
  88. function isTransparent(color) {
  89. return color === 'transparent' || color.replace(/ /g, '') === 'rgba(0,0,0,0)';
  90. }
  91.  
  92. function changeBgcolor(elem) {
  93. if (elem.nodeType !== Node.ELEMENT_NODE) {
  94. return;
  95. }
  96. var bgcolor = window.getComputedStyle(elem, null).backgroundColor;
  97. if (bgcolor && !isTransparent(bgcolor)) {
  98. var arRGB = bgcolor.match(/\d+/g);
  99. var r = parseInt(arRGB[0], 10);
  100. var g = parseInt(arRGB[1], 10);
  101. var b = parseInt(arRGB[2], 10);
  102.  
  103. // we adopt HSL's lightness definition, see http://en.wikipedia.org/wiki/HSL_and_HSV
  104. var brightness = (Math.max(r, g, b) + Math.min(r, g, b)) / 255 / 2;
  105.  
  106. if (brightness > Brightness_Threshold) {
  107. elem.style.backgroundColor = targetColor;
  108. }
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. }
  114.  
  115. function changeTransparent(elem) {
  116. var bgcolor = window.getComputedStyle(elem, null).backgroundColor;
  117. if (!bgcolor || isTransparent(bgcolor)) {
  118. elem.style.backgroundColor = targetColor;
  119. }
  120. }
  121.  
  122. var alltags = document.getElementsByTagName("*");
  123.  
  124. var bodyChanged = false;
  125.  
  126. function changeAll() {
  127. var len = alltags.length;
  128. for (var i = 0; i < len; i++) {
  129. var changed = changeBgcolor(alltags[i]);
  130. var tagName = alltags[i].tagName.toUpperCase();
  131. if (changed && (tagName === "BODY" || tagName === "HTML")) {
  132. bodyChanged = true;
  133. }
  134. }
  135. }
  136. changeAll();
  137.  
  138. if (window.top == window) {
  139. // change transparent only when in top frame
  140. if (!bodyChanged) {
  141. changeTransparent(document.body.parentNode);
  142. }
  143. }
  144.  
  145. for (var i = 0; i < longRunSites.length; i++) {
  146. if (location.hostname === longRunSites[i]) {
  147. console.info('make NoBrighter runs forever...');
  148. setInterval(changeAll, 2000); // convert every 2s
  149. break;
  150. }
  151. }
  152.  
  153. /*document.body.addEventListener('DOMNodeInserted', function(e) {
  154. changeBgcolor(e.target);
  155. }, false);*/