White background replacer

Replaces the background white color with yellow in order to decrease eye strain

当前为 2018-12-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name White background replacer
  3. // @namespace http://siavoshkc.ir/
  4. // @version 0.6
  5. // @description Replaces the background white color with yellow in order to decrease eye strain
  6. // @author siavoshkc
  7. // @include *
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. const BG_COLOR = "#ffedc4";
  15. if(document.styleSheets.length == 0) document.body.style.backgroundColor = BG_COLOR;
  16. else
  17. {
  18. for (let sheeti= 0; sheeti<document.styleSheets.length; sheeti++) {
  19. try{
  20. let sheet= document.styleSheets[sheeti];
  21. let rules= sheet.cssRules;
  22.  
  23. for (let rulei= 0; rulei<rules.length; rulei++) {
  24. let rule= rules[rulei];
  25. if (rule.type==CSSRule.STYLE_RULE)
  26. {
  27.  
  28. if(rule.style.backgroundColor=="white"||rule.style.backgroundColor=="#ffffff"||rule.style.backgroundColor=="#FFFF"||rule.style.backgroundColor=="#FFF"||rule.style.backgroundColor=="#fff" ||rule.style.backgroundColor=="rgb(255, 255, 255)") rule.style.backgroundColor=BG_COLOR;
  29. else if(rule.style.backgroundColor==undefined || rule.style.backgroundColor=="")
  30. {
  31. let pRule = rule;
  32. let foundParentColor = false;
  33. while(pRule.parentRule != null)
  34. {
  35. pRule = pRule.parentRule;
  36. if(pRule.style.backgroundColor != undefined && pRule.style.backgroundColor != "")
  37. {
  38. foundParentColor = true;
  39. break;
  40. }
  41. }
  42. if(!foundParentColor) rule.style.backgroundColor=BG_COLOR;
  43. }
  44.  
  45. }
  46. else if(rule.type==CSSRule.IMPORT_RULE)
  47. {
  48. let importRules = rule.styleSheet.cssRules;
  49. for(let importRulei = 0; importRulei < importRules.length; importRulei++)
  50. {
  51. let importRule = importRules[importRulei];
  52. if(importRule.style.backgroundColor=="white"||importRule.style.backgroundColor=="#ffffff"||importRule.style.backgroundColor=="#FFFF"||importRule.style.backgroundColor=="#FFF"||importRule.style.backgroundColor=="#fff" ||importRule.style.backgroundColor=="rgb(255, 255, 255)") importRule.style.backgroundColor=BG_COLOR;
  53. else if(importRule.style.backgroundColor==undefined || importRule.style.backgroundColor=="")
  54. {
  55. let pRule = importRule;
  56. let foundParentColor = false;
  57. while(pRule.parentRule != null)
  58. {
  59. pRule = pRule.parentRule;
  60. if(pRule.style.backgroundColor != undefined && pRule.style.backgroundColor != "")
  61. {
  62. foundParentColor = true;
  63. break;
  64. }
  65. }
  66. if(!foundParentColor) importRule.style.backgroundColor=BG_COLOR;
  67. }
  68. }
  69.  
  70. }
  71. else if(rule.type==CSSRule.MEDIA_RULE)
  72. {
  73. let mediaRules = rule.cssRules;
  74. for(let mediaRulei = 0; mediaRulei < mediaRules.length; mediaRulei++)
  75. {
  76. let mediaRule = mediaRules[mediaRulei];
  77. if(mediaRule.style.backgroundColor=="white"||mediaRule.style.backgroundColor=="#ffffff"||mediaRule.style.backgroundColor=="#FFFF"||mediaRule.style.backgroundColor=="#FFF"||mediaRule.style.backgroundColor=="#fff" ||mediaRule.style.backgroundColor=="rgb(255, 255, 255)") mediaRule.style.backgroundColor=BG_COLOR;
  78. else if(mediaRule.style.backgroundColor==undefined || mediaRule.style.backgroundColor=="")
  79. {
  80. let pRule = mediaRule;
  81. let foundParentColor = false;
  82. while(pRule.parentRule != null)
  83. {
  84. pRule = pRule.parentRule;
  85. if(pRule.style.backgroundColor != undefined && pRule.style.backgroundColor != "")
  86. {
  87. foundParentColor = true;
  88. break;
  89. }
  90. }
  91. if(!foundParentColor) mediaRule.style.backgroundColor=BG_COLOR;
  92. }
  93. }
  94. }
  95. else {console.log("Unsupported style rule of type: ".concat(rule.type))};
  96. }
  97. }
  98. catch(e)
  99. {
  100. console.log("Caught exception: ".concat(e));
  101. continue;
  102. }
  103. }
  104. }
  105.  
  106. })();