屏蔽知乎首页的所有推荐内容和热搜栏的火狐插件
1// 知乎推荐内容屏蔽器
2(function() {
3 'use strict';
4
5 // 屏蔽推荐 feed 中的内容
6 function blockRecommendations() {
7 // 检查是否在首页
8 if (!window.location.pathname.match(/^\/?$|^\/follow$/)) {
9 return;
10 }
11
12 // 获取当前激活的标签
13 const activeTab = document.querySelector('.Topstory-tabs .TopstoryTabs-link.is-active');
14
15 // 如果当前是"推荐"标签,隐藏所有内容
16 if (activeTab && activeTab.textContent.includes('推荐')) {
17 hideRecommendFeed();
18 }
19
20 // 隐藏推荐标签下的所有卡片中带有"推荐"标记的内容
21 hideRecommendedCards();
22 }
23
24 // 隐藏推荐 feed
25 function hideRecommendFeed() {
26 const feedList = document.querySelector('.Topstory-recommend .TopstoryMain');
27 if (feedList) {
28 feedList.style.display = 'none';
29 }
30 }
31
32 // 隐藏带有推荐标记的卡片
33 function hideRecommendedCards() {
34 // 隐藏首页推荐的内容项
35 const cards = document.querySelectorAll('.Card.TopstoryItem');
36 cards.forEach(card => {
37 // 检查是否有"推荐"来源标记
38 const sourceTag = card.querySelector('.FeedSource');
39 if (sourceTag && sourceTag.textContent.includes('推荐')) {
40 card.style.display = 'none';
41 }
42 });
43
44 // 隐藏整个推荐 feed 区域
45 const recommendSection = document.querySelector('[data-za-detail-view-path-module="TopstoryRecommend"]');
46 if (recommendSection) {
47 recommendSection.style.display = 'none';
48 }
49 }
50
51 // 自动切换到"关注"标签
52 function switchToFollowTab() {
53 // 只在首页执行
54 if (!window.location.pathname.match(/^\/?$/)) {
55 return;
56 }
57
58 const followTab = document.querySelector('.Topstory-tabs .TopstoryTabs-link[href="/follow"]');
59 if (followTab && !followTab.classList.contains('is-active')) {
60 followTab.click();
61 }
62 }
63
64 // 使用 MutationObserver 监听 DOM 变化
65 function observeDOM() {
66 const observer = new MutationObserver((mutations) => {
67 blockRecommendations();
68 });
69
70 observer.observe(document.body, {
71 childList: true,
72 subtree: true
73 });
74 }
75
76 // 页面加载完成后执行
77 function init() {
78 // 先尝试切换到关注标签
79 switchToFollowTab();
80
81 // 屏蔽推荐内容
82 blockRecommendations();
83
84 // 监听 DOM 变化以处理动态加载的内容
85 observeDOM();
86 }
87
88 // 确保 DOM 加载完成
89 if (document.readyState === 'loading') {
90 document.addEventListener('DOMContentLoaded', init);
91 } else {
92 init();
93 }
94
95 // 同时也在 window.load 时再次执行,确保所有动态内容都被处理
96 window.addEventListener('load', () => {
97 setTimeout(init, 500);
98 });
99})();