White background replacer

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

当前为 2023-01-31 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name White background replacer
  3. // @namespace http://siavoshkc.com/
  4. // @version 2.4
  5. // @description Replaces the background white color with a darker one 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. const rgb = bg.match(/[0-9]+/g);
  21. const hsl = bg.match(/hsl\(\s*(\d+)\s*,\s*(\d+(?:\.\d+)?%)\s*,\s*(\d+(?:\.\d+)?%)\)/)
  22. return (
  23. bg == "white"||
  24. bg == "#ffffff"||
  25. bg == "#FFFF"||
  26. bg == "#FFF"||
  27. bg == "#fff"||
  28. (hsl && Number(hsl[3].replace('%','')) > 89) ||
  29. (rgb && rgb[0] > 230 && rgb[1] > 230 && rgb[2] >230)
  30. );
  31. }
  32. function changeColor(style) {
  33. console.debug("White background replacer: Trying to change background color ", style.backgroundColor)
  34. style.backgroundColor = goodBgColors[currentGoodBgColor % goodBgColors.length]
  35. console.debug("White background replacer: Changing one style background-color to ", goodBgColors[currentGoodBgColor % goodBgColors.length]);
  36. if(style.color == style.backgroundColor) style.color = "black";
  37. if(currentGoodBgColor === Number.MAX_SAFE_INTEGER) currentGoodBgColor = 0;
  38. else currentGoodBgColor++;
  39. setTimeout(changeColor, INTERVAL, style)
  40.  
  41. }
  42. function checkPage()
  43. {
  44. 'use strict';
  45. console.debug("White background replacer: Running...");
  46.  
  47. if(document.styleSheets.length === 0) changeColor(document.body.style);
  48. else
  49. {
  50. for (let sheet of document.styleSheets) {
  51. try{
  52. let rules= sheet.cssRules;
  53.  
  54. for (let rule of rules) {
  55.  
  56. if( rule?.style && (
  57. isWhite(rule.style?.backgroundColor) ||
  58. isWhite(rule.style.getPropertyValue('--bg')) ||
  59. isWhite(rule.style.getPropertyValue('--bg-color')) ||
  60. isWhite(rule.style.getPropertyValue('--background-color')) ||
  61. isWhite(rule.style.getPropertyValue('--background'))
  62. ))
  63. {
  64. changeColor(rule.style);
  65. }
  66. if (rule?.cssRules) {
  67. for (let innerRule of rule.cssRules) {
  68. if( innerRule?.style && isWhite(innerRule.style?.backgroundColor)){
  69. changeColor(innerRule.style);
  70. }
  71. }
  72. }
  73.  
  74. }
  75. }
  76. catch(e)
  77. {
  78. console.warn("White background replacer: Caught exception: ".concat(e));
  79. continue;
  80. }
  81. }
  82. }
  83.  
  84. };
  85. checkPage() ;
  86.