// ==UserScript==
// @name B站视频过滤插件
// @namespace http://tampermonkey.net/
// @version 4.4.1
// @description 综合屏蔽管理:智能广告拦截、分类过滤、直播屏蔽、精准关键词过滤
// @author 觅星小竹
// @match *://www.bilibili.com/*
// @exclude *://www.bilibili.com/video/*
// @icon https://www.bilibili.com/favicon.ico
// @grant GM_setValue
// @grant GM_getValue
// @license MIT
// @downloadURL https://update.greasyfork.icu/scripts/530414/B%E7%AB%99%E8%A7%86%E9%A2%91%E8%BF%87%E6%BB%A4%E6%8F%92%E4%BB%B6.user.js
// @updateURL https://update.greasyfork.icu/scripts/530414/B%E7%AB%99%E8%A7%86%E9%A2%91%E8%BF%87%E6%BB%A4%E6%8F%92%E4%BB%B6.meta.js
// ==/UserScript==
(function() {
'use strict';
// 全局配置系统
const CONFIG = {
video: {
enabled: GM_getValue('videoEnabled', true),
blacklist: GM_getValue('videoBlacklist', [])
},
category: {
enabled: GM_getValue('categoryEnabled', true),
blacklist: GM_getValue('categoryBlacklist', [
'番剧', '直播', '国创', '综艺',
'课堂', '电影', '电视剧', '纪录片',"漫画",
])
},
ad: GM_getValue('adEnabled', true),
live: GM_getValue('liveEnabled', true)
};
// 广告屏蔽模块 -------------------------------------------------
const adKeywords = ['广告', 'Sponsored', '推广'];
const adSelectors = [
'.bili-video-card__stats--text',
'.feed-card',
'[data-report*="ad_card"]',
'.bili-ad-card',
'[ad-id]'
];
function removeItem(){
const recommendItem = document.querySelector(".recommended-swipe");
if(recommendItem) recommendItem.remove();
const feed_cards = document.querySelectorAll(".feed-card");
feed_cards.forEach(card =>{
card.remove();
});
}
function isAdElement(element) {
return adKeywords.some(keyword =>
element.textContent.includes(keyword) ||
element.getAttribute('data-report')?.includes('ad') ||
element.closest('[class*="ad"], [class*="Ad"]')
);
}
function hideAd(element) {
if (!CONFIG.ad) return;
const adContainer = element.closest('.feed-card, .bili-video-card, .bili-grid') || element;
if (adContainer && !adContainer.dataset.adBlocked) {
adContainer.remove();
}
}
function blockAds1() {
if (!CONFIG.ad) return;
adSelectors.forEach(selector => {
document.querySelectorAll(selector).forEach(element => {
if (isAdElement(element)) hideAd(element);
});
});
}
function blockAds(){
if (!CONFIG.ad) return;
document.querySelectorAll(".bili-feed-card").forEach(card=>{
const adElementText = card.querySelector(".bili-video-card__stats--text").textContent.trim();
const adItem = card.querySelector('svg path[d^="M16.9122"]');
if(adElementText === "广告" || adItem) card.remove();
});
}
// 分类屏蔽模块 -------------------------------------------------
function blockCategories() {
if (!CONFIG.category.enabled) return;
document.querySelectorAll('.floor-single-card').forEach(card => {
const categoryElement = card.querySelector('.floor-title');
if (!categoryElement) return;
const category = categoryElement.textContent.trim();
const shouldBlock = CONFIG.category.blacklist.some(keyword =>
category.includes(keyword)
);
if (shouldBlock) {
card.remove();
}
/**
if (shouldBlock) {
card.style.display = 'none';
card.dataset.blocked = 'true';
} else if (card.dataset.blocked) {
card.style.display = '';
delete card.dataset.blocked;
}
**/
});
}
// 视频关键词屏蔽模块 --------------------------------------------
function blockVideos() {
if (!CONFIG.video.enabled) return;
document.querySelectorAll('.bili-feed-card').forEach(video => {
const title = video.querySelector('.bili-video-card__info--tit')?.textContent.trim() || '';
const author = video.querySelector('.bili-video-card__info--author')?.textContent.trim() || '';
const shouldBlock = CONFIG.video.blacklist.some(keyword =>
title.includes(keyword) || author.includes(keyword)
);
if (shouldBlock) {
video.remove();
}
});
}
// 主控制面板 -------------------------------------------------
function createMainPanel() {
const panel = document.createElement('div');
panel.innerHTML = `
`;
panel.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
checkbox.addEventListener('change', e => {
const parentText = e.target.closest('.switch-item').querySelector('span').textContent;
if (parentText.includes('视频')) {
CONFIG.video.enabled = e.target.checked;
GM_setValue('videoEnabled', e.target.checked);
blockVideos();
} else if (parentText.includes('分类')) {
CONFIG.category.enabled = e.target.checked;
GM_setValue('categoryEnabled', e.target.checked);
blockCategories();
} else if (parentText.includes('广告')) {
CONFIG.ad = e.target.checked;
GM_setValue('adEnabled', e.target.checked);
blockAds();
} else {
CONFIG.live = e.target.checked;
GM_setValue('liveEnabled', e.target.checked);
}
});
});
panel.querySelectorAll('.manage-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
showManagementPanel(btn.dataset.type);
});
});
panel.querySelector('.close-btn').addEventListener('click', () => {
panel.style.display = 'none';
});
return panel;
}
// 视频关键词管理面板 --------------------------------------------
function showVideoPanel() {
const panel = document.createElement('div');
panel.innerHTML = `
当前屏蔽关键词:${CONFIG.video.blacklist.length} 个
`;
const closeBtn = panel.querySelector('.close-btn');
const input = panel.querySelector('input');
const addBtn = panel.querySelector('.add-btn');
const keywordList = panel.querySelector('.keyword-list');
const countSpan = panel.querySelector('#keyword-count');
function updateList() {
keywordList.innerHTML = CONFIG.video.blacklist.map(word => `
${word}
`).join('');
keywordList.querySelectorAll('button').forEach(btn => {
btn.addEventListener('click', () => {
CONFIG.video.blacklist = CONFIG.video.blacklist.filter(w => w !== btn.dataset.word);
GM_setValue('videoBlacklist', CONFIG.video.blacklist);
countSpan.textContent = CONFIG.video.blacklist.length;
updateList();
blockVideos();
});
});
}
addBtn.addEventListener('click', () => {
const keyword = input.value.trim();
if (keyword && !CONFIG.video.blacklist.includes(keyword)) {
CONFIG.video.blacklist.push(keyword);
GM_setValue('videoBlacklist', CONFIG.video.blacklist);
input.value = '';
countSpan.textContent = CONFIG.video.blacklist.length;
updateList();
blockVideos();
}
});
input.addEventListener('keypress', e => {
if (e.key === 'Enter') addBtn.click();
});
closeBtn.addEventListener('click', () => {
document.body.removeChild(panel);
});
updateList();
document.body.appendChild(panel);
}
// 分类管理面板 -------------------------------------------------
function showCategoryPanel() {
const panel = document.createElement('div');
panel.innerHTML = `
当前屏蔽分类:${CONFIG.category.blacklist.length} 个
`;
const closeBtn = panel.querySelector('.close-btn');
const input = panel.querySelector('input');
const addBtn = panel.querySelector('.add-btn');
const categoryList = panel.querySelector('.category-list');
const countSpan = panel.querySelector('#category-count');
function updateList() {
categoryList.innerHTML = CONFIG.category.blacklist.map(cat => `
${cat}
`).join('');
categoryList.querySelectorAll('button').forEach(btn => {
btn.addEventListener('click', () => {
CONFIG.category.blacklist = CONFIG.category.blacklist.filter(c => c !== btn.dataset.cat);
GM_setValue('categoryBlacklist', CONFIG.category.blacklist);
countSpan.textContent = CONFIG.category.blacklist.length;
updateList();
blockCategories();
});
});
}
addBtn.addEventListener('click', () => {
const category = input.value.trim();
if (category && !CONFIG.category.blacklist.includes(category)) {
CONFIG.category.blacklist.push(category);
GM_setValue('categoryBlacklist', CONFIG.category.blacklist);
input.value = '';
countSpan.textContent = CONFIG.category.blacklist.length;
updateList();
blockCategories();
}
});
input.addEventListener('keypress', e => {
if (e.key === 'Enter') addBtn.click();
});
closeBtn.addEventListener('click', () => {
document.body.removeChild(panel);
});
updateList();
document.body.appendChild(panel);
}
// 管理面板路由 -------------------------------------------------
function showManagementPanel(type) {
if (type === 'video') {
showVideoPanel();
} else if (type === 'category') {
showCategoryPanel();
}
}
// 直播屏蔽模块 -------------------------------------------------
function blockLive() {
document.querySelectorAll('.live-card, .bili-live-card').forEach(card => {
card.style.display = CONFIG.live ? 'none' : '';
});
}
// 初始化逻辑 -------------------------------------------------
const observer = new MutationObserver(() => {
blockAds();
blockCategories();
blockVideos();
blockLive();
removeItem();
});
const style = document.createElement('style');
style.textContent = `
[data-ad-blocked], [data-blocked] {
display: none !important;
height: 0 !important;
margin: 0 !important;
padding: 0 !important;
}
`;
document.head.appendChild(style);
// 浮动按钮
const floatBtn = document.createElement('div');
floatBtn.innerHTML = `
🛡️
`;
document.body.appendChild(floatBtn);
// 主控制面板
const mainPanel = createMainPanel();
document.body.appendChild(mainPanel);
mainPanel.style.display = 'none';
floatBtn.querySelector('.master-float-btn').addEventListener('click', () => {
mainPanel.style.display = mainPanel.style.display === 'none' ? 'block' : 'none';
});
// 启动监听
blockAds();
blockCategories();
blockVideos();
blockLive();
removeItem();
observer.observe(document.body, { childList: true, subtree: true });
})();