NoBrighter

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

目前为 2014-06-20 提交的版本。查看 最新版本

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