Greasy Fork is available in English.

Reddit Random Subreddit

buttons for random subreddit

// ==UserScript==
// @name            Reddit Random Subreddit
// @namespace       https://greasyfork.org/users/821661
// @description     buttons for random subreddit
// @match           https://www.reddit.com/*
// @match           https://new.reddit.com/*
// @grant           GM_addElement
// @grant           GM_addStyle
// @grant           GM_xmlhttpRequest
// @version         1.5
// @author          hdyzen
// @license         GPL-3.0
// ==/UserScript==
'use strict';

let styles = GM_addStyle(`.random-container{position:fixed;top:50px;left:0;transform:translateX(-90%);z-index:99;background-color:rgba(26,26,26,.5);padding:10px 15px;border-radius:4px;display:flex;flex-direction:column;gap:10px;text-align:center;transition:.15s;backdrop-filter:blur(5px);box-shadow:rgba(0,0,0,.35) 0 5px 15px;border-right:2px solid #e84306}.random-container:hover{transform:translateX(0%);}.rand-button{padding:5px;border-radius:4px;box-shadow:inset 0 0 0 0 transparent;transition:.4s;color: #fff;}.rand-button:hover{color:#eabdac;box-shadow:inset 95px 0 0 0 rgba(45,45,46,.8)}`);
const buttons = GM_addElement(document.body, 'div', {
    class: 'random-container',
});
buttons.innerHTML = `<a class="rand-button" data-path="/r/random">Random</a><a class="rand-button" data-path="/r/randnsfw">RandNSFW</a>`;

function requestRandom(link) {
    GM_xmlhttpRequest({
        url: `https://www.reddit.com${link.dataset.path}`,
        onload: e => {
            link.href = e.finalUrl.split('?')[0];
        },
    });
}

function updateHrefs(element) {
    [...element.children].forEach(requestRandom);
}

buttons.addEventListener('mouseenter', e => {
    updateHrefs(buttons);
});

buttons.addEventListener('mouseout', e => {
    if (e.target.nodeName === 'A') requestRandom(e.target);
});

buttons.addEventListener('auxclick', ({ target, button }) => {
    if (target.nodeName === 'A' && button === 1) requestRandom(target);
});

buttons.addEventListener('click', ({ target }) => {
    if (target.nodeName === 'A') requestRandom(target);
});