Reddit Sidebar Cleaner + Content Expander

Removes Reddit's right sidebar & expands main content - The perfect companion for Reddit Multi-Column

目前为 2025-03-28 提交的版本。查看 最新版本

// ==UserScript==
// @name         Reddit Sidebar Cleaner + Content Expander
// @description  Removes Reddit's right sidebar & expands main content - The perfect companion for Reddit Multi-Column
// @version      1.0
// @author       Stuart Saddler
// @match        *://*reddit.com/*
// @grant        none
// @license      MIT
// @namespace    https://github.com/yourusername
// ==/UserScript==

(function() {
    'use strict';

    // -------------------------------
    // Configuration and Selectors
    // -------------------------------
    const SELECTORS = {
        RIGHT_SIDEBAR: "#right-sidebar-container",
        MAIN_CONTAINER: ".main-container",
        MAIN_CONTENT: "main.main.w-full.min-w-0",
        SUBGRID: "#subgrid-container, div.subgrid-container"
    };

    const cachedElements = new Map();

    // -------------------------------
    // Element Handling Utilities
    // -------------------------------
    function getCachedElement(selector) {
        if (!cachedElements.has(selector)) {
            cachedElements.set(selector, document.querySelector(selector));
        }
        return cachedElements.get(selector);
    }

    function clearElementCache() {
        cachedElements.clear();
    }

    // -------------------------------
    // Layout Adjustment Functions
    // -------------------------------
    function adjustMainLayout() {
        try {
            // Remove right sidebar
            const rightSidebar = getCachedElement(SELECTORS.RIGHT_SIDEBAR);
            if (rightSidebar) rightSidebar.style.display = "none";

            // Expand main content container
            const mainContainer = getCachedElement(SELECTORS.MAIN_CONTAINER);
            const subgrid = getCachedElement(SELECTORS.SUBGRID);
            const mainContent = getCachedElement(SELECTORS.MAIN_CONTENT);

            if (mainContainer) {
                mainContainer.style.gridTemplateColumns = "1fr 0";
                mainContainer.style.gap = "0";
            }

            if (subgrid) {
                subgrid.style.width = "100%";
                subgrid.style.maxWidth = "100%";
            }

            if (mainContent) {
                mainContent.style.gridColumn = "1 / -1";
                mainContent.style.maxWidth = "none";
            }
        } catch (error) {
            console.error('Layout adjustment error:', error);
        }
    }

    // -------------------------------
    // Observation and Initialization
    // -------------------------------
    const observer = new MutationObserver(() => {
        clearElementCache();
        adjustMainLayout();
    });

    // Initial setup
    adjustMainLayout();
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: false,
        characterData: false
    });

    // Handle SPA navigation
    let lastUrl = location.href;
    const urlObserver = new MutationObserver(() => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            setTimeout(adjustMainLayout, 300);
        }
    });

    urlObserver.observe(document.body, {
        childList: true,
        subtree: true
    });

    // Window resize handling
    window.addEventListener('resize', () => {
        adjustMainLayout();
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址