New Reddit for Everything but Post Pages

Redirects or changes hrefs for Reddit pages to new.reddit.com except for post pages, which it redirects to old.reddit.com. This is mainly to allow for compatibility with the Reddit Enhancement Suite, and its post page and comment features, while still maintaining the nice overview of the new.reddit.com home page timeline and profile pages.

// ==UserScript==
// @name         New Reddit for Everything but Post Pages
// @namespace    plennhar-new-reddit-for-everything-but-post-pages
// @version      2.2
// @description  Redirects or changes hrefs for Reddit pages to new.reddit.com except for post pages, which it redirects to old.reddit.com.  This is mainly to allow for compatibility with the Reddit Enhancement Suite, and its post page and comment features, while still maintaining the nice overview of the new.reddit.com home page timeline and profile pages.
// @author       Plennhar
// @match        *://*.reddit.com/*
// @license      MIT
// @grant        none
// ==/UserScript==
// SPDX-FileCopyrightText: 2024 Plennhar
// SPDX-License-Identifier: MIT

(function() {
    'use strict';

    const newRedditPrefix = 'https://new.reddit.com';
    const oldRedditPrefix = 'https://old.reddit.com';
    const redditPrefix = 'https://www.reddit.com';

    function redirectToOldRedditIfNeeded() {
        if (window.location.href.startsWith(newRedditPrefix)) {
            const urlParts = window.location.pathname.split('/');
            if (urlParts.length > 3 && urlParts[1] === 'r' && urlParts[3] === 'comments') {
                const oldUrl = window.location.href.replace(newRedditPrefix, oldRedditPrefix);
                window.location.href = oldUrl;
            }
        }
    }

    function redirectToNewRedditIfNeeded() {
        if (window.location.href.startsWith(oldRedditPrefix)) {
            const urlParts = window.location.pathname.split('/');
            if (!(urlParts.length > 3 && urlParts[1] === 'r' && urlParts[3] === 'comments')) {
                const newUrl = window.location.href.replace(oldRedditPrefix, newRedditPrefix);
                window.location.href = newUrl;
            }
        } else if (window.location.href.startsWith(redditPrefix)) {
            const newUrl = window.location.href.replace(redditPrefix, newRedditPrefix);
            window.location.href = newUrl;
        }
    }

    if (window.location.href.startsWith(newRedditPrefix) || window.location.href.startsWith(redditPrefix)) {
        document.querySelectorAll('a[href*="/comments/"]').forEach(anchor => {
            const href = anchor.getAttribute('href');
            if (href && href.startsWith('/r/')) {
                const oldUrl = oldRedditPrefix + href;
                anchor.setAttribute('href', oldUrl);
            } else if (href && href.startsWith(redditPrefix + '/r/')) {
                const oldUrl = href.replace(redditPrefix, oldRedditPrefix);
                anchor.setAttribute('href', oldUrl);
            }
        });

        redirectToOldRedditIfNeeded();
    } else if (window.location.href.startsWith(oldRedditPrefix)) {
        document.querySelectorAll('a').forEach(anchor => {
            const href = anchor.getAttribute('href');
            if (href) {
                if (!href.includes('/comments/')) {
                    if (href.startsWith('/')) {
                        const newUrl = newRedditPrefix + href;
                        anchor.setAttribute('href', newUrl);
                    } else if (href.startsWith(oldRedditPrefix)) {
                        const newUrl = href.replace(oldRedditPrefix, newRedditPrefix);
                        anchor.setAttribute('href', newUrl);
                    } else if (href.startsWith(redditPrefix)) {
                        const newUrl = href.replace(redditPrefix, newRedditPrefix);
                        anchor.setAttribute('href', newUrl);
                    }
                }
            }
        });

        redirectToNewRedditIfNeeded();
    }

    redirectToNewRedditIfNeeded();
})();