Custom Change Color Script

当前为 2024-11-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Custom Change Color Script
  3. // @name:ru Пользовательский скрипт для замены цветов
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.1
  6. // @description:en Change specific colors to another on selected domains
  7. // @description:ru Заменяет указанные цвета на других доменах
  8. // @author shaman
  9. // @match *://*/*
  10. // @grant none
  11. // @license GPL-3.0
  12. // ==/UserScript==
  13.  
  14. const SOURCE_COLOR = '#5cdd8b';
  15. const TARGET_COLOR = '#93fab9';
  16. const ALLOWED_DOMAINS = ['example.com', 'anotherdomain.com']; // Список доменов
  17.  
  18. (function() {
  19. 'use strict';
  20.  
  21. const currentDomain = window.location.hostname;
  22.  
  23. if (!ALLOWED_DOMAINS.some(domain => currentDomain.includes(domain))) {
  24. return;
  25. }
  26.  
  27. function hexToRgb(hex) {
  28. const bigint = parseInt(hex.slice(1), 16);
  29. const r = (bigint >> 16) & 255;
  30. const g = (bigint >> 8) & 255;
  31. const b = bigint & 255;
  32. return `rgb(${r}, ${g}, ${b})`;
  33. }
  34.  
  35. const sourceRgb = hexToRgb(SOURCE_COLOR);
  36. const targetColor = TARGET_COLOR;
  37.  
  38. function updateColors() {
  39. document.querySelectorAll('*').forEach(el => {
  40. const styles = getComputedStyle(el);
  41. if (styles.color === sourceRgb) {
  42. el.style.setProperty('color', targetColor, 'important');
  43. }
  44. if (styles.backgroundColor === sourceRgb) {
  45. el.style.setProperty('background-color', targetColor, 'important');
  46. }
  47. if (styles.borderColor === sourceRgb) {
  48. el.style.setProperty('border-color', targetColor, 'important');
  49. }
  50. });
  51. }
  52.  
  53. function processShadowRoots(node) {
  54. if (node.shadowRoot) {
  55. updateColorsInShadow(node.shadowRoot);
  56. }
  57. node.childNodes.forEach(child => processShadowRoots(child));
  58. }
  59.  
  60. function updateColorsInShadow(shadowRoot) {
  61. shadowRoot.querySelectorAll('*').forEach(el => {
  62. const styles = getComputedStyle(el);
  63. if (styles.color === sourceRgb) {
  64. el.style.setProperty('color', targetColor, 'important');
  65. }
  66. if (styles.backgroundColor === sourceRgb) {
  67. el.style.setProperty('background-color', targetColor, 'important');
  68. }
  69. if (styles.borderColor === sourceRgb) {
  70. el.style.setProperty('border-color', targetColor, 'important');
  71. }
  72. });
  73. }
  74.  
  75. const observer = new MutationObserver(() => {
  76. updateColors();
  77. processShadowRoots(document.body);
  78. });
  79.  
  80. observer.observe(document.body, { childList: true, subtree: true });
  81.  
  82. updateColors();
  83. processShadowRoots(document.body);
  84. })();