Force new.reddit.com

Force new.reddit.com.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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
        }
    }
}