mod.reddit.com revived

Revive mod.reddit.com

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);
    });
})();