Reddit Alwayshello Random

Leverages the alwayshello API used by redditrand.com to redirect to a random subreddit.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Reddit Alwayshello Random
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Leverages the alwayshello API used by redditrand.com to redirect to a random subreddit.
// @author       PXA
// @match        *://*.reddit.com/*
// @grant        GM.xmlHttpRequest
// @run-at       document-idle
// @connect      api.alwayshello.com
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    async function goRandom(link) {
        const nsfw = link.target.text.toLowerCase().includes("nsfw") ? 1 : 0;
        const r = await GM.xmlHttpRequest({ url: `https://api.alwayshello.com/reddit-runner/rand?nsfw=${nsfw}` }).catch(e => console.error(e));
        window.location.href = `${window.location.origin}${JSON.parse(r.responseText).url}`;
    }

    const anchors = document.querySelectorAll('a.subbarlink');
    for (let i = 0; i < anchors.length; i++) {
        if (['/r/random/', '/r/randnsfw/'].includes(anchors[i].attributes.href.value)) {
            anchors[i].onclick = goRandom;
            anchors[i].href = "#";
        }
    }

})();