Greasy Fork 支持简体中文。

Fix bad CSS foreground color

If you use a dark theme defective CSS styles will give you white on white text color. This is a fix for that.

  1. // ==UserScript==
  2. // @name Fix bad CSS foreground color
  3. // @name:hu Rossz CSS előtér színek javítása
  4. // @namespace adventuretc
  5. // @description If you use a dark theme defective CSS styles will give you white on white text color. This is a fix for that.
  6. // @description:hu Ha sötét témát használsz a rossz CSS-ek fehér alapon fehér szöveget eredményezhetnek. Ez egy javítás erre.
  7. // @include *
  8. // @version 1
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. main();
  13.  
  14. setTimeout(function(){ main(); }, 2000);
  15.  
  16. function main()
  17. {
  18. var fixedTags = ["textarea", "input", "select"];
  19. for (var i = 0; i<fixedTags.length; ++i)
  20. {
  21. var v = fixedTags[i];
  22. if (!gettag(v)[0])
  23. continue;
  24.  
  25. for (var y = 0; y<gettag(v).length; ++y)
  26. {
  27. if (!gettag(v)[y])
  28. continue;
  29. var observed = gettag(v)[y];
  30. var changed = gettag(v)[y];
  31. var style = window.getComputedStyle(observed);
  32. var defaultStyle = window.getDefaultComputedStyle(observed);
  33.  
  34. var backgroundColor = style.getPropertyValue("background-color");
  35. var defaultBackgroundColor = defaultStyle.getPropertyValue("background-color");
  36. if (backgroundColor === "transparent" && !hasStyledBackground(observed))
  37. {
  38. var observedMaybe = findUpColoredBackground(observed); //find first parent with color
  39. if (observedMaybe)
  40. {
  41. observed = observedMaybe;
  42. }
  43. else
  44. {
  45. continue;
  46. }
  47. }
  48. style = window.getComputedStyle(observed);
  49. defaultStyle = window.getDefaultComputedStyle(observed);
  50.  
  51. backgroundColor = style.getPropertyValue("background-color");
  52. defaultBackgroundColor = defaultStyle.getPropertyValue("background-color");
  53.  
  54. if (hasStyledBackground(observed)) //webpage specified some background
  55. {
  56. if (style.color === defaultStyle.color) //but not foreground
  57. {
  58. if ( average( rgbArray(backgroundColor) ) > 127 )
  59. {
  60. changed.style.color = "#000";
  61. }
  62. else
  63. {
  64. changed.style.color = "#FFF";
  65. }
  66. if (backgroundColor === "transparent")
  67. {
  68. changed.style.color = "#000";
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75.  
  76. function average(someArr)
  77. {
  78. var sum = 0;
  79. for (var i=0; i<someArr.length; ++i)
  80. {
  81. sum += parseInt(someArr[i]);
  82. }
  83.  
  84. return (sum/someArr.length);
  85. }
  86.  
  87. function rgbArray(rgb)
  88. {
  89.  
  90. rgb = rgb.replace(/[^\d,]/g, '').split(',');
  91. return rgb;
  92. }
  93.  
  94. function gettag(s)
  95. {
  96. return document.getElementsByTagName(s);
  97. }
  98.  
  99. function findUpColoredBackground(el)
  100. {
  101. while (el.parentNode)
  102. {
  103. el = el.parentNode;
  104. if (hasStyledBackground(el)) //webpage specified some background
  105. {
  106. if (backgroundColor !== "transparent")
  107. {
  108. return el;
  109. }
  110. }
  111. }
  112. return null;
  113. }
  114.  
  115. function hasStyledBackground(element)
  116. {
  117. var style = window.getComputedStyle(element);
  118. var defaultStyle = window.getDefaultComputedStyle(element);
  119.  
  120. var backgroundColor = style.getPropertyValue("background-color");
  121. var defaultBackgroundColor = defaultStyle.getPropertyValue("background-color");
  122.  
  123. var backgroundImage = style.getPropertyValue("background-image");
  124. var defaultBackgroundImage = defaultStyle.getPropertyValue("background-image");
  125.  
  126. if (backgroundColor !== defaultBackgroundColor)
  127. {
  128. return true;
  129. }
  130. if (backgroundImage !== defaultBackgroundImage)
  131. {
  132. return true;
  133. }
  134. return false;
  135. }