隐藏B站动态瀑布流中的广告、评论区广告、充电内容以及美化首页
当前为
// ==UserScript==
// @name 隐藏B站充电和广告
// @version 1.20
// @description 隐藏B站动态瀑布流中的广告、评论区广告、充电内容以及美化首页
// @author chemhunt
// @match *.bilibili.com/*
// @match https://t.bilibili.com/*
// @match https://space.bilibili.com/*
// @match https://www.bilibili.com/
// @match https://www.bilibili.com/video/*
// @grant none
// @license GPL-3.0 License
// @namespace https://greasyfork.org/scripts/511437/
// ==/UserScript==
(function() {
'use strict';
// 白名单UP主,可自行修改、增删
const whiteList = [
'白名单UP_1', '白名单UP_2', '木鱼水心',
];
const messageDiv = document.createElement('div');
Object.assign(messageDiv.style, {
position: 'fixed',
top: '10px',
right: '10px',
padding: '10px',
backgroundColor: 'rgba(0, 0, 0, 0.7)',
color: 'white',
borderRadius: '5px',
zIndex: '9999',
display: 'none'
});
document.body.appendChild(messageDiv);
const blockedKeywords = [
'拼多多', '淘宝', '京东', '天猫', '手淘', '旗舰店','运费',
'特价', '领券', '下单', '礼包', '补贴', '优惠', '双11','双12', '618', '品牌方',
'溪木源', '海力生', '萌牙家', '妙界', 'DAWEI', '温眠', '护肝', '护颈','护眼', '护枕', '按摩', '冲牙','牙刷',
//'中奖', '预告', '抽奖',
];
const blockedLinks = [
'taobao.com', 'jd.com', 'pinduoduo.com', 'mall.bilibili.com', 'gaoneng.bilibili.com','yangkeduo.com',
];
function hideItem(item) {
item.style.display = 'none';
}
function showMessage(msg) {
messageDiv.textContent = msg;
messageDiv.style.display = 'block';
setTimeout(() => {
messageDiv.style.display = 'none';
}, 3000);
}
// 检查评论区广告
function checkCommentAds(item) {
const commentAds = item.querySelectorAll(
'.dynamic-card-comment .comment-list.has-limit .list-item.reply-wrap.is-top .con'
);
let hiddenAdCount = 0;
commentAds.forEach(comment => {
const links = comment.querySelectorAll('a');
let foundAd = false;
links.forEach(link => {
const href = link.getAttribute('href');
if (href && blockedLinks.some(blocked => href.includes(blocked))) {
foundAd = true;
}
});
if (foundAd) {
hideItem(comment);
hiddenAdCount++;
}
});
return hiddenAdCount;
}
function checkCommentsForAds(observer) {
const commentsContainer = document.querySelector('#commentapp > bili-comments');
if (commentsContainer && commentsContainer.shadowRoot) {
const thread = commentsContainer.shadowRoot.querySelector('bili-comment-thread-renderer');
if (thread && thread.shadowRoot) {
const commentRenderer = thread.shadowRoot.querySelector('#comment');
if (commentRenderer && commentRenderer.shadowRoot) {
const richText = commentRenderer.shadowRoot.querySelector('#content > bili-rich-text');
if (richText && richText.shadowRoot) {
const contentsElement = richText.shadowRoot.querySelector('#contents');
if (contentsElement) {
let foundAd = false;
const links = contentsElement.querySelectorAll('a');
links.forEach(link => {
const href = link.getAttribute('href');
if (href && blockedLinks.some(blocked => href.includes(blocked))) {
foundAd = true;
}
});
if (foundAd) {
hideItem(thread);
hiddenAdCount++;
observer.disconnect();
let message = `隐藏广告 x ${hiddenAdCount}`;
showMessage(message);
return true;
}
}
}
}
}
}
return false;
}
function checkForContentToHide(observer) {
let hiddenChargeCount = 0;
let hiddenAdCount = 0;
let hiddenFloorCardCount = 0;
if (window.location.hostname === 't.bilibili.com' || window.location.hostname === 'space.bilibili.com') {
const items = document.querySelectorAll('.bili-dyn-list__item');
items.forEach(item => {
//评论区上方横条
const noticeElement = document.querySelector('bili-comments-notice');
if (noticeElement) {hideItem(item);}
//检查UP白名单
const titleElement = item.querySelector('.bili-dyn-title');
if (titleElement && whiteList.includes(titleElement.textContent.trim())) {
return;
}
//充电按钮
const blockedmask = item.querySelector('.dyn-blocked-mask');
//充电视频
const badge = item.querySelector('.bili-dyn-card-video__badge');
//充电抽奖
const lotteryTitle = item.querySelector('.dyn-upower-lottery__title');
//广告关键词
const contentDiv = item.querySelector('.bili-dyn-content');
//UP推荐商品
const goods = item.querySelector('dyn-goods');
const goods2 = item.querySelector('bili-dyn-card-goods');
//子元素
const spans = item.querySelectorAll('span');
if (goods || goods2) {
hideItem(item);
hiddenAdCount++;
} else if (blockedmask) {
hideItem(item);
hiddenChargeCount++;
} else if (badge && badge.textContent.includes('专属') ||
Array.from(spans).some(span => span.textContent.includes('专属')) ||
lotteryTitle && lotteryTitle.textContent.includes('专属')) {
hideItem(item);
hiddenChargeCount++;
} else if (contentDiv) {
const contentText = contentDiv.textContent;
if (blockedKeywords.some(keyword => contentText.includes(keyword))) {
hideItem(item);
hiddenAdCount++;
}
}
spans.forEach(span => {
const dataUrl = span.getAttribute('data-url');
if (dataUrl && blockedLinks.some(blocked => dataUrl.includes(blocked))) {
hideItem(item);
hiddenAdCount++;
}
});
// **合并:检查评论区广告**
hiddenAdCount += checkCommentAds(item);
});
}
//处理首页
if (window.location.hostname === 'www.bilibili.com' && !window.location.pathname.startsWith('/video/')){
// 首页推广
const floorCards = document.querySelectorAll('.floor-single-card');
floorCards.forEach(card => {
hideItem(card);
hiddenFloorCardCount++;
});
// 首页大屏
const targetElement = document.querySelector('.recommended-swipe.grid-anchor');
if (targetElement) {
console.log('隐藏首页大屏');
hideItem(targetElement);
}
}
if (window.location.pathname.startsWith('/video/')) {
// 尝试立即检查评论中的广告
if (!checkCommentsForAds(observer)) {
// 如果未发现广告,3秒后再尝试一次
setTimeout(() => {
checkCommentsForAds(observer);
}, 3000);
}
}
let message = '';
if (hiddenChargeCount > 0) {
message += `隐藏充电 x ${hiddenChargeCount} `;
}
if (hiddenAdCount > 0) {
message += `隐藏广告 x ${hiddenAdCount} `;
}
//if (hiddenFloorCardCount > 0) {
// message += `隐藏卡片 x ${hiddenFloorCardCount}`;
//}
if (message) {
showMessage(message.trim());
}
}
const observer = new MutationObserver(checkForContentToHide);
observer.observe(document.body, {
childList: true, subtree: true
});
checkForContentToHide();
})();