Reddit take back random

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

2025-02-19 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

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