mod.reddit.com revived

Revive mod.reddit.com

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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