Reddit → Old Reddit Redirector

Redirect all reddit.com pages and links to old.reddit.com

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reddit → Old Reddit Redirector
// @version      1.0
// @description  Redirect all reddit.com pages and links to old.reddit.com
// @match        *://*.reddit.com/*
// @author        Mane
// @license      CC0-1.0
// @run-at       document-start
// @grant        none
// @namespace https://greasyfork.org/users/1491313
// ==/UserScript==

(function() {
    'use strict';

    // Helper: if URL belongs to reddit.com (but not old.reddit.com), return rewritten URL
    function toOldReddit(url) {
        try {
            const u = new URL(url);
            const host = u.hostname.toLowerCase();
            if (
                (host === 'reddit.com' ||
                 host === 'www.reddit.com' ||
                 host === 'new.reddit.com' ||
                 (host.endsWith('.reddit.com') && !host.startsWith('old.')))
            ) {
                u.hostname = 'old.reddit.com';
                return u.href;
            }
        } catch (err) {
            return null;
        }
        return null;
    }

    // 1. Redirect current page on load
    const rewritten = toOldReddit(window.location.href);
    if (rewritten) {
        window.location.replace(rewritten);
        return;
    }

    // 2. Intercept all clicks on <a> tags pointing to reddit.com
    document.addEventListener('click', function(event) {
        const anchor = event.target.closest('a[href]');
        if (!anchor) return;

        const newHref = toOldReddit(anchor.href);
        if (newHref) {
            event.preventDefault();
            window.location.href = newHref;
        }
    }, true);
})();