NoBrighter

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

目前为 2014-12-17 提交的版本。查看 最新版本

  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 20141217.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'; // 90
  53. /* Wheat */
  54. // var targetColor = '#E6D6B8'; // 90
  55. // var targetColor = '#E3E1D1'; // 89
  56. var Brightness_Threshold = 0.94; // a number between 0 and 1
  57. // For websites updating their contents via ajax, NoBrighter can run in background and convert background color periodically.
  58. var longRunSites = [
  59. 'mail.google.com',
  60. 'docs.google.com',
  61. 'plus.google.com',
  62. 'twitter.com',
  63. 'github.com',
  64. 'www.coursera.org',
  65. 'class.coursera.org',
  66. 'weibo.com',
  67. 'www.weibo.com',
  68. 'www.renren.com',
  69. 'feedly.com',
  70. 'reader.aol.com',
  71. ];
  72. // ========== End of config ========== //
  73. function isTransparent(color) {
  74. return color === 'transparent' || color.replace(/ /g, '') === 'rgba(0,0,0,0)';
  75. }
  76. function changeBgcolor(elem) {
  77. if (elem.nodeType !== Node.ELEMENT_NODE) {
  78. return;
  79. }
  80. var bgcolor = window.getComputedStyle(elem, null).backgroundColor;
  81. if (bgcolor && !isTransparent(bgcolor)) {
  82. var arRGB = bgcolor.match(/\d+/g);
  83. var r = parseInt(arRGB[0], 10);
  84. var g = parseInt(arRGB[1], 10);
  85. var b = parseInt(arRGB[2], 10);
  86. // we adopt HSL's lightness definition, see http://en.wikipedia.org/wiki/HSL_and_HSV
  87. var brightness = (Math.max(r, g, b) + Math.min(r, g, b)) / 255 / 2;
  88. if (brightness > Brightness_Threshold) {
  89. elem.style.backgroundColor = targetColor;
  90. }
  91. return true;
  92. }
  93. else {
  94. return false;
  95. }
  96. }
  97. function changeTransparent(elem) {
  98. var bgcolor = window.getComputedStyle(elem, null).backgroundColor;
  99. if (!bgcolor || isTransparent(bgcolor)) {
  100. elem.style.backgroundColor = targetColor;
  101. }
  102. }
  103. var alltags = document.getElementsByTagName("*");
  104. var bodyChanged = false;
  105. function changeAll() {
  106. var len = alltags.length;
  107. for (var i = 0; i < len; i++) {
  108. var changed = changeBgcolor(alltags[i]);
  109. var tagName = alltags[i].tagName.toUpperCase();
  110. if (changed && (tagName === "BODY" || tagName === "HTML")) {
  111. bodyChanged = true;
  112. }
  113. }
  114. }
  115. changeAll();
  116. if (window.top == window) {
  117. // change transparent only when in top frame
  118. if (!bodyChanged) {
  119. changeTransparent(document.body.parentNode);
  120. }
  121. }
  122. for (var i = 0; i < longRunSites.length; i++) {
  123. if (location.hostname === longRunSites[i]) {
  124. console.info('make NoBrighter runs forever...');
  125. setInterval(changeAll, 2000); // convert every 2s
  126. break;
  127. }
  128. }