mod.reddit.com revived

Revive mod.reddit.com

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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);
    });
})();