Force new.reddit.com

Force new.reddit.com.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         Force new.reddit.com
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Force new.reddit.com.
// @author       You
// @match        https://new.reddit.com/*
// @grant        none
// @license     MIT
// ==/UserScript==

(function() {
    'use strict';

    if (document.readyState == "complete" || document.readyState == "loaded" || document.readyState == "interactive") {
        work();
    } else {
        document.addEventListener("DOMContentLoaded", function(event) {
            work();
        });
    }
})();

function work() {

    // Check the condition to prevent a redirect loop
    const shouldNotRedirect = document.querySelectorAll('shreddit-app').length === 0;

    // Function to check if we're already on the messages page
    function isOnMessagesPage() {
        const url = window.location.href.split('?')[0];
        return url === 'https://new.reddit.com/message/messages';
    }

    // If shouldNotRedirect is true and we are not on the messages page, exit the script
    if (shouldNotRedirect && !isOnMessagesPage()) {
        console.log("don't redirect", document.querySelectorAll('shreddit-app'), window.location.href, 'https://new.reddit.com/message/messages' === window.location.href);
        return;
    }

    // Check if we're not on the messages page
    if (!isOnMessagesPage()) {
        // Save the current URL in sessionStorage if not already set
        if (!sessionStorage.getItem('originalUrl')) {
            sessionStorage.setItem('originalUrl', window.location.href);
        }

        // Redirect to the messages page with a full page refresh
        document.location.replace('https://new.reddit.com/message/messages');
    } else {
        // We are on the messages page

        // Retrieve the original URL from sessionStorage
        const originalUrl = sessionStorage.getItem('originalUrl');

        // If there is an original URL stored, navigate back to it
        if (originalUrl) {
            console.log("nav back", originalUrl, window.location.href);
            setTimeout(() => {
                // Use history.pushState to navigate back to the original Reddit page
                history.pushState({}, '', originalUrl);

                // Dispatch a popstate event to notify the React Router of the URL change
                window.dispatchEvent(new PopStateEvent('popstate'));

                // Clear the stored original URL after navigation
                sessionStorage.removeItem('originalUrl');
            }, 1); // Adjust the delay as needed to ensure messages are checked
        }
    }
}