mod.reddit.com revived

Revive mod.reddit.com

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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