本帖最后由 0772boy 于 2026-5-1 16:41 编辑
前两天节前上班摸鱼,浏览IT之家被铺天盖地的财报恶心到了,遂有此脚本,直接上代码:
食用方法:根据需求修改自定义屏蔽词即可。
[JavaScript] 纯文本查看 复制代码
// ==UserScript==
// @name IT之家资讯屏蔽器
// @namespace [url]http://tampermonkey.net/[/url]
// @version 1.2
// @description IT之家资讯过滤
// @author 0772Boy
// @match [url]https://it.ithome.com/[/url]
// @match [url]https://ithome.com/[/url]
// @match [url]https://www.ithome.com/[/url]
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// ====================== 【自定义屏蔽词】 ======================
const blockRules = [
/20[0-9]{2}Q[1-4]/i, // 匹配 2000~2099年 + Q1~Q4
/营业收入|营收|净利润|亏损|同比/i,
/亿新台币|亿元|万亿韩元|亿美元/i,
/财年第一财季|财季净利润/i
];
// 屏蔽目标 class
const targetClass = 'title';
function isBlockedTitle(title) {
if (!title) return false;
return blockRules.some(rule => rule.test(title));
}
function blockNews() {
const titleElements = document.querySelectorAll(`.${targetClass}`);
titleElements.forEach(element => {
const titleText = element.textContent.trim();
if (isBlockedTitle(titleText)) {
let newsCard = element.closest('li, div.article, div.item');
if (newsCard) {
newsCard.style.display = 'none';
newsCard.style.height = '0';
newsCard.style.margin = '0';
newsCard.style.padding = '0';
}
}
});
}
function listenMoreButton() {
const checkButton = setInterval(() => {
const moreButton = document.querySelector('a.more');
if (moreButton) {
clearInterval(checkButton);
moreButton.addEventListener('click', () => {
// 延迟 666ms 确保新内容加载完成再过滤
setTimeout(blockNews, 666);
});
}
}, 300);
}
window.addEventListener('load', () => {
setTimeout(blockNews, 500);
listenMoreButton();
});
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.addedNodes.length) {
blockNews();
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();
|