Jandan user blocker

block Jandan users

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Jandan user blocker
// @namespace    jandan.net
// @name:zh-CN   煎蛋屏蔽用户
// @version      0.1
// @description  block Jandan users
// @description:zh-cn  屏蔽特定用户
// @author       Suzuhana
// @match        http://jandan.net/pic
// @match        http://jandan.net/pic/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const localStorage = window.localStorage;
  let blockList = localStorage.getItem('blockList')
    ? JSON.parse(localStorage.getItem('blockList'))
    : [];
  const commentList = document.querySelector('ol.commentlist');
  const posts = document.querySelectorAll("ol.commentlist li[id^='comment']");
  const postsArr = Array.from(posts);
  const filteredPosts = postsArr.filter((ele) => {
    let shouldBeFiltered = false;

    blockList
      .map((ele) => ele.userId)
      .forEach((blockedId) => {
        if (
          ele
            .querySelector('div.author > strong')
            .getAttribute('title')
            .includes(blockedId)
        ) {
          shouldBeFiltered = true;
        }
      });

    return !shouldBeFiltered;
  });

  //empty nodeList and repopulate with filtered comments
  commentList.innerHTML = '';

  filteredPosts.forEach((ele) => {
    //Grab user name and user id;
    const userNode = ele.querySelector('div.author > strong');

    const userName = userNode.innerText;
    const userId = userNode.getAttribute('title').split(':')[1];

    //Create a Button for blocking user
    const blockButton = document.createElement('BUTTON');
    blockButton.innerHTML = 'Block this User';
    blockButton.addEventListener('click', () => {
      blockList.push({
        userId,
        userName,
      });
      localStorage.setItem('blockList', JSON.stringify(blockList));
      //TODO: could be replaced with better option
      location.reload();
    });

    ele.querySelector('div.author').appendChild(blockButton);
    commentList.appendChild(ele);
  });

  //reset blocklist
  const resetButton = document.createElement('BUTTON');
  resetButton.innerHTML = 'Release the trolls';
  resetButton.addEventListener('click', () => {
    blockList = [];
    localStorage.removeItem('blockList');
    //TODO: could be replaced with better option
    location.reload();
  });
  resetButton.style = 'float: right';

  document.querySelector('div.cp-pagenavi').appendChild(resetButton);
})();