知乎摸鱼脚本

知乎摸鱼脚本分享,关闭右侧栏、添加自定义黑名单关键词、过滤争议话题、过滤**视频

// ==UserScript==
// @name        知乎摸鱼脚本 - zhihu.com
// @namespace   Violentmonkey Scripts
// @match       https://www.zhihu.com/*
// @grant       none
// @version     1.1.0
// @author      JackyChou (Original Author)
// @author      Cola (Contributor)
// @license     GPL License
// @supportURL  https://greasyfork.org/zh-CN/users/986002
// @homepageURL https://greasyfork.org/zh-CN/users/986002
// @namespace   https://greasyfork.org/zh-CN/users/986002
// @description 版本更新:2022/11/28 17:36:07;版本更新:2024/11/13 18:49:40 添加过滤规则:过滤傻逼视频和争议话题。
// @downloadURL https://update.greasyfork.org/scripts/455568/%E7%9F%A5%E4%B9%8E-%E5%85%B3%E9%97%AD%E5%8F%B3%E4%BE%A7%E6%A0%8F%20-%20zhihucom.user.js
// @updateURL https://update.greasyfork.org/scripts/455568/%E7%9F%A5%E4%B9%8E-%E5%85%B3%E9%97%AD%E5%8F%B3%E4%BE%A7%E6%A0%8F%20-%20zhihucom.meta.js
// ==/UserScript==
(function () {
  'use strict';
// 黑名单关键词
  const blacklist = ["媒体", "开封", "骑行", "干部", "朝鲜", "华为", "对立", "爱国", "马云", "考研", "考公", "考编", "以色列", "大数据", "网红", "官", "苏联",
  "央视","鸿蒙","张宇","释放","创业","直播","北航","清华"];

  const changeNode = () => {
    let contentNode = document.getElementsByClassName('Topstory-container');
    if (!contentNode.length) contentNode = document.getElementsByClassName('Question-main');
    if (contentNode.length) {
      const leftNode = contentNode[0].children[0];
      const rightNode = contentNode[0].children[1];
      rightNode.style.display = 'none';
      leftNode.style.width = '100%';
      leftNode.style.maxWidth = '100%';
      const mainColumnNode = document.getElementsByClassName('Question-mainColumn')[0];
      if (mainColumnNode) mainColumnNode.style.width = '100%';
    }

// 处理视频元素,隐藏父级标签
    hideVideosAndParents();
  };

  const hideVideosAndParents = () => {
// 找到视频元素
    const videoItems = document.getElementsByClassName('ZVideoItem-video');
    const answerPlayers = document.getElementsByClassName('VideoAnswerPlayer');

// 隐藏视频的父级标签
    for (let i = 0; i < videoItems.length; i++) {
      const parentCard = videoItems[i].closest('.Card.TopstoryItem.TopstoryItem-isRecommend');
      if (parentCard) parentCard.style.display = 'none';
    }

    for (let i = 0; i < answerPlayers.length; i++) {
      const parentCard = answerPlayers[i].closest('.Card.TopstoryItem.TopstoryItem-isRecommend');
      if (parentCard) parentCard.style.display = 'none';
    }
  };

// 关键词过滤并隐藏父级标签
  const filterBlacklistContent = () => {
    document.querySelectorAll('div.ContentItem.AnswerItem, div.ContentItem.ArticleItem').forEach((contentItem) => {
      const titleElement = contentItem.querySelector('h2.ContentItem-title span a, .ContentItem-title a');
      if (titleElement) {
        const titleText = titleElement.textContent;
        if (blacklist.some(keyword => titleText.includes(keyword))) {
          const parentCard = contentItem.closest('.Card.TopstoryItem.TopstoryItem-isRecommend');
          if (parentCard) parentCard.style.display = 'none';
        }
      }
    });
  };

// 执行初始隐藏
  changeNode();
  filterBlacklistContent();

// 监听 DOM 变化,动态隐藏
  const observer = new MutationObserver(() => {
    hideVideosAndParents();
    filterBlacklistContent();
  });

  observer.observe(document.body, {
    childList: true,
    subtree: true
  });
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
最后一次更新于: 2024/11/13, 21:45:19