Add Link To Remove Old/Crowded Reddit Posts

Add a link on Reddit tab bar to remove posts on front page or subreddit in N number of pages which are older than N hours or has more than N number of comments.

  1. // ==UserScript==
  2. // @name Add Link To Remove Old/Crowded Reddit Posts
  3. // @namespace AddLinkToRemoveOldCrowdedRedditPosts
  4. // @description Add a link on Reddit tab bar to remove posts on front page or subreddit in N number of pages which are older than N hours or has more than N number of comments.
  5. // @include https://www.reddit.com/*
  6. // @version 1.1.1
  7. // @author jcunews
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11.  
  12. //increase this wait time if subsequent page processing didn't work.
  13. //for proper use, after the page has been loaded, the link won't show after before period.
  14. //i.e. when network is slow
  15. var networkWaitTime = 500; //time in milliseconds
  16.  
  17.  
  18. setTimeout(function() {
  19. if (!document.querySelector(".content > .spacer > .sitetable .hide-button")) return;
  20. var ele = document.createElement("SCRIPT");
  21. ele.innerHTML = "(" + (function() {
  22.  
  23. //*** settings start ***
  24. var maxAge = 17; //in hours
  25. var maxComments = 1000;
  26. var maxNumPages = 2;
  27. //*** settings end ***
  28.  
  29. //initialize variables
  30. var config = JSON.parse(sessionStorage.removeOldCrowdedPosts || "{}"), index = -1;
  31. var postsToHide, link, indicatorCurtain, indicatorMessagePanel;
  32. config.pageCount = config.pageCount || 0;
  33. //prepare posts removal indicator message
  34. indicatorCurtain = document.createElement("DIV");
  35. indicatorCurtain.style.cssText = "position: fixed; z-index: 99; left: 0; top: 0; right: 0; bottom: 0; opacity: 0.66; background: black";
  36. indicatorMessagePanel = document.createElement("DIV");
  37. indicatorMessagePanel.style.cssText = "position: fixed; z-index: 100; left: 30%; top: 40%; right: 30%; background: red; color: white; text-align: center; line-height: 3em; font-size: 20pt; font-weight: bold";
  38. indicatorMessagePanel.textContent = "Hiding posts...";
  39.  
  40. //add the link if not already added
  41. function addLink() {
  42. var tabmenu = document.querySelector("#header-bottom-left .tabmenu");
  43. if (!tabmenu || link) return;
  44. link = document.createElement("A");
  45. link.textContent = "Remove Old/Crowded";
  46. link.style.marginLeft = "3ex"
  47. link.href = "javascript:void(0)";
  48. link.onclick = startRemovePosts;
  49. tabmenu.appendChild(link);
  50. }
  51.  
  52. //check whether to process next page or not
  53. function checkForNextPage() {
  54. config.pageCount--;
  55. sessionStorage.removeOldCrowdedPosts = JSON.stringify(config);
  56. if (config.pageCount) {
  57. //there are more pages to process. click the next-page link
  58. var link = document.querySelector(".sitetable .next-button a");
  59. if (link) {
  60. link.click();
  61. }
  62. } else {
  63. //no more page to process. add the link
  64. addLink();
  65. document.body.removeChild(indicatorMessagePanel);
  66. document.body.removeChild(indicatorCurtain);
  67. }
  68. }
  69.  
  70. //remove the posts
  71. function removePosts() {
  72. var posts = document.querySelectorAll(".content > .spacer > .sitetable > .thing");
  73. var time = (new Date()).valueOf(), maxAgeMs = maxAge*3600000;
  74. var i, postTime, comments, link;
  75.  
  76. //add matching posts in current page into hide-list
  77. postsToHide = [];
  78. for (i = posts.length-1; i >= 0; i--) {
  79. //get post's time
  80. postTime = parseInt(posts[i].getAttribute("data-timestamp"));
  81. //get post's number of comments
  82. comments = parseInt(posts[i].querySelector(".comments").textContent.match(/\d+/)[0]);
  83. //main decision
  84. if (((time-postTime) > maxAgeMs) || (comments > maxComments)) {
  85. //add post's hide link into hide-list
  86. link = posts[i].querySelector(".hide-button a");
  87. if (link) {
  88. postsToHide.push(link);
  89. }
  90. }
  91. }
  92. if (postsToHide.length) {
  93. //has post(s) to hide. show posts removal indicator message
  94. document.body.appendChild(indicatorCurtain);
  95. document.body.appendChild(indicatorMessagePanel);
  96. //hide the first post in the hide-list
  97. postsToHide.splice(0, 1)[0].click();
  98. } else {
  99. //no post to hide. check for next page
  100. checkForNextPage();
  101. }
  102. }
  103.  
  104. //start removing posts
  105. function startRemovePosts() {
  106. if (!navigator.onLine) {
  107. alert("Web browser is offline.");
  108. return;
  109. }
  110. config.pageCount = maxNumPages;
  111. sessionStorage.removeOldCrowdedPosts = JSON.stringify(config);
  112. removePosts();
  113. }
  114.  
  115. //setup network-post callback
  116. window._change_state = window.change_state;
  117. window.change_state = function(e, t, n, i, s) {
  118. var _s = s, link;
  119. if ((t === "hide") && (n === window.hide_thing)) {
  120. s = function() {
  121. if (postsToHide.length) {
  122. //has more post(s) to hide. hide the next one in the hide-list
  123. postsToHide.splice(0, 1)[0].click();
  124. } else {
  125. //all hide network requests has completed. check for next page
  126. checkForNextPage();
  127. }
  128. if (_s) {
  129. return _s.apply(this, arguments);
  130. }
  131. };
  132. }
  133. return window._change_state(e, t, n, i, s);
  134. };
  135.  
  136. //check whether posts removal is in effect
  137. if (config.pageCount) {
  138. //posts removal is in effect. process it
  139. removePosts();
  140. } else {
  141. //posts removal is not in effect, or no more pages to process. add the link
  142. addLink();
  143. }
  144.  
  145. }).toString() + ")()";
  146. document.head.appendChild(ele);
  147. }, networkWaitTime);