// ==UserScript== // @name B站净化器 // @namespace http://tampermonkey.net/ // @version 1.0 // @description Better Bilibili 是一款Tampermonkey脚本,旨在为您提供更清爽、无广告的B站浏览体验。它能自动过滤广告、推广内容,并允许您根据播放量、视频时长、UP主、标题关键词和分区等多种条件自定义隐藏视频,让您的B站首页和视频页面只显示您真正感兴趣的内容。轻松配置,即刻享受纯净B站! // @author Kiyuiro // @match https://*.bilibili.com/* // @icon https://www.google.com/s2/favicons?sz=64&domain=bilibili.com // @grant none // @license Apache-2.0 // @downloadURL none // ==/UserScript== (function () { 'use strict'; function findElement(selector, timeout = 5000, interval = 50) { return new Promise((resolve) => { const start = Date.now(); function check() { const element = document.querySelector(selector); if (element) { resolve(element); } else if (Date.now() - start >= timeout) { resolve(undefined); } else { setTimeout(check, interval); } } check(); }); } function findElements(selector, timeout = 5000, interval = 50) { return new Promise((resolve) => { const start = Date.now(); function check() { const elements = document.querySelectorAll(selector) || []; if (elements.length > 0) { resolve(elements); } else if (Date.now() - start >= timeout) { resolve([]); } else { setTimeout(check, interval); } } check(); }); } function parseDuration(duration) { const parts = duration.split(':').map(Number); let hours = 0, minutes = 0, seconds = 0; if (parts.length === 2) { // mm:ss 格式 [minutes, seconds] = parts; } else if (parts.length === 3) { // hh:mm:ss 格式 [hours, minutes, seconds] = parts; } else { throw new Error('Invalid duration format'); } return hours * 3600 + minutes * 60 + seconds; } const path = window.location.href; const paths = path.split('/').filter(it => it.length > 0); // 广告tips async function adblockClear() { const adblock = await findElement('.adblock-tips'); if (adblock) { adblock.remove(); } } // 视频过滤 async function videoFilter() { const config = JSON.parse(localStorage.getItem('BiliFilterConfig')) || []; console.log(config); // 过滤 setInterval(async () => { // 广告/播放量/时长 过滤 if (config.ad || config.video.duration || config.video.playCount) { const items = await findElements(".bili-video-card__stats"); items.forEach(it => { // 广告 if (config.ad && it.innerHTML.length < 150) { it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode .remove(); // it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode // .style.backgroundColor = 'red' return; } // 播放量过滤 if (config.video.playCount) { const t = it.querySelectorAll('.bili-video-card__stats--text') if (t[0]) { if (t[0].innerHTML === '广告') return; let count; const countText = t[0].innerHTML; if (countText.includes('万')) { count = parseFloat(countText.replace('万', '')) * 10000; } else if (countText.includes('亿')) { count = parseFloat(countText.replace('亿', '')) * 100000000; } else { count = parseInt(countText); } if (count < config.video.playCount) { // it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode // .style.backgroundColor = 'red' it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode .remove(); return; } } // if(t[1]) { // t[1].style.color = 'blue' // } } // 时长过滤 if (config.video.duration) { const t = it.querySelector('.bili-video-card__stats__duration') const configDuration = parseDuration(config.video.duration); if (t) { const duration = parseDuration(t.innerHTML); if (duration < configDuration) { // it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode // .style.backgroundColor = 'green' it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode .remove(); } } } }) } // 推广过滤 if (config.prom) { const items = await findElements('.vui_icon') items.forEach(it => { it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode .remove(); }) } // UP 主过滤 if (config.upList && config.upList.length > 0) { const items = await findElements('.bili-video-card__info--author') items.forEach(it => { if (config.upList.find(up => it.title.includes(up))) { it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode .remove(); } }) } // 标题过滤 if (config.titleList && config.titleList.length > 0) { const items = await findElements('.bili-video-card__info--tit') items.forEach(it => { if (config.titleList.find(up => it.title.includes(up))) { it.parentNode.parentNode.parentNode.parentNode.parentNode .remove(); } }) } // 分区过滤 if (config.partList && config.partList.length > 0) { const items = await findElements('.floor-title') const isAll = config.partList[0] === 'ALL'; items.forEach(it => { if (isAll || config.partList.find(up => it.title.includes(up))) { it.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode .remove(); } }) } // 空元素清除 const feeds = await findElements('.feed-card'); feeds.forEach(it => { if (it.innerHTML.length < 10) { it.remove(); } }) }, 100); } // 视频页 async function videoPage() { if (paths.indexOf('video') === -1) { return } // 删除投币展示框 setInterval(async () => { const toubi = await findElement('.bili-danmaku-x-guide-all'); if (toubi) { toubi.remove(); } }, 100) // 删除视频下广告 setInterval(async () => { const ads = await findElements('.ad-report, #slide_ad, .activity-m-v1, .video-page-game-card-small'); if (ads) { ads.forEach(ad => { ad.innerHTML = ""; }); } }, 100); // 多 p 视频列表扩展 const list = await findElement('.video-pod__body'); const video = await findElement('.bpx-player-video-area') if (list) { if (video) { setInterval(() => { list.style.maxHeight = video.offsetHeight - 165 + 'px' }, 10) } else { list.style.maxHeight = '400px'; } } } // 添加配置窗口 function addConfigOverlay() { // 过滤配置 // 定义配置窗口的 HTML、CSS 和 JavaScript const configHtml = ` `; const configCss = ` .config-window-container { background-color: #ffffff; padding: 30px; border-radius: 12px; box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1); width: 100%; max-width: 500px; box-sizing: border-box; max-height: 80vh; /* 设置最大高度为视口高度的 80% */ overflow-y: auto; /* 启用垂直滚动 */ } .config-window-container h2, .config-window-container h3 { text-align: center; color: #1f2937; margin-bottom: 25px; font-weight: 700; } .config-section { margin-bottom: 20px; padding: 20px; border: 1px solid #e5e7eb; border-radius: 8px; background-color: #f9fafb; } .config-section label { display: block; margin-bottom: 10px; font-weight: 600; color: #374151; font-size: 0.95rem; } .config-section textarea, .config-section input[type="text"] { width: 100%; padding: 10px; margin-bottom: 15px; border: 1px solid #d1d5db; border-radius: 6px; box-sizing: border-box; min-height: 40px; resize: vertical; font-size: 0.9rem; color: #4b5563; } .config-section textarea { min-height: 80px; } .subsection { margin-top: 20px; padding-top: 15px; border-top: 1px dashed #d1d5db; } /* 开关样式 */ .config-window-container .switch { position: relative; display: inline-block; width: 50px; height: 20px; vertical-align: middle; } .config-window-container .switch input { opacity: 0; width: 0; height: 0; } .config-window-container .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #d1d5db; transition: .4s; border-radius: 28px; } .config-window-container .slider:before { position: absolute; content: ""; height: 16px; width: 24px; left: 2px; bottom: 2px; background-color: white; transition: .4s; border-radius: 28px; } .config-window-container input:checked + .slider { background-color: #2563eb; } .config-window-container input:focus + .slider { box-shadow: 0 0 1px #2563eb; } .config-window-container input:checked + .slider:before { transform: translateX(22px); } .switch-label { display: inline-block; vertical-align: middle; color: #374151; font-weight: 600; margin-right: 15px; } /* 隐藏的元素 */ .config-window-container .hidden { display: none !important; /* 使用 !important 确保覆盖 Tailwind 的 display */ } `; // 注入 Tailwind CSS CDN const tailwindScript = document.createElement('script'); tailwindScript.src = 'https://cdn.tailwindcss.com'; document.head.appendChild(tailwindScript); // 注入自定义 CSS const styleElement = document.createElement('style'); styleElement.textContent = configCss; document.head.appendChild(styleElement); // 注入配置窗口 HTML document.body.insertAdjacentHTML('beforeend', configHtml); // 配置窗口的 DOM 元素 const biliFilterConfigOverlay = document.getElementById('BiliFilterConfigOverlay'); // 注入配置按钮,在头像下面 const interval = setInterval(async () => { const links = await findElement('.links-item', 1000); if (links) { const configButton = document.createElement('div'); configButton.className = 'single-link-item'; configButton.insertAdjacentHTML('beforeend', ` `); links.appendChild(configButton); configButton.addEventListener('click', () => { biliFilterConfigOverlay.classList.remove('hidden'); // 显示配置窗口 }); clearInterval(interval) } }, 1200); // 配置窗口的 JavaScript 逻辑 // 确保在 DOM 元素可用后再执行 tailwindScript.onload = () => { // 等待 Tailwind 加载完成 const saveConfigButton = document.getElementById('saveConfig'); const closeConfigButton = document.getElementById('closeConfig'); // 收集所有配置数据并返回一个对象 const collectConfigData = () => { return { ad: document.getElementById('adToggle').checked, prom: document.getElementById('promToggle').checked, upList: document.getElementById('upList').value.split('\n').filter(item => item.trim() !== ''), titleList: document.getElementById('titleList').value.split('\n').filter(item => item.trim() !== ''), partList: document.getElementById('partList').value.split('\n').filter(item => item.trim() !== ''), video: { playCount: parseInt(document.getElementById('videoPlayCount').value.trim()), duration: document.getElementById('videoDuration').value.trim() }, }; }; // 保存配置按钮点击事件 saveConfigButton.addEventListener('click', () => { const config = collectConfigData(); console.log('保存的配置数据:', config); // 将配置数据保存到 localStorage localStorage.setItem('BiliFilterConfig', JSON.stringify(config)); const messageBox = document.createElement('div'); messageBox.className = 'fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50'; messageBox.innerHTML = `

配置已保存,刷新页面生效。

`; document.body.appendChild(messageBox); document.getElementById('messageBoxClose').addEventListener('click', () => { document.body.removeChild(messageBox); biliFilterConfigOverlay.classList.add('hidden'); // 隐藏配置窗口 }); }); // 关闭配置窗口按钮点击事件 closeConfigButton.addEventListener('click', () => { biliFilterConfigOverlay.classList.add('hidden'); // 隐藏配置窗口 }); // 加载配置数据到表单 const loadConfigData = (data) => { if (!data) { // 如果没有数据,则初始化为空对象 data = { ad: false, upList: [], titleList: [], video: {playCount: '', duration: ''}, }; } document.getElementById('adToggle').checked = data.ad || true; document.getElementById('promToggle').checked = data.prom || true; document.getElementById('upList').value = data.upList ? data.upList.join('\n') : ''; document.getElementById('titleList').value = data.titleList ? data.titleList.join('\n') : ''; document.getElementById('partList').value = data.partList ? data.partList.join('\n') : ''; if (data.video) { document.getElementById('videoPlayCount').value = data.video.playCount || ''; document.getElementById('videoDuration').value = data.video.duration || ''; } }; // 初始加载配置数据 loadConfigData(JSON.parse(localStorage.getItem('BiliFilterConfig'))); }; } function _main() { adblockClear(); videoFilter(); videoPage(); addConfigOverlay(); } _main(); })();