White background replacer

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

当前为 2022-11-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name White background replacer
  3. // @namespace http://siavoshkc.com/
  4. // @version 2.1
  5. // @description Replaces the background white color with a darker color in order to decrease eye strain
  6. // @author siavoshkc
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // @run-at document-idle
  11. // ==/UserScript==
  12. const goodBgColors = ["#b0edc4", "#79d2a6", "#80e5ff", "#79C664", "#64C6B6","#4DD7C0","#B1CE61", "#61CEB2", "#66C0A9",
  13. "#6BA6D2", "#4295D3", "#7190DB", "#D97FD5", "#D97FC0","#D97F7F", "#7FD7D9", "#7FD997", "#87D97F",
  14. "#D9BD7F", "#DDA279", "#73CA70", "#7092CA", "#7CCA70", "#ACCA70,", "#9B70CA", "#70CA82", "#7599E0"]
  15. const INTERVAL = 120000
  16. var currentGoodBgColor = 0
  17.  
  18. function isWhite(bg)
  19. {
  20. return (
  21. bg == "white"||
  22. bg == "#ffffff"||
  23. bg == "#FFFF"||
  24. bg == "#FFF"||
  25. bg == "#fff" ||
  26. bg.replace(/ /g,'') == "rgb(255,255,255)"||
  27. bg.replace(/ /g,'').substring(0, 16) == "rgba(255,255,255"
  28. );
  29. }
  30. function changeColor(style) {
  31. style.backgroundColor = goodBgColors[currentGoodBgColor % goodBgColors.length]
  32. if(style.color == style.backgroundColor) style.color = "black";
  33. if(currentGoodBgColor === Number.MAX_SAFE_INTEGER) currentGoodBgColor = 0;
  34. else currentGoodBgColor++;
  35. setTimeout(changeColor, INTERVAL, style)
  36.  
  37. }
  38. function checkPage()
  39. {
  40. 'use strict';
  41. console.log("White background replacer...");
  42.  
  43. if(document.styleSheets.length === 0) changeColor(document.body.style);
  44. else
  45. {
  46. for (let sheet of document.styleSheets) {
  47. try{
  48. let rules= sheet.cssRules;
  49.  
  50. for (let rule of rules) {
  51.  
  52. if( rule?.style && isWhite(rule.style?.backgroundColor)){
  53. changeColor(rule.style);
  54. }
  55. if (rule?.cssRules) {
  56. for (let innerRule of rule.cssRules) {
  57. if( innerRule?.style && isWhite(innerRule.style?.backgroundColor)){
  58. changeColor(innerRule.style);
  59. }
  60. }
  61. }
  62.  
  63. }
  64. }
  65. catch(e)
  66. {
  67. console.log("White background replacer: Caught exception: ".concat(e));
  68. continue;
  69. }
  70. }
  71. }
  72.  
  73. };
  74. checkPage() ;
  75.