Add Link To Remove Old/Crowded Reddit Posts

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

当前为 2017-02-04 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Add Link To Remove Old/Crowded Reddit Posts
// @namespace   AddLinkToRemoveOldCrowdedRedditPosts
// @description Add a link on Reddit tab bar to remove posts on front page or subreddit which are older than N hours or has more than N number of comments.
// @include     https://www.reddit.com/*
// @version     1
// @author      jcunews
// @grant       none
// ==/UserScript==

(function() {
  //*** settings start ***
  var maxAge      = 10;   //in hours
  var maxComments = 500;
  //*** settings end ***

  //remove the posts
  function removePosts() {
    var posts = document.querySelectorAll(".content > .spacer > .sitetable > .thing");
    var time = (new Date()).valueOf(), maxAgeMs = maxAge*3600000, i, postTime, comments, link;
    for (i = posts.length-1; i >= 0; i--) {
      //get post's time
      postTime = parseInt(posts[i].getAttribute("data-timestamp"));
      //get post's number of comments
      comments = parseInt(posts[i].querySelector(".comments").textContent.match(/\d+/)[0]);
      //main decision
      if (((time-postTime) > maxAgeMs) || (comments > maxComments)) {
        //click "hide" link
        link = posts[i].querySelector(".hide-button a");
        if (link) {
          link.click();
        }
      }
    }
  }
  
  //add the link
  var tabmenu = document.querySelector("#header-bottom-left .tabmenu"), link;
  if (!tabmenu) return;
  link = document.createElement("A");
  link.textContent = "Remove Old/Crowded";
  link.style.marginLeft = "3ex"
  link.href = "javascript:void(0)";
  link.onclick = removePosts;
  tabmenu.appendChild(link);
})();