diff --git a/Zhihu-Enhanced.user.js b/Zhihu-Enhanced.user.js index 28d0a1f78..c3e1e6cff 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) {