油猴知乎摸鱼脚本

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

// ==UserScript==
// @name        知乎摸鱼脚本 - zhihu.com
// @namespace   Violentmonkey Scripts
// @match       https://www.zhihu.com/*
// @grant       none
// @version     1.2.0
// @author      JackyChou (Original Author)
// @author      the0n3 (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 版本更新:2025/2/25 添加广告卡片过滤功能;过滤视频和争议话题;隐藏右侧栏
// @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 = ["关键词1","关键词2","关键词3","关键词4","关键词5"];

  // 广告卡片隐藏功能
  const hideAdvertCards = () => {
    document.querySelectorAll('.TopstoryItem--advertCard').forEach(advert => {
      const parentCard = advert.closest('.Card');
      if (parentCard) parentCard.style.display = 'none';
    });
  };

  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();
    hideAdvertCards();
  };

  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();
  hideAdvertCards();

  // 动态监听
  const observer = new MutationObserver(() => {
    hideVideosAndParents();
    filterBlacklistContent();
    hideAdvertCards();
  });

  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
86
87
88
89
90
91
92
93
94
95
96
最后一次更新于: 2025/02/25, 18:52:37