新增 [屏蔽关键词] 功能对评论的支持 (#59)

* 添加屏蔽评论功能

* 支持通知等其他地方的评论屏蔽
This commit is contained in:
Rcmcpe
2021-07-30 07:25:41 +08:00
committed by GitHub
parent d649e170d1
commit 0f31217c0c
+50 -1
View File
@@ -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) {