background-color

Change the white elements to green for protecting your eyes!

目前为 2018-12-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name background-color
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.8.2
  5. // @description Change the white elements to green for protecting your eyes!
  6. // @author developerdong
  7. // @match http*://*/*
  8. // @grant none
  9. // ==/UserScript==
  10. (function () {
  11. "use strict";
  12.  
  13. function toHex(N) {
  14. if (N == null) return "00";
  15. N = parseInt(N);
  16. if (N === 0 || isNaN(N)) return "00";
  17. N = Math.max(0, N);
  18. N = Math.min(N, 255);
  19. N = Math.round(N);
  20. // return "0123456789abcdef".charAt((N - N % 16) / 16) + "0123456789abcdef".charAt(N % 16); // Lower case
  21. return "0123456789ABCDEF".charAt((N - N % 16) / 16) + "0123456789ABCDEF".charAt(N % 16); // Upper case
  22. }
  23.  
  24. /**
  25. * Transform RGB or RGBA color to HEX color
  26. * @return {string}
  27. */
  28. function RGBAtoHEX(str) {
  29. const leftParenthesisIndex = str.indexOf('(');
  30. const rightParenthesisIndex = str.indexOf(')');
  31. const colorFormat = str.substring(0, leftParenthesisIndex);
  32. const colorValueArray = str.substring(leftParenthesisIndex + 1, rightParenthesisIndex).split(',');
  33. if (colorFormat === "rgba" && parseFloat(colorValueArray[3]) !== 1) {
  34. return "TRANSPARENT";
  35. } else if (colorFormat === "rgba" || colorFormat === "rgb") {
  36. const r = colorValueArray[0].trim(), g = colorValueArray[1].trim(), b = colorValueArray[2].trim();
  37. const hex = [toHex(r), toHex(g), toHex(b)];
  38. return '#' + hex.join('');
  39. } else {
  40. return str;
  41. }
  42. }
  43.  
  44. /**
  45. * Change the background color of all sub elements by level order
  46. */
  47. function changeBackgroundColor(node) {
  48. const queue = [];
  49. while (node) {
  50. // nodeType === 1 means the node is an element
  51. if (node.nodeType === 1) {
  52. const nodeStyle = window.getComputedStyle(node);
  53. const hexColor = RGBAtoHEX(nodeStyle.backgroundColor);
  54. // nodeStyle.cursor !== "pointer" means the node is non-clickable
  55. if ((node === document.body && hexColor === "TRANSPARENT") || (hexColor === "#FFFFFF" && nodeStyle.cursor !== "pointer")) {
  56. node.style.setProperty("background-color", "#C1E6C6", "important");
  57. }
  58. Array.from(node.children).forEach(function (child) {
  59. queue.push(child);
  60. });
  61. }
  62. node = queue.shift();
  63. }
  64. }
  65.  
  66. // Change the background color of newly added elements
  67. const observer = new MutationObserver(function (records) {
  68. records.map(function (record) {
  69. if (record.type === "childList") {
  70. record.addedNodes.forEach(changeBackgroundColor);
  71. }
  72. });
  73. });
  74. const option = {
  75. "childList": true,
  76. "subtree": true
  77. };
  78. observer.observe(document.body, option);
  79.  
  80. // Change the background color of initially loaded elements
  81. changeBackgroundColor(document.body);
  82.  
  83. })();