Reddit - remove share buttons, collapse header

Removes all share buttons from Reddit and makes the header collapsible

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Reddit - remove share buttons, collapse header
// @namespace    reddit-no-share
// @version      1.0.1
// @description  Removes all share buttons from Reddit and makes the header collapsible
// @author       lowblow
// @match        https://www.reddit.com/*
// @match        https://old.reddit.com/*
// @match        https://new.reddit.com/*
// @match        https://sh.reddit.com/*
// @icon         https://redditinc.com/hs-fs/hubfs/Reddit%20Inc/Content/Brand%20Page/Reddit_Logo.png?width=200&height=200&name=Reddit_Logo.png
// @license      MIT
// @run-at       document-idle
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    'use strict';

    // CSS for light DOM elements (old reddit, anything outside shadow DOM)
    GM_addStyle(`
        shreddit-post-share-button,
        shreddit-share-button,
        share-button,
        .post-sharing-button,
        li.share,
        a.post-sharing-button {
            display: none !important;
        }
    `);

    // Inject CSS into a shadow root to hide share buttons inside it
    function injectShadowCSS(shadowRoot) {
        if (shadowRoot._shareHidden) return;
        shadowRoot._shareHidden = true;

        const style = document.createElement('style');
        style.textContent = `
            shreddit-post-share-button,
            shreddit-share-button,
            share-button,
            [slot="share-button"],
            slot[name="share-button"] {
                display: none !important;
            }
        `;
        shadowRoot.appendChild(style);
    }

    // Walk all shadow roots in the document and inject CSS
    function hideShareButtons() {
        // Target all custom elements that might have shadow roots
        const elements = document.querySelectorAll('shreddit-post, shreddit-comment, shreddit-ad-post, shreddit-post-share-button');
        for (const el of elements) {
            if (el.shadowRoot) {
                injectShadowCSS(el.shadowRoot);

                // Also check nested shadow roots
                const nested = el.shadowRoot.querySelectorAll('*');
                for (const n of nested) {
                    if (n.shadowRoot) {
                        injectShadowCSS(n.shadowRoot);
                    }
                }
            }
            // Also hide the element itself if it's a share button
            if (el.tagName.toLowerCase().includes('share')) {
                el.style.setProperty('display', 'none', 'important');
            }
        }

        // Brute force: find ALL elements with shadow roots
        const allElements = document.querySelectorAll('*');
        for (const el of allElements) {
            if (el.shadowRoot && !el.shadowRoot._shareHidden) {
                injectShadowCSS(el.shadowRoot);
            }
        }
    }

    // Run immediately
    hideShareButtons();

    // Make the header collapsible
    function initCollapsibleHeader() {
        if (document.getElementById('rutil-header-toggle')) return;

        const header = document.querySelector('shreddit-header') || document.querySelector('header');
        if (!header) return;

        const toggleBtn = document.createElement('button');
        toggleBtn.id = 'rutil-header-toggle';

        Object.assign(toggleBtn.style, {
            position: 'fixed',
            top: '0',
            right: '50px',
            zIndex: '9999999',
            background: 'var(--color-neutral-background, #1a1a1b)',
            color: 'var(--color-neutral-content, #d7dadc)',
            border: '1px solid var(--color-neutral-border, #343536)',
            borderTop: 'none',
            borderBottomLeftRadius: '4px',
            borderBottomRightRadius: '4px',
            padding: '2px 10px',
            cursor: 'pointer',
            fontSize: '12px',
            fontWeight: 'bold',
            opacity: '0.7',
            transition: 'opacity 0.2s',
            height: '24px',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            boxShadow: '0 2px 4px rgba(0,0,0,0.2)'
        });

        toggleBtn.addEventListener('mouseenter', () => toggleBtn.style.opacity = '1');
        toggleBtn.addEventListener('mouseleave', () => toggleBtn.style.opacity = '0.7');

        let isHidden = localStorage.getItem('rutil:header-hidden') === 'true';

        function updateHeader() {
            if (isHidden) {
                header.style.setProperty('display', 'none', 'important');
                toggleBtn.textContent = '↓';
                toggleBtn.title = 'Show Header';
            } else {
                header.style.removeProperty('display');
                toggleBtn.textContent = '↑';
                toggleBtn.title = 'Hide Header';
            }
        }

        updateHeader();

        toggleBtn.addEventListener('click', () => {
            isHidden = !isHidden;
            localStorage.setItem('rutil:header-hidden', isHidden);
            updateHeader();
        });

        document.body.appendChild(toggleBtn);
    }

    initCollapsibleHeader();

    // Watch for new posts loading (infinite scroll, navigation)
    const observer = new MutationObserver((mutations) => {
        let shouldRun = false;
        for (const mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                shouldRun = true;
                break;
            }
        }
        if (shouldRun) {
            hideShareButtons();
            initCollapsibleHeader();
        }
    });

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

    // Also run periodically for the first few seconds to catch late-loading elements
    let runs = 0;
    const interval = setInterval(() => {
        hideShareButtons();
        initCollapsibleHeader();
        runs++;
        if (runs > 20) clearInterval(interval);
    }, 500);
})();