Comments killfile for Cyanide & Happiness

Removes comments by users that I don't care to read

目前為 2016-07-29 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Comments killfile for Cyanide & Happiness
// @namespace   https://pineight.com/
// @description Removes comments by users that I don't care to read
// @include     http://explosm.net/shorts/*
// @include     http://explosm.net/comics/*
// @version     1
// @grant    GM_addStyle
// @grant    GM_getValue
// @grant    GM_setValue
// @noframes
// ==/UserScript==

(function () {
  var trim = (function () {
    var trimRE = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
    return function(s) {
      return s.replace(trimRE, '');
    };
  })();

  var read_users_from_string = (function(s) {
    var kfllines = s.split("\n");
    var r = {};
    for (key in kfllines) {
      var line = trim(kfllines[key]);
      if (line !== '') r[line] = true;
    }
    return r;
  });

  var read_users_from_textarea = (function() {
    var kfl = document.getElementById('pino_killfile_list');
    return read_users_from_string(kfl.value);
  });

  var go = (function () {
    console.log("Applying killfile");
    kfset = read_users_from_textarea();
    console.log("to "+Object.keys(kfset));

    var cws = document.querySelectorAll(".comment-wrapper");
    for (var i = 0; i < cws.length; ++i) {
      var cw = cws[i];
      var cuspan = cw.querySelector(".comment-username");
      if (!cuspan) continue;
      var username = trim(cuspan.firstChild.textContent);
      var in_kfset = Object.prototype.hasOwnProperty.call(kfset, username);
      if (!in_kfset) continue;
      cw.innerHTML = "";
      cw.textContent = "(Ignoring comment by "+username+")";
    }
    var usersaslines = Object.keys(kfset).join("\n");
    if (GM_setValue) GM_setValue("kf_users", usersaslines);
  });

  var install = (function () {
    var jstr = document.querySelector("a.js-toggle-replies");
    if (!jstr) return false;

    var userslist = GM_getValue && GM_getValue("kf_users");
    var users = userslist ? read_users_from_string(userslist) : {};
    var kflwrap = document.createElement('div');
    var usersaslines = Object.keys(users).join("\n");

    kflwrap.setAttribute('id', 'pino_killfile_list_wrapper');
    kflwrap.innerHTML = '<a id="pino_killfile_button" href="#pino_killfile_button">Hide comments</a> by these users:<br><textarea cols="30" rows="5" id="pino_killfile_list"></textarea>';
    jstr.parentNode.appendChild(kflwrap);
    kfl = document.getElementById('pino_killfile_list');
    kfl.textContent = usersaslines;
    kfbutton = document.getElementById('pino_killfile_button');
    kfbutton.addEventListener("click", go, false);
    console.log("Killfile loaded");
    return kfbutton;
  });

  var waitinstall = (function () {
    if (!install()) {
      console.log("Killfile by PinoBatch will be installed once comments load");
      window.setTimeout(waitinstall, 10000);
    }
  })
  waitinstall();
})();