Greasy Fork is available in English.

Reddit take back random

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

Versión del día 19/2/2025. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

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