Reddit Hide All Posts & Reload Hotkey

Hide all posts & reload page on Ctrl+Shift+R; cancel with Escape

  1. // ==UserScript==
  2. // @name Reddit Hide All Posts & Reload Hotkey
  3. // @namespace redditposthideandreload_kk
  4. // @version 1.5
  5. // @description Hide all posts & reload page on Ctrl+Shift+R; cancel with Escape
  6. // @match https://*.reddit.com/*
  7. // @grant none
  8. // @run-at document-start
  9. // ==/UserScript==
  10.  
  11. let oldhidebtns;
  12. let newhidebtns;
  13.  
  14. function getbtns() {
  15. oldhidebtns = document.getElementsByTagName("a");
  16. newhidebtns = document.getElementsByClassName("icon-hide");
  17. }
  18.  
  19. let wait = ms => new Promise(resolve => setTimeout(resolve, ms));
  20. let waitTime = 500;
  21. let canReload = true;
  22.  
  23. async function doHide() {
  24. getbtns();
  25.  
  26. try {
  27. for (let i = 0; i < oldhidebtns.length; i++) {
  28. if (!canReload) return;
  29.  
  30. if (oldhidebtns[i].innerText.toLowerCase() === "hide") {
  31. oldhidebtns[i].click();
  32. await wait(waitTime);
  33. }
  34. }
  35.  
  36. let didNewBtnRun = false;
  37.  
  38. for (let i = 1; i < newhidebtns.length; i++) {
  39. if (!canReload) return;
  40. i--;
  41.  
  42. newhidebtns[i].click();
  43. didNewBtnRun = true;
  44. await wait(waitTime);
  45. }
  46.  
  47. if (didNewBtnRun) return doHide();
  48.  
  49. await wait(waitTime + 1000);
  50.  
  51. if (!canReload) return;
  52.  
  53. location.reload();
  54. }
  55. catch (e) {
  56. console.warn(e);
  57. doHide();
  58. }
  59. }
  60.  
  61. document.addEventListener("keydown", (e) => {
  62. if (e.ctrlKey && e.shiftKey && e.keyCode == 82) {
  63. e.preventDefault();
  64. canReload = true;
  65. doHide();
  66. }
  67. if (e.key.toLowerCase() === "escape" || e.code.toLowerCase() === "escape") {
  68. e.preventDefault();
  69. canReload = false;
  70. }
  71. }, false);
  72.