Skip to content

Commit 4f646b3

Browse files
committed
新增 [屏蔽关键词] 对热榜的支持; 优化 [屏蔽关键词] 功能
1 parent ccac278 commit 4f646b3

1 file changed

Lines changed: 24 additions & 49 deletions

File tree

Zhihu-Enhanced.user.js

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 知乎增强
3-
// @version 1.7.8
3+
// @version 1.7.9
44
// @author X.I.U
55
// @description 移除登录弹窗、屏蔽首页视频、默认收起回答、快捷收起当前回答/评论(左键两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、屏蔽盐选内容、净化标题消息、展开问题描述、置顶显示时间、完整问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链
66
// @match *://www.zhihu.com/*
@@ -727,18 +727,11 @@ function blockKeywords(type) {
727727
function blockKeywords_(className1, className2) {
728728
// 前几条因为是直接加载的,而不是动态插入网页的,所以需要单独判断
729729
function blockKeywords_now() {
730-
document.querySelectorAll(className1).forEach(function(item1){
731-
let item = item1.querySelector('h2.ContentItem-title meta[itemprop="name"], meta[itemprop="headline"]'); // 标题所在元素
732-
if (item) {
733-
for (const keyword of menu_value('menu_customBlockKeywords')) { // 遍历关键词黑名单
734-
if (item.content.toLowerCase().indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该信息流
735-
console.log(item.content);
736-
item1.hidden = true;
737-
break;
738-
}
739-
}
740-
}
741-
})
730+
if (location.pathname === '/hot') {
731+
document.querySelectorAll('.HotItem').forEach(function(item1){blockKeywords_1(item1, 'h2.HotItem-title');})
732+
} else {
733+
document.querySelectorAll(className1).forEach(function(item1){blockKeywords_1(item1, 'h2.ContentItem-title meta[itemprop="name"], meta[itemprop="headline"]');})
734+
}
742735
}
743736

744737
blockKeywords_now();
@@ -751,18 +744,7 @@ function blockKeywords(type) {
751744
for (const mutation of mutationsList) {
752745
for (const target of mutation.addedNodes) {
753746
if (target.nodeType != 1) return
754-
if (target.className === className2) {
755-
let item = target.querySelector('h2.ContentItem-title meta[itemprop="name"], meta[itemprop="headline"]'); // 标题所在元素
756-
if (item) {
757-
for (const keyword of menu_value('menu_customBlockKeywords')) { // 遍历关键词黑名单
758-
if (item.content.toLowerCase().indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该信息流
759-
console.log(item.content);
760-
target.hidden = true;
761-
break;
762-
}
763-
}
764-
}
765-
}
747+
if (target.className === className2) {blockKeywords_1(target, 'h2.ContentItem-title meta[itemprop="name"], meta[itemprop="headline"]');}
766748
}
767749
}
768750
};
@@ -774,18 +756,7 @@ function blockKeywords(type) {
774756
function blockKeywords_search() {
775757
function blockKeywords_now() {
776758
if (location.search.indexOf('type=content') === -1) return // 目前只支持搜索页的 [综合]
777-
document.querySelectorAll('.Card.SearchResult-Card[data-za-detail-view-path-module="AnswerItem"], .Card.SearchResult-Card[data-za-detail-view-path-module="PostItem"]').forEach(function(item1){
778-
let item = item1.querySelector('a[data-za-detail-view-id]'); // 标题所在元素
779-
if (item) {
780-
for (const keyword of menu_value('menu_customBlockKeywords')) { // 遍历关键词黑名单
781-
if (item.textContent.toLowerCase().indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该信息流
782-
console.log(item.textContent);
783-
item1.hidden = true;
784-
break;
785-
}
786-
}
787-
}
788-
})
759+
document.querySelectorAll('.HotLanding-contentItem, .Card.SearchResult-Card[data-za-detail-view-path-module="AnswerItem"], .Card.SearchResult-Card[data-za-detail-view-path-module="PostItem"]').forEach(function(item1){blockKeywords_1(item1, 'a[data-za-detail-view-id]');})
789760
}
790761

791762
setTimeout(blockKeywords_now, 500);
@@ -798,18 +769,7 @@ function blockKeywords(type) {
798769
for (const mutation of mutationsList) {
799770
for (const target of mutation.addedNodes) {
800771
if (target.nodeType != 1) return
801-
if (target.className === 'Card SearchResult-Card' && (target.dataset.zaDetailViewPathModule === 'AnswerItem' || target.dataset.zaDetailViewPathModule === 'PostItem')) {
802-
let item = target.querySelector('a[data-za-detail-view-id]'); // 标题所在元素
803-
if (item) {
804-
for (const keyword of menu_value('menu_customBlockKeywords')) { // 遍历关键词黑名单
805-
if (item.textContent.toLowerCase().indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该信息流
806-
console.log(item.textContent);
807-
target.hidden = true;
808-
break;
809-
}
810-
}
811-
}
812-
}
772+
if (target.className === 'Card SearchResult-Card' && (target.dataset.zaDetailViewPathModule === 'AnswerItem' || target.dataset.zaDetailViewPathModule === 'PostItem')) {blockKeywords_1(target, 'a[data-za-detail-view-id]');}
813773
}
814774
}
815775
};
@@ -854,6 +814,21 @@ function blockKeywords(type) {
854814
const observer = new MutationObserver(callback);
855815
observer.observe(document, { childList: true, subtree: true });
856816
}
817+
818+
function blockKeywords_1(item1, css) {
819+
let item = item1.querySelector(css); // 标题所在元素
820+
if (item) {
821+
for (const keyword of menu_value('menu_customBlockKeywords')) { // 遍历关键词黑名单
822+
let text = item.content || item.textContent;
823+
if (text.toLowerCase().indexOf(keyword.toLowerCase()) > -1) { // 找到就删除该信息流
824+
console.log(text);
825+
item1.hidden = true;
826+
item1.style.display = 'none';
827+
break;
828+
}
829+
}
830+
}
831+
}
857832
}
858833

859834

0 commit comments

Comments
 (0)