mod.reddit.com revived

Revive mod.reddit.com

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         mod.reddit.com revived
// @namespace    https://greasyfork.org/en/users/1574555-littux
// @version      1.2
// @description  Revive mod.reddit.com
// @author       littux
// @match        https://mod-reddit-com.netlify.app/*
// @match        http://localhost:44444/*
// @match        https://*.reddit.com/mail/*
// @connect      www.reddit.com
// @connect      sh.reddit.com
// @connect      gql-fed.reddit.com
// @icon         https://www.redditstatic.com/modmail/favicon/android-icon-192x192.png
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_xmlhttpRequest
// @grant        GM_cookie
// @run-at       document-start
// @license      MIT
// ==/UserScript==

const useLocalhost = false;

(async function() {
    if (location.hostname.endsWith("reddit.com")) {
        window.stop();
        location.replace((useLocalhost ? "http://localhost:44444" : "https://mod-reddit-com.netlify.app") + location.pathname + location.search + location.hash);
        return;
    };

    document.addEventListener('DOMContentLoaded', async () => {
        const gmFetch = (options) => {
            return new Promise((resolve, reject) => {
                GM_xmlhttpRequest({
                    ...options,
                    onload: (res) => resolve(res),
                    onerror: (err) => reject(err)
                });
            });
        }

        unsafeWindow.gmFetch = gmFetch;

        const getExternalCookie = (name, url) => {
            return new Promise((resolve, reject) => {
                if (typeof window.GM_cookie !== 'undefined') {
                    GM_cookie.list({ name, url }, (cookies, error) => {
                        if (error) reject(error);
                        else resolve(cookies.length > 0 ? cookies[0].value : null);
                    });
                } else {
                    reject("GM_cookie not supported. Ensure you are using Tampermonkey.");
                }
            });
        };

        const getToken = async () => {
            const loid = await getExternalCookie("loid", "https://www.reddit.com/mail/");
            if (!loid) {
                console.error("Not logged into Reddit? LOID missing.");
                return null;
            }

            const tokenKey = `rAPI:accessToken~${loid}`;
            const expiryKey = `rAPI:accessTokenExpiry~${loid}`;

            const cachedToken = GM_getValue(tokenKey);
            const cachedExpiry = GM_getValue(expiryKey);
            if (cachedToken && cachedExpiry > Date.now()) return cachedToken;

            await gmFetch({
                method: "HEAD",
                url: "https://sh.reddit.com/404",
                withCredentials: true
            });

            const csrf = await getExternalCookie("csrf_token", "https://sh.reddit.com/mail/");

            try {
                const response = await gmFetch({
                    method: "POST",
                    url: "https://www.reddit.com/svc/shreddit/token",
                    withCredentials: true,
                    headers: {
                        "Content-Type": "application/json",
                        "Origin": "https://www.reddit.com",
                        "Referer": "https://www.reddit.com/"
                    },
                    data: JSON.stringify({ csrf_token: csrf })
                });

                if (response.status === 200) {
                    const data = JSON.parse(response.responseText);
                    GM_setValue(tokenKey, data.token);
                    GM_setValue(expiryKey, data.expires);
                    return data.token;
                }
            } catch (e) {
                console.error("Token fetch failed:", e);
            }
            return null;
        };

        console.log("rAPI access token ready.");

        const event = new CustomEvent('rAPI-accessToken-ready', { detail: { accessToken: await getToken() }});
        window.dispatchEvent(event);
    });
})();