Restore Random Subreddit Feature

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Restore Random Subreddit Feature
// @namespace    http://tampermonkey.net/
// @version      0.8
// @description  Leverages the API used by redditrand.com to redirect to a random subreddit.
// @author       CheatFreak, PXA
// @match        *://*.reddit.com/*
// @grant        GM.xmlHttpRequest
// @run-at       document-start
// @connect      api.redditrand.com
// @license      MIT
// ==/UserScript==

(function() {
  'use strict';

  function goRandom(nsfw) {
    GM.xmlHttpRequest({
      method: 'GET',
      url: `https://api.redditrand.com/reddit-runner/rand?nsfw=${nsfw}`,
      onload: response => {
        const result = JSON.parse(response.responseText);
        window.location.href = `${window.location.origin}${result.url}`;
      }
    });
  }

  function replaceLinks() {
    const links = document.querySelectorAll('a');
    links.forEach(link => {
      if (link.href.includes('/r/random')) {
        link.href = 'javascript:void(0)';
        link.addEventListener('click', () => goRandom(0));
      } else if (link.href.includes('/r/randnsfw')) {
        link.href = 'javascript:void(0)';
        link.addEventListener('click', () => goRandom(1));
      }
    });
  }

  document.addEventListener('DOMContentLoaded', replaceLinks);

  if (window.location.pathname.startsWith('/r/random')) {
    goRandom(0);
  } else if (window.location.pathname.startsWith('/r/randnsfw')) {
    goRandom(1);
  }
})();