Reddit Enhancer (Modern + Old Support)

Enhance Reddit UI: dark mode, wide layout, remove promoted/suggested, auto-expand comments, bottom-right floating gear menu, and more. Supports both new and old Reddit layouts.

目前為 2025-06-01 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Reddit Enhancer (Modern + Old Support)
// @namespace    https://greasyfork.org/en/users/123456-eliminater74
// @version      1.2
// @description  Enhance Reddit UI: dark mode, wide layout, remove promoted/suggested, auto-expand comments, bottom-right floating gear menu, and more. Supports both new and old Reddit layouts.
// @author       Eliminater74
// @license      MIT
// @match        https://www.reddit.com/*
// @match        https://old.reddit.com/*
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    'use strict';

    const settingsKey = 'redditEnhancerSettings';
    const defaultSettings = {
        darkMode: false,
        hidePromoted: true,
        hideSuggested: true,
        wideMode: true,
        fontSize: '15px',
        lineHeight: '1.6',
        autoExpandComments: true,
        autoRefresh: false
    };

    let settings = JSON.parse(localStorage.getItem(settingsKey)) || defaultSettings;

    function saveSettings() {
        localStorage.setItem(settingsKey, JSON.stringify(settings));
    }

    function applySettings() {
        document.body.style.setProperty('--custom-font-size', settings.fontSize);
        document.body.style.setProperty('--custom-line-height', settings.lineHeight);
        document.body.classList.toggle('re-wide-mode', settings.wideMode);
        document.body.classList.toggle('re-dark-mode', settings.darkMode);
    }

    function hideElements() {
        const isOldReddit = location.hostname.startsWith('old.');

        if (settings.hidePromoted) {
            if (isOldReddit) {
                document.querySelectorAll('.promotedlink').forEach(el => el.remove());
            } else {
                document.querySelectorAll('span').forEach(el => {
                    if (el.textContent.toLowerCase().includes('promoted')) {
                        el.closest('div[data-testid="post-container"]')?.remove();
                    }
                });
            }
        }

        if (settings.hideSuggested) {
            if (isOldReddit) {
                document.querySelectorAll('.thing').forEach(el => {
                    const txt = el.innerText.toLowerCase();
                    if (txt.includes('because you follow') || txt.includes('suggested')) {
                        el.remove();
                    }
                });
            } else {
                document.querySelectorAll('div[data-testid="post-container"]').forEach(el => {
                    const txt = el.innerText.toLowerCase();
                    if (txt.includes('because you follow') || txt.includes('suggested')) {
                        el.remove();
                    }
                });
            }
        }
    }

    function autoExpandComments() {
        if (!settings.autoExpandComments) return;
        const isOldReddit = location.hostname.startsWith('old.');

        if (isOldReddit) {
            document.querySelectorAll('.morecomments a').forEach(btn => btn.click());
        } else {
            document.querySelectorAll('button[data-testid="comment_expand_button"]').forEach(btn => btn.click());
        }
    }

    function autoRefreshFeed() {
        if (settings.autoRefresh) {
            setTimeout(() => location.reload(), 1000 * 60 * 5); // 5 min
        }
    }

    function createSettingsMenu() {
        const menu = document.createElement('div');
        menu.id = 'reddit-enhancer-menu';
        menu.innerHTML = `
            <button id="re-toggle" title="Reddit Enhancer Settings">⚙️</button>
            <div id="re-panel">
                <label><input type="checkbox" id="re-darkMode"> Dark Mode</label><br>
                <label><input type="checkbox" id="re-hidePromoted"> Hide Promoted Posts</label><br>
                <label><input type="checkbox" id="re-hideSuggested"> Hide Suggested Posts</label><br>
                <label><input type="checkbox" id="re-wideMode"> Wide Layout</label><br>
                <label><input type="checkbox" id="re-autoExpandComments"> Auto Expand Comments</label><br>
                <label><input type="checkbox" id="re-autoRefresh"> Auto Refresh Feed</label><br>
                <label>Font Size: <input type="text" id="re-fontSize" value="${settings.fontSize}"></label><br>
                <label>Line Height: <input type="text" id="re-lineHeight" value="${settings.lineHeight}"></label><br>
                <button id="re-save">Save</button>
            </div>
        `;
        document.body.appendChild(menu);

        document.getElementById('re-darkMode').checked = settings.darkMode;
        document.getElementById('re-hidePromoted').checked = settings.hidePromoted;
        document.getElementById('re-hideSuggested').checked = settings.hideSuggested;
        document.getElementById('re-wideMode').checked = settings.wideMode;
        document.getElementById('re-autoExpandComments').checked = settings.autoExpandComments;
        document.getElementById('re-autoRefresh').checked = settings.autoRefresh;

        document.getElementById('re-toggle').onclick = () =>
            document.getElementById('re-panel').classList.toggle('open');

        document.getElementById('re-save').onclick = () => {
            settings.darkMode = document.getElementById('re-darkMode').checked;
            settings.hidePromoted = document.getElementById('re-hidePromoted').checked;
            settings.hideSuggested = document.getElementById('re-hideSuggested').checked;
            settings.wideMode = document.getElementById('re-wideMode').checked;
            settings.autoExpandComments = document.getElementById('re-autoExpandComments').checked;
            settings.autoRefresh = document.getElementById('re-autoRefresh').checked;
            settings.fontSize = document.getElementById('re-fontSize').value;
            settings.lineHeight = document.getElementById('re-lineHeight').value;
            saveSettings();
            applySettings();
            hideElements();
        };
    }

    function addStyles() {
        GM_addStyle(`
            #reddit-enhancer-menu {
                position: fixed;
                bottom: 10px;
                right: 10px;
                z-index: 99999;
                font-family: sans-serif;
            }
            #re-toggle {
                font-size: 18px;
                background: #333;
                color: #fff;
                border: none;
                border-radius: 6px;
                padding: 6px 8px;
                cursor: pointer;
            }
            #re-panel {
                display: none;
                position: absolute;
                bottom: 40px;
                right: 0;
                background: #fff;
                border: 1px solid #ccc;
                padding: 10px;
                border-radius: 8px;
                box-shadow: 0 0 10px rgba(0,0,0,0.2);
                background: #f9f9f9;
                color: #000;
                width: 240px;
            }
            #re-panel.open {
                display: block;
            }
            body.re-wide-mode ._1OVBBWLtHoSPfGCRaPzpTf,
            body.re-wide-mode .content,
            body.re-wide-mode .listing-outer {
                max-width: 100% !important;
                width: 100% !important;
            }
            body.re-dark-mode {
                background-color: #121212 !important;
                color: #e0e0e0 !important;
            }
            body.re-dark-mode #re-panel {
                background: #222;
                color: #eee;
                border-color: #444;
            }
        `);
    }

    function init() {
        addStyles();
        createSettingsMenu();
        applySettings();
        hideElements();
        autoExpandComments();
        autoRefreshFeed();
        new MutationObserver(hideElements).observe(document.body, { childList: true, subtree: true });
    }

    window.addEventListener('load', init);
})();