Reddit take back random

this script simulates the "/r/random" removed from reddit

  1. // ==UserScript==
  2. // @name Reddit take back random
  3. // @name:pt-BR Reddit pegue de volta o random
  4. // @namespace https://greasyfork.org/users/821661
  5. // @match https://www.reddit.com/*
  6. // @match https://sh.reddit.com/*
  7. // @match https://old.reddit.com/*
  8. // @grant GM.xmlHttpRequest
  9. // @run-at document-start
  10. // @version 1.1
  11. // @author hdyzen
  12. // @description this script simulates the "/r/random" removed from reddit
  13. // @description:pt-br esse script simula o “/r/random” removido do reddit
  14. // @license GPL-3.0-only
  15. // ==/UserScript==
  16.  
  17. /**
  18. * A reference to the unsafeWindow object, which provides access to the page's window object
  19. * from a userscript. This allows the script to interact with the page's JavaScript context.
  20. *
  21. * @type {Window}
  22. */
  23. const window = unsafeWindow;
  24.  
  25. /**
  26. * The hostname of the current window location.
  27. * @type {string}
  28. */
  29. const domain = window.location.hostname;
  30.  
  31. function init() {
  32. main();
  33. patchConsoleLog();
  34. hideGuardModal();
  35. }
  36. init();
  37.  
  38. /**
  39. * Patches the console.log function to add a custom behavior.
  40. *
  41. * This function overrides the default console.log method to check if the first argument
  42. * is the string "Navigation listeners online". If it is, it adds an event listener to
  43. * the window's navigation object that triggers the `main` function on the "navigate" event.
  44. *
  45. * @function patchConsoleLog
  46. */
  47. function patchConsoleLog() {
  48. const originalLog = window.console.log;
  49.  
  50. window.console.log = function (...args) {
  51. const result = originalLog.apply(this, args);
  52.  
  53. if (args[0] === "Navigation listeners online") {
  54. window.navigation.addEventListener("navigate", e => main());
  55. }
  56.  
  57. return result;
  58. };
  59. }
  60.  
  61. /**
  62. * Hides the guard community modal for specific subreddits.
  63. *
  64. * This function inserts a style element into the document that hides the
  65. * guard community modal for subreddits with the names "random" and "randnsfw".
  66. */
  67. function hideGuardModal() {
  68. document.documentElement.insertAdjacentHTML("beforeend", `<style>guard-community-modal:is([subredditname="random"], [subredditname="randnsfw"]) { display: none !important; }</style>`);
  69. }
  70.  
  71. /**
  72. * Checks if the current page is a random subreddit page.
  73. *
  74. * This function examines the current window's location pathname to determine
  75. * if it matches either "/r/random" or "/r/randnsfw". If it matches, it returns
  76. * the matched subreddit type ("random" or "randnsfw"). Otherwise, it returns null.
  77. *
  78. * @returns {string|null} The type of random subreddit page ("random" or "randnsfw"), or null if not a random page.
  79. */
  80. function isRandomPage() {
  81. const wht = window.location.pathname.match(/^\/r\/([^\/?\s]+)/)?.[1];
  82.  
  83. return wht === "random" || wht === "randnsfw" ? wht : null;
  84. }
  85.  
  86. /**
  87. * Generates a random row number based on the specified type.
  88. *
  89. * @param {string} type - The type of row to generate. Can be 'random' or 'randnsfw'.
  90. * @returns {number} A random row number within the range specified for the given type.
  91. */
  92. function getRandomRow(type) {
  93. const firstRow = {
  94. random: 2,
  95. randnsfw: 290856,
  96. };
  97. const lastRow = 330694;
  98.  
  99. return Math.floor(Math.random() * (lastRow - firstRow[type]) + firstRow[type]);
  100. }
  101.  
  102. /**
  103. * Main function to fetch a random subreddit from a Google Sheets document and redirect the user to that subreddit.
  104. *
  105. * @async
  106. * @function main
  107. * @throws Will log an error to the console if the request fails.
  108. */
  109. async function main() {
  110. try {
  111. const type = isRandomPage();
  112.  
  113. if (!type) {
  114. return;
  115. }
  116.  
  117. const res = await GM.xmlHttpRequest({
  118. url: `https://docs.google.com/spreadsheets/d/1xLFbNcvpdU9j1n2fF8u2n_eGW4OekO-R2JUJb7YZxOE/gviz/tq?tq=select C limit 1 offset ${getRandomRow(type)}`,
  119. });
  120.  
  121. const subreddit = res.response.match(/"v":"(.+?)"/)?.[1];
  122.  
  123. console.log(`Random: ${subreddit}`);
  124.  
  125. if (subreddit) {
  126. window.location.href = `https://${domain}/r/${subreddit}`;
  127. }
  128. } catch (err) {
  129. console.error(err);
  130. }
  131. }