Reddit take back random

this script simulates the "/r/random" removed from reddit

Από την 19/02/2025. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το 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                Reddit take back random
// @name:pt-BR          Reddit pegue de volta o random
// @namespace           https://greasyfork.org/users/821661
// @match               https://www.reddit.com/*
// @match               https://sh.reddit.com/*
// @match               https://old.reddit.com/*
// @grant               GM.xmlHttpRequest
// @run-at              document-start
// @version             1.0
// @author              hdyzen
// @description         this script simulates the "/r/random" removed from reddit
// @description:pt-br   esse script simula o “/r/random” removido do reddit
// @license             GPL-3.0-only
// ==/UserScript==

/**
 * A reference to the unsafeWindow object, which provides access to the page's window object
 * from a userscript. This allows the script to interact with the page's JavaScript context.
 *
 * @type {Window}
 */
const window = unsafeWindow;

/**
 * The hostname of the current window location.
 * @type {string}
 */
const domain = window.location.hostname;

/**
 * The type of the current page, determined by the isRandomPage function.
 * @type {string}
 */
const type = isRandomPage();

/**
 * Checks if the current page is a random subreddit page.
 *
 * This function examines the current window's location pathname to determine
 * if it matches either "/r/random" or "/r/randnsfw". If it matches, it returns
 * the matched subreddit type ("random" or "randnsfw"). Otherwise, it returns null.
 *
 * @returns {string|null} The type of random subreddit page ("random" or "randnsfw"), or null if not a random page.
 */
function isRandomPage() {
    const wht = window.location.pathname.match(/^\/r\/([^\/?\s]+)/)?.[1];

    return wht === "random" || wht === "randnsfw" ? wht : null;
}

/**
 * Generates a random row number based on the specified type.
 *
 * @param {string} type - The type of row to generate. Can be 'random' or 'randnsfw'.
 * @returns {number} A random row number within the range specified for the given type.
 */
function getRandomRow(type) {
    const firstRow = {
        random: 2,
        randnsfw: 290856,
    };
    const lastRow = 330694;

    return Math.floor(Math.random() * (lastRow - firstRow[type]) + firstRow[type]);
}

/**
 * Main function to fetch a random subreddit from a Google Sheets document and redirect the user to that subreddit.
 *
 * @async
 * @function main
 * @throws Will log an error to the console if the request fails.
 */
async function main() {
    try {
        if (!type) return;

        window.stop();

        const res = await GM.xmlHttpRequest({
            url: `https://docs.google.com/spreadsheets/d/1xLFbNcvpdU9j1n2fF8u2n_eGW4OekO-R2JUJb7YZxOE/gviz/tq?tq=select C limit 1 offset ${getRandomRow(type)}`,
        });

        const subreddit = res.response.match(/"v":"(.+?)"/)?.[1];

        console.log(`Random: ${subreddit}`);

        if (subreddit) {
            window.location.href = `https://${domain}/r/${subreddit}`;
        }
    } catch (err) {
        console.error(err);
    }
}
main();