Advertisement
Guest User

Untitled

a guest
Dec 11th, 2024
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.83 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name         Reddit Green Beta Theme Remover
  3. // @namespace    http://tampermonkey.net/
  4. // @version      2024-12-11
  5. // @description  Removes Reddit's latest UI disaster
  6. // @author       ez
  7. // @match        https://www.reddit.com/*
  8. // @match        https://reddit.com/*
  9. // @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
  10. // @grant        none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.     'use strict';
  15.  
  16.     // Function to remove a specific class from an element, if it exists
  17.     function removeClassFromElement(element, className) {
  18.         if (element.classList && element.classList.contains(className)) {
  19.             element.classList.remove(className);
  20.         }
  21.     }
  22.  
  23.     // Function to replace a specific class in the element's class list
  24.     function replaceClassInElement(element, oldClass, newClass) {
  25.         if (element.classList && element.classList.contains(oldClass)) {
  26.             element.classList.replace(oldClass, newClass);
  27.         }
  28.     }
  29.  
  30.     // Function to replace color in inline styles
  31.     function replaceColorInStyles(element) {
  32.         if (element.style.cssText) {
  33.             element.style.cssText = element.style.cssText.replace(/#0e1113/g, '#121213');
  34.         }
  35.     }
  36.  
  37.     // Function to replace color '#0e1113' with '#121213' in styles
  38.     function replaceColorInPage() {
  39.         const allElements = document.querySelectorAll('*');
  40.         allElements.forEach(element => {
  41.             replaceColorInStyles(element);
  42.         });
  43.     }
  44.  
  45.     // Function to modify the classes for the page (removing or replacing specific classes)
  46.     function modifyClassesOnPage() {
  47.         const allElements = document.querySelectorAll('*');
  48.         allElements.forEach(element => {
  49.             // For all elements, remove 'theme-beta' and 'bg-neutral-background'
  50.             if (element.classList) {
  51.                 removeClassFromElement(element, 'theme-beta');
  52.                 removeClassFromElement(element, 'bg-neutral-background');
  53.             }
  54.         });
  55.     }
  56.  
  57.     // Function to update all CSS variables starting with '--color-neutral-background'
  58.     function changeCssVariablesWithWildcard() {
  59.         const root = document.documentElement;
  60.  
  61.         // Update all CSS variables that start with --color-neutral-background
  62.         const styles = window.getComputedStyle(root);
  63.         for (let i = 0; i < styles.length; i++) {
  64.             const property = styles[i];
  65.             if (property.startsWith('--color-neutral-background')) {
  66.                 root.style.setProperty(property, '#121213');
  67.             }
  68.         }
  69.     }
  70.  
  71.     // Debounced function to apply changes
  72.     let scrollTimeout;
  73.     function applyChangesOnScroll() {
  74.         clearTimeout(scrollTimeout);
  75.         scrollTimeout = setTimeout(() => {
  76.             modifyClassesOnPage();  // Modify classes
  77.             replaceColorInPage();   // Replace color
  78.             changeCssVariablesWithWildcard();    // Update CSS variables
  79.         }, 200); // Adjust debounce delay as needed (200ms in this case)
  80.     }
  81.  
  82.     // Scroll event listener to apply changes as you scroll
  83.     window.addEventListener('scroll', applyChangesOnScroll);
  84.  
  85.     // Initial call to apply changes when the page loads
  86.     window.addEventListener('load', function() {
  87.         modifyClassesOnPage();  // Modify classes
  88.         replaceColorInPage();   // Replace color
  89.         changeCssVariablesWithWildcard();    // Update CSS variables
  90.     });
  91.  
  92.     // Use MutationObserver to detect dynamically added elements and update them
  93.     const observer = new MutationObserver(() => {
  94.         modifyClassesOnPage();  // Modify classes
  95.     });
  96.  
  97.     // Observe changes to the entire document, particularly for added nodes (including dynamically added <div>s)
  98.     observer.observe(document.body, { childList: true, subtree: true });
  99.  
  100. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement