Darksite

browse the dark side

当前为 2019-08-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Darksite
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description browse the dark side
  6. // @author Tim L. Greller
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. window.addEventListener('load', function(){
  12. document.body.style.backgroundColor = "#1d1511";
  13. document.querySelectorAll("*").forEach(adjustColor);
  14. });
  15.  
  16.  
  17. function adjustColor(element) {
  18. var style = window.getComputedStyle(element);
  19. var background = new Color(style['background-color']);
  20. var text = new Color(style['color']);
  21. if (background.luma > 100 || text.luma < 200) {
  22. element.style.color = text.inverted.toString();
  23. element.style.backgroundColor = background.inverted.toString();
  24. }
  25. }
  26.  
  27.  
  28. var Color = (function () {
  29. function toHex(num, padding) { return num.toString(16).padStart(padding || 2); }
  30. function parsePart(value) {
  31. var perc = value.lastIndexOf('%');
  32. return perc < 0 ? value : value.substr(0, perc);
  33. }
  34. function Color(data) {
  35. if (arguments.length > 1) {
  36. this[0] = arguments[0];
  37. this[1] = arguments[1];
  38. this[2] = arguments[2];
  39. if (arguments.length > 3) { this[3] = arguments[3]; }
  40. } else if (data instanceof Color || Array.isArray(data)) {
  41. this[0] = data[0];
  42. this[1] = data[1];
  43. this[2] = data[2];
  44. this[3] = data[3];
  45. } else if (typeof data === 'string') {
  46. data = data.trim();
  47. if (data[0] === "#") {
  48. switch (data.length) {
  49. case 4:
  50. this[0] = parseInt(data[1], 16); this[0] = (this[0] << 4) | this[0];
  51. this[1] = parseInt(data[2], 16); this[1] = (this[1] << 4) | this[1];
  52. this[2] = parseInt(data[3], 16); this[2] = (this[2] << 4) | this[2];
  53. break;
  54. case 9:
  55. this[3] = parseInt(data.substr(7, 2), 16);
  56. //Fall Through
  57. case 7:
  58. this[0] = parseInt(data.substr(1, 2), 16);
  59. this[1] = parseInt(data.substr(3, 2), 16);
  60. this[2] = parseInt(data.substr(5, 2), 16);
  61. break;
  62. }
  63. } else if (data.startsWith("rgb")) {
  64. var parts = data.substr(data[3] === "a" ? 5 : 4, data.length - (data[3] === "a" ? 6 : 5)).split(',');
  65. this.r = parsePart(parts[0]);
  66. this.g = parsePart(parts[1]);
  67. this.b = parsePart(parts[2]);
  68. if (parts.length > 3) { this.a = parsePart(parts[3]); }
  69. }
  70. }
  71. }
  72. Color.prototype = {
  73. constructor: Color,
  74. 0: 255,
  75. 1: 255,
  76. 2: 255,
  77. 3: 255,
  78. get r() { return this[0]; },
  79. set r(value) { this[0] = value == null ? 0 : Math.max(Math.min(parseInt(value), 255), 0); },
  80. get g() { return this[1]; },
  81. set g(value) { this[1] = value == null ? 0 : Math.max(Math.min(parseInt(value), 255), 0); },
  82. get b() { return this[2]; },
  83. set b(value) { this[2] = value == null ? 0 : Math.max(Math.min(parseInt(value), 255), 0); },
  84. get a() { return this[3] / 255; },
  85. set a(value) { this[3] = value == null ? 255 : Math.max(Math.min(value > 1 ? value : parseFloat(value) * 255, 255), 0); },
  86. get luma() { return .299 * this.r + .587 * this.g + .114 * this.b; },
  87. get inverted() { return new Color(255 - this[0], 255 - this[1], 255 - this[2], this[3]); },
  88. toString: function (option) {
  89. if (option === 16) {
  90. return '#' + toHex(this.r) + toHex(this.g) + toHex(this.b) + (this[3] === 255 ? '' : toHex(this[3]));
  91. } else if (option === '%') {
  92. if (this.a !== 1) {
  93. return `rgba(${this.r / 255 * 100}%, ${this.b / 255 * 100}%, ${this.g / 255 * 100}%, ${this.a / 255})`;
  94. } else {
  95. return `rgb(${this.r / 255 * 100}%, ${this.b / 255 * 100}%, ${this.g / 255 * 100})%`;
  96. }
  97. } else {
  98. if (this.a !== 1) {
  99. return `rgba(${this.r}, ${this.b}, ${this.g}, ${this.a})`;
  100. } else {
  101. return `rgb(${this.r}, ${this.b}, ${this.g})`;
  102. }
  103. }
  104. }
  105. };
  106.  
  107. return Color;
  108. }());