Add Reddit Post Report Link For Mods

Add a report link on each Reddit post (for Mods). The report link will report the post using "Other" as the reason and without any additional comment. This is roughly the same as the old style of the report link. i.e. without any menus.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Add Reddit Post Report Link For Mods
// @namespace    https://greasyfork.org/en/users/85671-jcunews
// @version      1.0.2
// @license      GNU AGPLv3
// @description  Add a report link on each Reddit post (for Mods). The report link will report the post using "Other" as the reason and without any additional comment. This is roughly the same as the old style of the report link. i.e. without any menus.
// @author       jcunews
// @match        *://www.reddit.com/*
// @grant        none
// ==/UserScript==

//Time delay before reshowing the report link after the previous report attemp has failed (after showing the "report failed" message).
var linkReshowDelay = 3000; //in milliseconds. 1000ms = 1 second.

(function(reportLink) {
  function reportLinkClick(ev, link, tid, xhr) {
    link = this;
    tid = $(link).thing()[0].id.substr(6);
    xhr = new XMLHttpRequest();
    xhr.onload = function(resp, z) {
      try {
        resp = JSON.parse(xhr.responseText);
        if (resp.success) {
          link.nextElementSibling.innerHTML = "reported";
        } else throw 0;
      } catch(z) {
        xhr.onerror();
      }
    };
    xhr.onerror = function() {
      link.nextElementSibling = "report failed";
      setTimeout(function(link) {
        link.style.display = "";
        link.nextElementSibling.innerHTML = "";
      }, linkReshowDelay, link);
    };
    xhr.open("POST", "https://www.reddit.com/api/report", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xhr.send("thing_id=" + tid + "&reason=other&other_reason=&id=%23report-action-form&r=" + r.config.post_site + "&uh=" + r.config.modhash + "&renderstyle=html");
    link.style.display = "none";
    link.nextElementSibling.innerHTML = "reporting...";
    ev.preventDefault();
  }
  reportLink = document.createElement("LI");
  reportLink.innerHTML = '<a href="javascript:void(0)">report</a><span></span>';
  document.querySelectorAll(".thing .lock-button").forEach(
    function(ele, linkContainer) {
      linkContainer = reportLink.cloneNode(true);
      linkContainer.firstElementChild.addEventListener("click", reportLinkClick);
      ele = ele.parentNode;
      ele.parentNode.insertBefore(linkContainer, ele.parentNode.querySelector(".crosspost-button").nextElementSibling);
    }
  );
})();