Advertisement
Guest User

Untitled

a guest
Dec 14th, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.92 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name         Reddit Beta Theme Remover
  3. // @namespace    http://tampermonkey.net/
  4. // @version      2024-12-11
  5. // @description  Changes Reddit's latest UI disaster to something you can at least stomach
  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.     // Helper function to add/remove a class from an element
  17.     function toggleClassOnElement(element, className, action) {
  18.         if (element.classList) {
  19.             action === 'add' ? element.classList.add(className) : element.classList.remove(className);
  20.         }
  21.     }
  22.  
  23.     // Helper function to update max-width for main#main-content based on screen width
  24.     function updateMaxWidth(mainElement) {
  25.         if (window.innerWidth > 1920) {
  26.             mainElement.style.maxWidth = '1200px';  // For screens larger than 1920px
  27.         } else {
  28.             mainElement.style.maxWidth = '950px';   // For screens 1920px or smaller
  29.         }
  30.     }
  31.  
  32.     // Helper function to update margin-left for targetDiv based on screen width
  33.     function updateMarginLeft(targetDiv) {
  34.         targetDiv.style.marginLeft = window.innerWidth > 1920 ? '200px' : '50px';
  35.     }
  36.  
  37.     // Function to replace colors in inline styles and CSS variables
  38.     function replaceColors() {
  39.         const allElements = document.querySelectorAll('*');
  40.         allElements.forEach(element => {
  41.             if (element.style.cssText) {
  42.                 element.style.cssText = element.style.cssText.replace(/#0e1113/g, '#121213');
  43.             }
  44.         });
  45.  
  46.         const root = document.documentElement;
  47.         const styles = window.getComputedStyle(root);
  48.         Array.from(styles).forEach(property => {
  49.             if (property.startsWith('--color-neutral-background')) {
  50.                 root.style.setProperty(property, '#121213');
  51.             }
  52.         });
  53.     }
  54.  
  55.     // Function to modify specific elements by adding/removing classes
  56.     function modifyClassesOnPage() {
  57.         document.querySelectorAll('*').forEach(element => {
  58.             toggleClassOnElement(element, 'theme-beta', 'remove');
  59.             toggleClassOnElement(element, 'bg-neutral-background', 'remove');
  60.         });
  61.     }
  62.  
  63.     // Function to apply custom margins and styles to targetDiv
  64.     function applyCustomMarginsAndStyles() {
  65.         const targetDiv = document.querySelector('div.subgrid-container.m\\:col-start-2');
  66.         const mainElement = document.querySelector('main#main-content');
  67.         const rightSidebar = document.querySelector('#right-sidebar-container'); // Right sidebar
  68.  
  69.         if (targetDiv && mainElement && rightSidebar) {
  70.             // Apply dynamic margin and width adjustments for left content (subgrid-container)
  71.             toggleClassOnElement(targetDiv, 'mx-auto', 'remove');
  72.             targetDiv.classList.add('mr-auto');  // Apply right margin auto for alignment
  73.             updateMarginLeft(targetDiv);         // Update margin-left based on screen width
  74.             toggleClassOnElement(targetDiv, 'm:w-[1120px]', 'remove');
  75.             targetDiv.classList.replace('w-full', 'w-3/4');  // Apply w-3/4 to subgrid-container
  76.  
  77.             // Update the max-width for the main element
  78.             updateMaxWidth(mainElement);   // Update max-width based on screen width
  79.  
  80.             // Set min-width to 950px for main element
  81.             mainElement.style.minWidth = '950px';  // Ensure min-width is 950px
  82.  
  83.             // Apply transform to shift right sidebar
  84.             if (window.innerWidth > 1920) {
  85.                 rightSidebar.style.transform = 'translateX(250px)';  // 250px shift for large screens
  86.             } else {
  87.                 rightSidebar.style.transform = 'translateX(50px)';  // 150px shift for smaller screens
  88.             }
  89.  
  90.             // Ensure main content stays full width (w-full)
  91.             toggleClassOnElement(mainElement, 'w-full', 'add');
  92.         }
  93.     }
  94.  
  95.     // Function to add 'theme-beta' class to specific elements
  96.     function addThemeBetaClassToElements() {
  97.         const elements = document.querySelectorAll('span[data-part="inbox"], span[data-part="advertise"], span[data-part="chat"], span[data-part="create"], reddit-search-large, #user-drawer-content');
  98.         elements.forEach(element => toggleClassOnElement(element, 'theme-beta', 'add'));
  99.     }
  100.  
  101.     // Main function to apply all changes (called on load, scroll, and resize)
  102.     function applyChanges() {
  103.         modifyClassesOnPage();
  104.         replaceColors();
  105.         applyCustomMarginsAndStyles();
  106.         addThemeBetaClassToElements();
  107.     }
  108.  
  109.     // Throttle the scroll event to optimize performance (to prevent excess firing)
  110.     let scrollTimeout;
  111.     function handleScroll() {
  112.         clearTimeout(scrollTimeout);
  113.         scrollTimeout = setTimeout(applyChanges, 50); // Apply every 50ms to throttle the scroll event
  114.     }
  115.  
  116.     // Resize event handler using requestAnimationFrame for smoother resizing
  117.     let resizing = false;
  118.     function handleResize() {
  119.         if (!resizing) {
  120.             resizing = true;
  121.             requestAnimationFrame(() => {
  122.                 applyChanges();
  123.                 resizing = false;
  124.             });
  125.         }
  126.     }
  127.  
  128.     // Initial call to apply changes when the page loads
  129.     window.addEventListener('load', applyChanges);
  130.  
  131.     // Scroll and resize event listeners (with throttling and requestAnimationFrame)
  132.     window.addEventListener('scroll', handleScroll);  // Throttled scroll event
  133.     window.addEventListener('resize', handleResize);  // requestAnimationFrame for resize
  134.  
  135.     // MutationObserver to detect dynamically added elements and update them
  136.     const observer = new MutationObserver(() => {
  137.         applyChanges();
  138.     });
  139.     observer.observe(document.body, { childList: true, subtree: true });
  140. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement