// ==UserScript== // @name 哔哩哔哩广告隐藏(优化版) // @name:zh-CN 哔哩哔哩广告隐藏(优化版) // @namespace http://tampermonkey.net/ // @version 1.0.0 // @author RTX9090 // @description Automatically hide various ads and promotional sections on Bilibili to provide a cleaner browsing experience. // @description:zh-CN 自动隐藏B站页面中的广告和推广内容,包括首页广告卡片、视频页活动推广、直播自动播放等,提供清爽的浏览体验。 // @match *://www.bilibili.com/* // @match *://search.bilibili.com/* // @match *://*.bilibili.com/* // @icon https://www.bilibili.com/favicon.ico // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant GM_setValue // @grant GM_getValue // @grant GM_notification // @grant GM_addStyle // @license MIT // ==/UserScript== (function() { 'use strict'; // ---------- 菜单 ---------- const MENU_KEY = 'hideBilibiliCarousel'; if (GM_getValue(MENU_KEY) === undefined) GM_setValue(MENU_KEY, true); function refreshMenu() { const menuId = GM_registerMenuCommand( `${GM_getValue(MENU_KEY) ? '✅' : '❌'} 隐藏首页左侧轮播图`, () => { const current = GM_getValue(MENU_KEY); GM_setValue(MENU_KEY, !current); GM_notification({ text: `已${!current ? '开启' : '关闭'} [隐藏首页左侧轮播图]\n(点击刷新网页后生效)`, timeout: 3500, onclick: () => location.reload() }); refreshMenu(); } ); return menuId; } let menuCommandId = refreshMenu(); // ---------- 工具函数 ---------- const injectCSS = (css) => GM_addStyle(css); // 使用 CSS class 批量隐藏,性能更好 const hiddenClassName = 'tm-bili-ad-hidden'; injectCSS(` .${hiddenClassName} { display: none !important; visibility: hidden !important; opacity: 0 !important; height: 0 !important; width: 0 !important; overflow: hidden !important; position: absolute !important; pointer-events: none !important; z-index: -9999 !important; clip: rect(0,0,0,0) !important; } `); function hideElement(el) { if (!el) return; el.classList.add(hiddenClassName); // 清除事件监听(简单做法:克隆节点替换) const clone = el.cloneNode(true); clone.classList.add(hiddenClassName); if (el.parentNode) el.parentNode.replaceChild(clone, el); } // ---------- 页面规则 ---------- function getPageInfo() { const host = location.hostname; const path = location.pathname; return { host, path }; } function applyPageRules() { const { host, path } = getPageInfo(); // 通用隐藏 GM_addStyle(` .login-tip, .vip-login, .vip-login-tip, .login-panel-popover, .desktop-download-tip, .lazy-img { display: none !important; } `); // 首页 if (host === 'www.bilibili.com' && (path === '/' || path === '/index.html')) { const carouselCSS = GM_getValue(MENU_KEY) ? `.recommended-swipe, .fixed-card { display: none !important; } .feed-card { margin-top: 0 !important; }` : ''; GM_addStyle(` ${carouselCSS} .bili-video-card__skeleton.loading_animation, .bili-live-card.is-rcmd.enable-no-interest, .ad-report.ad-floor-exp.left-banner, .floor-single-card, .bili-live-card.is-rcmd { display: none !important; } `); } // 视频播放页 if (host === 'www.bilibili.com' && path.startsWith('/video/')) { GM_addStyle(` .bpx-player-qoeFeedback, .bili-danmaku-x-guide.bili-danmaku-x-show, .bili-danmaku-x-cmd-shrink, .bili-danmaku-x-link.bili-danmaku-x-show, .bili-danmaku-x-scoreSum.bili-danmaku-x-show, .bili-danmaku-x-vote.bili-danmaku-x-show, .bili-danmaku-x-score.bili-danmaku-x-show, .bili-danmaku-x-guide-all.bili-danmaku-x-guide.bili-danmaku-x-show, .ad-report.strip-ad.left-banner, .ad-report.ad-floor-exp.left-banner, .ad-report.ad-floor-exp.right-bottom-banner, .activity-m-v1.act-end, .activity-m-v1.act-now, .video-card-ad-small, .video-page-game-card-small, .slide-ad-exp { display: none !important; } `); } // 直播首页防自动播放 if (host === 'live.bilibili.com' && (path === '/' || path === '')) { GM_addStyle(` .player-area-ctnr.border-box.p-relative.t-center { display: none !important; } `); HTMLMediaElement.prototype.play = new Proxy(HTMLMediaElement.prototype.play, { apply(target, thisArg, args) { if (new Error().stack?.includes('home-player.prod.min.js')) { thisArg.pause(); thisArg.currentTime = 0; thisArg.removeAttribute('autoplay'); return Promise.reject(new DOMException('play() failed')); } return Reflect.apply(target, thisArg, args); } }); } // 动态首页 if (host === 't.bilibili.com') { GM_addStyle(` .bili-dyn-ads { display: none !important; } `); } } // ---------- 广告卡片清理 ---------- function hideAdCards() { document.querySelectorAll('a[href*="cm.bilibili.com/cm/api"]').forEach(link => { const card = link.closest('.bili-video-card, .video-card-common, .bili-live-card, .feed-card'); if (card) hideElement(card); }); } // ---------- 全局观察器 ---------- const observer = new MutationObserver(() => { hideAdCards(); // 可扩展其他动态检查 }); observer.observe(document.body, { childList: true, subtree: true }); // URL 变化监听(SPA) let lastUrl = location.href; const urlObserver = new MutationObserver(() => { if (location.href !== lastUrl) { lastUrl = location.href; applyPageRules(); // 重新应用页面规则 } }); urlObserver.observe(document.body, { childList: true, subtree: true }); // ---------- 启动 ---------- applyPageRules(); hideAdCards(); })();