Force new.reddit.com

Redirect to messages and then back to the original page on Reddit

目前為 2024-09-01 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Force new.reddit.com
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Redirect to messages and then back to the original page on Reddit
// @author       You
// @match        https://www.reddit.com/*
// @match        https://new.reddit.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 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() {
        return window.location.href === 'https://new.reddit.com/message/messages';
    }

    // If shouldNotRedirect is true and we are not on the messages page, exit the script
    if (shouldNotRedirect && !isOnMessagesPage()) {
        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
        window.location.href = '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) {
            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
        }
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址