继续操作前请注册或者登录。

No White Background Color

Save your eyes by changing white background color

  1. // ==UserScript==
  2. // @author Tommy Reynolds <oldest.software.guy@gmail.com>
  3. // @description Save your eyes by changing white background color
  4. // @grant none
  5. // @include https?://*
  6. // @name No White Background Color
  7. // @namespace www.MegaCoder.com
  8. // @run-at document-end
  9. // @version 1.0.2
  10. // ==/UserScript==
  11. // ============================================================================
  12. // No White Background Color (NWBC) Script for GreaseMonkey
  13. // Modified by oldest.software.guy@gmail.com
  14. // Modified by Wellen
  15. // Originally by Howard Smith
  16. // ---------------------------------------------------------------------------
  17. // This was originally by Howard Smith and seems to be the best script which
  18. // works on the most websites for getting the eyeball burning white background
  19. // color most sites seem to use, to be a less harsh color of the users
  20. // choice. Firstly I have no intention of taking the credit for this script as
  21. // it is Howard's work from many years ago, but as I couldn't find it online
  22. // easily I wanted to make sure it wasn't lost to the Internet ether so I
  23. // thought I should post it here.
  24. // ---------------------------------------------------------------------------
  25. // I found it on:
  26. // http://superuser.com/questions/181214/change-the-white-background-in-webpages-to-another-color
  27. // Must also thanks user The Bahamunt for not only posting it there, but for
  28. // digging it out from an old computer! Hope everyone finds it useful and
  29. // enjoys not having their eyes burnt at night!
  30. // ---------------------------------------------------------------------------
  31. // wellen
  32. // ===========================================================================
  33.  
  34. (function() {
  35. function noWhiteBackgroundColor() {
  36. function changeBackgroundColor( x ) {
  37. // auto change colors too close to white
  38. var backgroundColorRGB =
  39. window.getComputedStyle( x, null ).backgroundColor;
  40. if( backgroundColorRGB != "transparent" ) {
  41. // convert hex color to rgb color to compare
  42. var RGBValuesArray = backgroundColorRGB.match( /\d+/g );
  43. var red = RGBValuesArray[ 0 ];
  44. var green = RGBValuesArray[ 1 ];
  45. var blue = RGBValuesArray[ 2 ];
  46.  
  47. // ===========================================================
  48. // Set the base colors you require:
  49. // use: http://www.colorpicker.com
  50. // to find the rgb values of the base color you wish to
  51. // suppress white backgrounds with:
  52. // Default grey provided:
  53. // ===========================================================
  54.  
  55. var red_needed = 230;
  56. var green_needed = 230;
  57. var blue_needed = 230;
  58.  
  59. if(
  60. red >= 220 &&
  61. green >= 220 &&
  62. blue >= 220
  63. ) {
  64. // This color is just too bright for this world
  65. if(
  66. red >= 250 &&
  67. red <= 255 &&
  68. green >= 250 &&
  69. green <= 255 &&
  70. blue >= 250 &&
  71. blue <= 255
  72. ) {
  73. red_needed += 0;
  74. green_needed += 0;
  75. blue_needed += 0;
  76. } else if(
  77. red >= 240 &&
  78. red <= 255 &&
  79. green >= 240 &&
  80. green <= 255 &&
  81. blue >= 240 &&
  82. blue <= 255
  83. ) {
  84. red_needed += 6;
  85. green_needed += 3;
  86. blue_needed += 0;
  87. } else if(
  88. red >= 230 &&
  89. red <= 255 &&
  90. green >= 230 &&
  91. green <= 255 &&
  92. blue >= 230 &&
  93. blue <= 255
  94. ) {
  95. red_needed += 10;
  96. green_needed += 5;
  97. blue_needed += 0;
  98. } else if(
  99. red >= 220 &&
  100. red <= 255 &&
  101. green >= 220 &&
  102. green <= 255 &&
  103. blue >= 220 &&
  104. blue <= 255
  105. ) {
  106. red_needed += 14;
  107. green_needed += 7;
  108. blue_needed += 0;
  109. }
  110. // the background-color you want
  111. x.style.backgroundColor =
  112. "rgb( " +
  113. red_needed +
  114. ", " +
  115. green_needed +
  116. ", " +
  117. blue_needed +
  118. ")";
  119. }
  120. }
  121. }
  122. // Check and possibly change background color on each element
  123. var allElements = document.getElementsByTagName( "*" );
  124. for( var i = 0; i < allElements.length; i++ ) {
  125. changeBackgroundColor( allElements[ i ] );
  126. }
  127. }
  128. window.addEventListener(
  129. "DOMContentLoaded",
  130. noWhiteBackgroundColor,
  131. false
  132. );
  133. })();
  134.  
  135. // vim: nonu noet sw=4 ts=4 ai sm