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 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();