From 0f31217c0c6808c37c69ff780d00fe0ccf403f96 Mon Sep 17 00:00:00 2001 From: Rcmcpe Date: Fri, 30 Jul 2021 07:25:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20[=E5=B1=8F=E8=94=BD?= =?UTF-8?q?=E5=85=B3=E9=94=AE=E8=AF=8D]=20=E5=8A=9F=E8=83=BD=E5=AF=B9?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E7=9A=84=E6=94=AF=E6=8C=81=20(#59)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加屏蔽评论功能 * 支持通知等其他地方的评论屏蔽 --- Zhihu-Enhanced.user.js | 51 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/Zhihu-Enhanced.user.js b/Zhihu-Enhanced.user.js index 28d0a1f..c3e1e6c 100644 --- a/Zhihu-Enhanced.user.js +++ b/Zhihu-Enhanced.user.js @@ -483,6 +483,9 @@ function blockKeywords(type) { case 'search': blockKeywords_search(); break; + case 'comment': + blockKeywords_comment(); + break; } function blockKeywords_index_() { @@ -630,6 +633,52 @@ function blockKeywords(type) { } document.addEventListener('DOMNodeInserted', blockKeywords); // 监听插入事件 } + + + function blockKeywords_comment() { + function filterComment(comment) { + let content = comment.querySelector('.RichText'); + let texts = [content.textContent.toLowerCase()]; + for (let i = 0; i < content.children.length; i++) { + let emoticonValue = content.children[i].getAttribute('data-zhihu-emoticon'); + if (emoticonValue) { + texts.push(emoticonValue) + } + } + + let keywords = menu_value('menu_customBlockKeywords'); + for (const text of texts) { + for (const keyword of keywords) { // 遍历关键词黑名单 + if (text.indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该信息流 + console.log(text); + content.textContent = '[已屏蔽]'; + break; + } + } + } + } + function blockKeywords(node) { + switch (node.className) { + case 'NestComment--rootComment': + case 'NestComment--child': + case 'CommentItemV2': + filterComment(node); + break; + } + } + + const callback = (mutationsList, observer) => { + for (const mutation of mutationsList) { + for (const target of mutation.addedNodes) { + for (const node of target.querySelectorAll('*')) { + blockKeywords(node); + } + } + } + }; + const observer = new MutationObserver(callback); + observer.observe(document, { childList: true, subtree: true }); + } } @@ -992,12 +1041,12 @@ function questionInvitation(){ }); } - (function() { addEventListener_DOMNodeInserted(); // 监听 网页插入元素 事件 questionInvitation(); // 默认折叠邀请 setInterval(originalPic,100); // 默认高清原图 if (menu_value('menu_directLink')) setInterval(directLink, 100); // 默认站外直链 + blockKeywords('comment'); // 评论屏蔽指定关键词 if (window.location.href.indexOf("question") > -1) { // 回答页 // if (window.location.href.indexOf("waiting") == -1) {