Reddit bug workaround to allow posting on the r/userscript subreddit.

Redirects Reddit's submit page for r/userscripts to the old Reddit interface to fix the bug accessing the new interfaces's submit page for the subreddit.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         Reddit bug workaround to allow posting on the r/userscript subreddit.
// @namespace    https://gist.github.com/f-steff
// @version      1.2
// @description  Redirects Reddit's submit page for r/userscripts to the old Reddit interface to fix the bug accessing the new interfaces's submit page for the subreddit.
// @author       Flemming Steffensen
// @license      MIT
// @match        https://www.reddit.com/r/userscripts/*
// @match        https://old.reddit.com/r/*/comments/*
// @grant        none
// @homepageURL  https://gist.github.com/f-steff/cd4c5fafc574e5595fc7a153516792ab
// @run-at       document-start
// ==/UserScript==


(function() {
    'use strict';

    // Redirect old.reddit.com comment links to www.reddit.com
    if (window.location.href.includes('old.reddit.com') && window.location.href.includes('/comments/')) {
        window.location.href = window.location.href.replace('old.reddit.com', 'www.reddit.com');
    }

    // Function to modify the "Create Post" button
    function modifyCreatePostButton() {
        const createPostButton = document.querySelector('a[data-testid="create-post"]');
        if (createPostButton && createPostButton.href.includes('www.reddit.com')) {
            createPostButton.href = createPostButton.href.replace('www.reddit.com', 'old.reddit.com');
        }
    }

    // Ensure the DOM is fully loaded before running
    function init() {
        modifyCreatePostButton();

        // Observe for dynamic changes to the page and reapply the modification if needed
        const observer = new MutationObserver(() => {
            modifyCreatePostButton();
        });

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

    if (window.location.href.includes('/r/userscripts/')) {
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', init);
        } else {
            init();
        }
    }
})();