MCPcopy Create free account
hub / github.com/XIU2/UserScript / blockKeywords_comment

Function blockKeywords_comment

Zhihu-Enhanced.user.js:1006–1047  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1004
1005
1006 function blockKeywords_comment() {
1007 function filterComment(comment) {
1008 let content = comment.querySelector('.CommentContent'); // 寻找评论文字所在元素
1009 let text = content.textContent.toLowerCase(); // 全部转为小写(用来不区分大小写)
1010 content.querySelectorAll('img.sticker[alt]').forEach((img)=>{text += img.alt}) // 将评论中的表情添加到待遍历的评论文字中
1011
1012 let keywords = menu_value('menu_customBlockKeywords');
1013 for (const keyword of keywords) { // 遍历关键词黑名单
1014 if (keyword != '' && text.indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该评论
1015 console.log('已屏蔽评论:' + text);
1016 content.dataset.text = content.innerHTML
1017 content.onclick = (e)=>{if (e.target.dataset.text) {e.target.innerHTML = e.target.dataset.text;e.target.removeAttribute('data-text');}}
1018 content.textContent = '[该评论已屏蔽,可点击显示]';
1019 }
1020 }
1021 }
1022
1023 const callback = (mutationsList, observer) => {
1024 for (const mutation of mutationsList) {
1025 for (const target of mutation.addedNodes) {
1026 if (target.nodeType != 1) return
1027 //console.log(target);
1028 if (target.tagName == 'DIV' && target.className.indexOf('css-') == 0 && target.dataset.id == undefined) {
1029 let item = target.querySelector('a[href^="https://www.zhihu.com/people/"]>img.Avatar[alt][loading]')
1030 if (item) {
1031 //console.log(item)
1032 filterComment(item.parentElement.parentElement.parentElement.parentElement)
1033 }
1034 }
1035
1036 /*if (target.tagName == 'DIV' && target.dataset.id !== undefined) {
1037 console.log(target);
1038 for (const node of target.querySelectorAll('*')) {
1039 if (node.className === 'CommentItemV2-metaSibling') filterComment(node);
1040 }
1041 }*/
1042 }
1043 }
1044 };
1045 const observer = new MutationObserver(callback);
1046 observer.observe(document, { childList: true, subtree: true });
1047 }
1048
1049 function blockKeywords_1(item1, css) {
1050 let item = item1.querySelector(css); // 标题所在元素

Callers 1

blockKeywordsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected