Redirect Reddit URLs

Redirect any Reddit domain that isn't "old" to old.reddit.com while keeping the rest of the URL. Remove string after the last / in old.reddit.com URLs. Integrate shouldExcludePath function.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Redirect Reddit URLs
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Redirect any Reddit domain that isn't "old" to old.reddit.com while keeping the rest of the URL. Remove string after the last / in old.reddit.com URLs. Integrate shouldExcludePath function.
// @author       You
// @match        *://*.reddit.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function removeDotsFromPath() {
        const host = window.location.host;
        const path = window.location.pathname;
        if (host === "old.reddit.com") {
            if (path.includes("/domain/")) {
                const newPath = path.replace(/\.[a-z]+$/, '');
                const newUrl = `https://${host}${newPath}${window.location.search}${window.location.hash}`;
                if (newUrl !== window.location.href) {
                    window.location.replace(newUrl);
                }
            } else if (path.includes(".")) {
                const newPath = path.replace(/\.[a-z]+$/, '');
                const newUrl = `https://${host}${newPath}${window.location.search}${window.location.hash}`;
                if (newUrl !== window.location.href) {
                    window.location.replace(newUrl);
                }
            }
        }
    }

    function shouldExcludePath(path) {
        // Define paths to exclude
        const excludePaths = ['/poll/', '/gallery/', '/media'];

        // Check if the path contains any excluded substring and is not preceded by "/r"
        return excludePaths.some(exclude => path.includes(exclude) && !path.includes('/r' + exclude));
    }

    function redirectNonOldDomains() {
        const host = window.location.host;
        const path = window.location.pathname;
        const nonOldDomainRegex = /^(?!old\.)/;
        if (nonOldDomainRegex.test(host) && !shouldExcludePath(path)) {
            window.location.href = 'https://old.reddit.com' + window.location.pathname + window.location.search + window.location.hash;
        }
    }

    function redirectToOldRedditIfNotOnOldOrRedditOrTwoLetterDomain() {
        const host = window.location.host;
        const path = window.location.pathname;

        if (
            host === "www.reddit.com" &&
            !shouldExcludePath(path)
        ) {
            redirectToOldReddit();
        } else if (host === "old.reddit.com" && shouldExcludePath(path)) {
            const newUrl = `https://www.reddit.com${path}${window.location.search}${window.location.hash}`;
            window.location.replace(newUrl);
        }
    }

    function redirectToOldReddit() {
        window.location.href = 'https://old.reddit.com' + window.location.pathname + window.location.search + window.location.hash;
    }

    // Call the functions
    removeDotsFromPath();
    redirectNonOldDomains();
    redirectToOldRedditIfNotOnOldOrRedditOrTwoLetterDomain();
})();