Skip to content

Commit 4709280

Browse files
committed
优化 [屏蔽指定关键词] 功能(首页和搜索结果遗漏问题)
1 parent bf3d934 commit 4709280

1 file changed

Lines changed: 54 additions & 28 deletions

File tree

Zhihu-Enhanced.user.js

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 知乎增强
3-
// @version 1.4.5
3+
// @version 1.4.6
44
// @author X.I.U
55
// @description 移除登录弹窗、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽指定用户、屏蔽指定关键词(标题)、屏蔽盐选内容、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链
66
// @match *://www.zhihu.com/*
@@ -372,6 +372,7 @@ function blockKeywords(type) {
372372
if (!menu_value('menu_customBlockKeywords') || menu_value('menu_customBlockKeywords').length < 1) return
373373
switch(type) {
374374
case 'index':
375+
blockKeywords_index_();
375376
blockKeywords_index();
376377
break;
377378
case 'topic':
@@ -386,6 +387,38 @@ function blockKeywords(type) {
386387
break;
387388
}
388389

390+
function blockKeywords_index_() {
391+
let item = document.querySelectorAll('h2.ContentItem-title meta[itemprop="name"]'); // 标题所在元素
392+
if (item.length > 0) {
393+
item.forEach(function(item2){
394+
//console.log(item2)
395+
menu_value('menu_customBlockKeywords').forEach(function(item1){ // 遍历关键词黑名单
396+
if (item2.content.toLowerCase().indexOf(item1.toLowerCase()) > -1) { // 找到就删除该信息流
397+
console.log(item2.content);
398+
if (item2.parentNode.parentNode.parentNode.parentNode.parentNode.className === 'Feed') {
399+
item2.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.remove();
400+
} else {
401+
item2.parentNode.parentNode.parentNode.parentNode.parentNode.remove();
402+
}
403+
}
404+
})
405+
})
406+
}
407+
// 如果是文章标题
408+
item = document.querySelectorAll('.ContentItem.ArticleItem meta[itemprop="headline"]'); // 标题所在元素
409+
if (item.length > 0) {
410+
item.forEach(function(item2){
411+
//console.log(item2)
412+
menu_value('menu_customBlockKeywords').forEach(function(item1){ // 遍历关键词黑名单
413+
if (item2.content.toLowerCase().indexOf(item1.toLowerCase()) > -1) { // 找到就删除该信息流
414+
console.log('文章',item2.content);
415+
item2.parentNode.parentNode.parentNode.remove();
416+
}
417+
})
418+
})
419+
}
420+
}
421+
389422
function blockKeywords_index() {
390423
let blockKeywords = e => {
391424
if (e.target.innerHTML && e.target.getElementsByClassName('ContentItem-title').length > 0) {
@@ -395,7 +428,11 @@ function blockKeywords(type) {
395428
menu_value('menu_customBlockKeywords').forEach(function(item1){ // 遍历关键词黑名单
396429
if (item.content.toLowerCase().indexOf(item1.toLowerCase()) > -1) { // 找到就删除该信息流
397430
console.log(item.content);
398-
item.parentNode.parentNode.parentNode.parentNode.parentNode.remove();
431+
if (item.parentNode.parentNode.parentNode.parentNode.parentNode.className === 'Feed') {
432+
item.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.remove();
433+
} else {
434+
item.parentNode.parentNode.parentNode.parentNode.parentNode.remove();
435+
}
399436
}
400437
})
401438
} else { // 如果是文章标题
@@ -428,19 +465,19 @@ function blockKeywords(type) {
428465
}
429466
})
430467
})
431-
} else { // 如果是文章标题
432-
item = document.querySelectorAll('.ContentItem.ArticleItem meta[itemprop="headline"]'); // 标题所在元素
433-
if (item.length > 0) {
434-
//console.log(item)
435-
item.forEach(function(item2){
436-
menu_value('menu_customBlockKeywords').forEach(function(item1){ // 遍历关键词黑名单
437-
if (item2.content.toLowerCase().indexOf(item1.toLowerCase()) > -1) { // 找到就删除该信息流
438-
console.log('文章',item2.content);
439-
item2.parentNode.parentNode.remove();
440-
}
441-
})
468+
}
469+
// 如果是文章标题
470+
item = document.querySelectorAll('.ContentItem.ArticleItem meta[itemprop="headline"]'); // 标题所在元素
471+
if (item.length > 0) {
472+
//console.log(item)
473+
item.forEach(function(item2){
474+
menu_value('menu_customBlockKeywords').forEach(function(item1){ // 遍历关键词黑名单
475+
if (item2.content.toLowerCase().indexOf(item1.toLowerCase()) > -1) { // 找到就删除该信息流
476+
console.log('文章',item2.content);
477+
item2.parentNode.parentNode.remove();
478+
}
442479
})
443-
}
480+
})
444481
}
445482
}
446483

@@ -481,26 +518,15 @@ function blockKeywords(type) {
481518
//if (e.target.innerHTML) console.log(e.target.innerHTML)
482519
if (e.target.innerHTML && e.target.getElementsByClassName('ContentItem-title').length > 0) {
483520
//console.log(e.target.innerHTML)
484-
let item = e.target.querySelector('h2.ContentItem-title meta[itemprop="name"]'); // 标题所在元素
521+
let item = e.target.querySelector('h2.ContentItem-title a[data-za-detail-view-id]'); // 标题所在元素
485522
if (item) {
486523
//console.log(item)
487524
menu_value('menu_customBlockKeywords').forEach(function(item1){ // 遍历关键词黑名单
488-
if (item.content.toLowerCase().indexOf(item1.toLowerCase()) > -1) { // 找到就删除该信息流
489-
console.log(item.content);
525+
if (item.innerText.toLowerCase().indexOf(item1.toLowerCase()) > -1) { // 找到就删除该信息流
526+
console.log(item.innerText);
490527
item.parentNode.parentNode.parentNode.parentNode.parentNode.remove();
491528
}
492529
})
493-
} else { // 如果是文章标题
494-
item = e.target.querySelector('h2.ContentItem-title a[data-za-detail-view-id]'); // 标题所在元素
495-
if (item) {
496-
//console.log(item)
497-
menu_value('menu_customBlockKeywords').forEach(function(item1){ // 遍历关键词黑名单
498-
if (item.innerText.toLowerCase().indexOf(item1.toLowerCase()) > -1) { // 找到就删除该信息流
499-
console.log('文章',item.content);
500-
item.parentNode.parentNode.parentNode.parentNode.parentNode.remove();
501-
}
502-
})
503-
}
504530
}
505531
}
506532
}

0 commit comments

Comments
 (0)