Reddit Alwayshello Random

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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 = "#";
        }
    }

})();