Greasy Fork is available in English.

Reddit Random Subreddit

buttons for random subreddit

  1. // ==UserScript==
  2. // @name Reddit Random Subreddit
  3. // @namespace https://greasyfork.org/users/821661
  4. // @description buttons for random subreddit
  5. // @match https://www.reddit.com/*
  6. // @match https://new.reddit.com/*
  7. // @grant GM_addElement
  8. // @grant GM_addStyle
  9. // @grant GM_xmlhttpRequest
  10. // @version 1.5
  11. // @author hdyzen
  12. // @license GPL-3.0
  13. // ==/UserScript==
  14. 'use strict';
  15.  
  16. 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)}`);
  17. const buttons = GM_addElement(document.body, 'div', {
  18. class: 'random-container',
  19. });
  20. buttons.innerHTML = `<a class="rand-button" data-path="/r/random">Random</a><a class="rand-button" data-path="/r/randnsfw">RandNSFW</a>`;
  21.  
  22. function requestRandom(link) {
  23. GM_xmlhttpRequest({
  24. url: `https://www.reddit.com${link.dataset.path}`,
  25. onload: e => {
  26. link.href = e.finalUrl.split('?')[0];
  27. },
  28. });
  29. }
  30.  
  31. function updateHrefs(element) {
  32. [...element.children].forEach(requestRandom);
  33. }
  34.  
  35. buttons.addEventListener('mouseenter', e => {
  36. updateHrefs(buttons);
  37. });
  38.  
  39. buttons.addEventListener('mouseout', e => {
  40. if (e.target.nodeName === 'A') requestRandom(e.target);
  41. });
  42.  
  43. buttons.addEventListener('auxclick', ({ target, button }) => {
  44. if (target.nodeName === 'A' && button === 1) requestRandom(target);
  45. });
  46.  
  47. buttons.addEventListener('click', ({ target }) => {
  48. if (target.nodeName === 'A') requestRandom(target);
  49. });