BlockKeywordsByCnBeta

在CB中拦截指定关键词的文章

目前為 2019-03-27 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name BlockKeywordsByCnBeta
// @namespace Violentmonkey Scripts
// @match https://www.cnbeta.com/
// @grant none
// @description 在CB中拦截指定关键词的文章
// @version 0.0.1.20190327033443
// ==/UserScript==


// 拦截包含以下关键词的文章
const keywords = ["华为", "苹果", "三星", "小米", "环球时报"];

var keywordsRegs = [];
keywords.forEach(k => {
  keywordsRegs.push(new RegExp(k));
});

window.addEventListener("load", () => {
  var targetNode = document.querySelector(".items-area"); // 文章节点容器
  var config = { childList: true };

  var remove = () => {
    let list = targetNode.querySelectorAll(".item"); // 文章节点列表
    list.forEach(item => {
      let block = false;
      keywordsRegs.forEach(r => {
        if (r.test(item.textContent)) {
          block = true;
        }
      });
      if (block) {
        targetNode.removeChild(item);
      }
    });
    observer.observe(targetNode, config);
  };
  var callback = function(mutationsList, observer) {
    for (var mutation of mutationsList) {
      if (mutation.type == "childList") {
        console.log("A child node has been added or removed.");
        observer.disconnect(); // 终止监听
        remove();
      }
    }
  };
  var observer = new MutationObserver(callback);
  remove();
});