Leverages the redditrand API used by redditrand.com to redirect to a random subreddit.
// ==UserScript==
// @name Reddit Alwayshello Random
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Leverages the redditrand 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.redditrand.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.redditrand.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 = "#";
}
}
if (window.location.pathname.startsWith('/r/random')) {
goRandom({target:{text:''}});
} else if (window.location.pathname.startsWith('/r/randnsfw')) {
goRandom({target:{text:'nsfw'}});
}
})();