NoBrighter

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

目前为 2016-03-19 提交的版本。查看 最新版本

  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 20151009.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. // ========== End of config ========== //
  86.  
  87. function isTransparent(color) {
  88. return color === 'transparent' || color.replace(/ /g, '') === 'rgba(0,0,0,0)';
  89. }
  90.  
  91. function changeBgcolor(elem) {
  92. if (elem.nodeType !== Node.ELEMENT_NODE) {
  93. return;
  94. }
  95. var bgcolor = window.getComputedStyle(elem, null).backgroundColor;
  96. if (bgcolor && !isTransparent(bgcolor)) {
  97. var arRGB = bgcolor.match(/\d+/g);
  98. var r = parseInt(arRGB[0], 10);
  99. var g = parseInt(arRGB[1], 10);
  100. var b = parseInt(arRGB[2], 10);
  101.  
  102. // we adopt HSL's lightness definition, see http://en.wikipedia.org/wiki/HSL_and_HSV
  103. var brightness = (Math.max(r, g, b) + Math.min(r, g, b)) / 255 / 2;
  104.  
  105. if (brightness > Brightness_Threshold) {
  106. elem.style.backgroundColor = targetColor;
  107. }
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113.  
  114. function changeTransparent(elem) {
  115. var bgcolor = window.getComputedStyle(elem, null).backgroundColor;
  116. if (!bgcolor || isTransparent(bgcolor)) {
  117. elem.style.backgroundColor = targetColor;
  118. }
  119. }
  120.  
  121. var alltags = document.getElementsByTagName("*");
  122.  
  123. var bodyChanged = false;
  124.  
  125. function changeAll() {
  126. var len = alltags.length;
  127. for (var i = 0; i < len; i++) {
  128. var changed = changeBgcolor(alltags[i]);
  129. var tagName = alltags[i].tagName.toUpperCase();
  130. if (changed && (tagName === "BODY" || tagName === "HTML")) {
  131. bodyChanged = true;
  132. }
  133. }
  134. }
  135. changeAll();
  136.  
  137. if (window.top == window) {
  138. // change transparent only when in top frame
  139. if (!bodyChanged) {
  140. changeTransparent(document.body.parentNode);
  141. }
  142. }
  143.  
  144. for (var i = 0; i < longRunSites.length; i++) {
  145. if (location.hostname === longRunSites[i]) {
  146. console.info('make NoBrighter runs forever...');
  147. setInterval(changeAll, 2000); // convert every 2s
  148. break;
  149. }
  150. }
  151.  
  152. /*document.body.addEventListener('DOMNodeInserted', function(e) {
  153. changeBgcolor(e.target);
  154. }, false);*/