Reddit take back random

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

Verzia zo dňa 19.02.2025. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

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