White background replacer

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

当前为 2023-08-24 提交的版本,查看 最新版本

  1. 'use strict';
  2. // ==UserScript==
  3. // @name White background replacer
  4. // @namespace http://siavoshkc.com/
  5. // @version 2.5
  6. // @description Replaces the background white color with a darker one in order to decrease eye strain
  7. // @author siavoshkc
  8. // @match *://*/*
  9. // @grant none
  10. // @license MIT
  11. // @run-at document-idle
  12. // ==/UserScript==
  13.  
  14. const goodBgColors = ["#b0edc4", "#79d2a6", "#80e5ff", "#79C664", "#64C6B6","#4DD7C0","#B1CE61", "#61CEB2", "#66C0A9",
  15. "#6BA6D2", "#4295D3", "#7190DB", "#009900", "#6666ff","#e29d9d", "#7FD7D9", "#7FD997", "#87D97F",
  16. "#D9BD7F", "#DDA279", "#73CA70", "#7092CA", "#7CCA70", "#ACCA70,", "#ffffb3", "#70CA82", "#c4e87d"]
  17. const INTERVAL = 120000
  18. var currentGoodBgColor = 0
  19.  
  20. function isWhite(bg) {
  21. const rgb = bg.match(/[0-9]+/g);
  22. const hsl = bg.match(/hsl\(\s*(\d+)\s*,\s*(\d+(?:\.\d+)?%)\s*,\s*(\d+(?:\.\d+)?%)\)/)
  23. return (
  24. bg == "white"||
  25. bg == "#ffffff"||
  26. bg == "#FFFF"||
  27. bg == "#FFF"||
  28. bg == "#fff"||
  29. bg == "#fdfdfd" ||
  30. (hsl && Number(hsl[3].replace('%','')) > 89) ||
  31. (rgb && rgb[0] > 230 && rgb[1] > 230 && rgb[2] >230)
  32. );
  33. }
  34.  
  35. function changeColor(style) {
  36. console.debug("White background replacer: Trying to change background color ", style.backgroundColor)
  37. style.backgroundColor = goodBgColors[currentGoodBgColor % goodBgColors.length]
  38. console.debug("White background replacer: Changing one style background-color to ", goodBgColors[currentGoodBgColor % goodBgColors.length])
  39. if(style.color == style.backgroundColor) style.color = "black"
  40. if(currentGoodBgColor === Number.MAX_SAFE_INTEGER) currentGoodBgColor = 0
  41. else currentGoodBgColor++
  42. setTimeout(changeColor, INTERVAL, style)
  43. }
  44.  
  45. function iterateRules(cssRules) {
  46. if(!cssRules) return
  47.  
  48. for (let rule of cssRules) {
  49. try {
  50. if(checkStyle(rule?.style)){
  51. changeColor(rule?.style)
  52. }
  53. iterateRules(rule.cssRules)
  54. } catch(e) {
  55. console.warn("WBR: Caught exception: ".concat(e))
  56. }
  57.  
  58. }
  59. }
  60.  
  61. function checkStyle(style) {
  62. if(!style) return
  63. return (
  64. isWhite(style.background) ||
  65. isWhite(style.backgroundColor) ||
  66. isWhite(style.getPropertyValue('--bg')) ||
  67. isWhite(style.getPropertyValue('--bg-color')) ||
  68. isWhite(style.getPropertyValue('--background-color')) ||
  69. isWhite(style.getPropertyValue('--background')) ||
  70. isWhite(style.getPropertyValue('--lighting-color')) ||
  71. isWhite(style.getPropertyValue('--theme-background-color')) ||
  72. isWhite(style.getPropertyValue('--theme-content-background-color'))
  73. )
  74. }
  75.  
  76. function checkPage() {
  77. console.debug("White background replacer: Running...", window.getComputedStyle(document.documentElement));
  78. if(checkStyle(window.getComputedStyle(document.documentElement))){
  79. changeColor(document.body.style)
  80. } else if(document.styleSheets?.length > 0) {
  81. for (let sheet of document.styleSheets) {
  82. try {
  83. iterateRules(sheet.cssRules)
  84. } catch(e) {
  85. console.warn("WBR: Caught exception: ".concat(e))
  86. }
  87. }
  88. }
  89. }
  90. checkPage()
  91.  
  92.