// ==UserScript== // @name B站主页美化 // @namespace http://tampermonkey.net/ // @version 2.3 // @description 去除B站主页视频列表中的广告,支持预加载,可配置显示分区 // @author thanksplay // @match https://www.bilibili.com/ // @match https://www.bilibili.com/index.html // @match https://www.bilibili.com/video/* // @icon https://www.google.com/s2/favicons?sz=64&domain=bilibili.com // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // @license MIT // ==/UserScript== (function() { 'use strict'; var sections = [ { key: 'showMovie', name: '电影', default: false }, { key: 'showTeleplay', name: '电视剧', default: false }, { key: 'showBangumi', name: '番剧', default: false }, { key: 'showGuochuang', name: '国创', default: false }, { key: 'showVariety', name: '综艺', default: false }, { key: 'showAnime', name: '动画', default: false }, { key: 'showGame', name: '游戏', default: false }, { key: 'showGuichu', name: '鬼畜', default: false }, { key: 'showMusic', name: '音乐', default: false }, { key: 'showDance', name: '舞蹈', default: false }, { key: 'showYingshi', name: '影视', default: false }, { key: 'showYule', name: '娱乐', default: false }, { key: 'showKnowledge', name: '知识', default: false }, { key: 'showTech', name: '科技', default: false }, { key: 'showFood', name: '美食', default: false }, { key: 'showCar', name: '汽车', default: false }, { key: 'showFashion', name: '时尚', default: false }, { key: 'showNews', name: '资讯', default: false }, { key: 'showLife', name: '生活', default: false }, { key: 'showSports', name: '运动', default: false }, { key: 'showAnimal', name: '动物圈', default: false }, { key: 'showVlog', name: 'VLOG', default: false } ]; var settings = {}; sections.forEach(function(section) { settings[section.key] = GM_getValue(section.key, section.default); }); var style = document.createElement('style'); style.textContent = ` .recommended-swipe { display: none !important; } .video-card-ad-small { display: none !important; } .slide-ad-exp { display: none !important; } .bili-settings-btn { position: fixed; bottom: 70px; right: 20px; z-index: 99999; width: 44px; height: 44px; background: #f4f4f4; border: none; border-radius: 8px; cursor: pointer; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 10px rgba(0,0,0,0.1); transition: all 0.3s; } .bili-settings-btn:hover { background: #e8e8e8; transform: scale(1.05); } .bili-settings-btn svg { width: 22px; height: 22px; fill: #666; transition: transform 0.3s; } .bili-settings-btn:hover svg { transform: rotate(45deg); } .bili-settings-panel { position: fixed; bottom: 130px; right: 20px; z-index: 99998; width: 280px; max-height: 400px; overflow-y: auto; background: #fff; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.15); padding: 16px; display: none; } .bili-settings-panel.show { display: block; } .bili-settings-title { font-size: 14px; font-weight: bold; color: #333; margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid #eee; } .bili-settings-item { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; } .bili-settings-item span { font-size: 13px; color: #333; } .bili-switch { position: relative; width: 44px; height: 22px; background: #ccc; border-radius: 11px; cursor: pointer; transition: background 0.3s; } .bili-switch.on { background: #00a1d6; } .bili-switch::after { content: ''; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; background: #fff; border-radius: 50%; transition: left 0.3s; } .bili-switch.on::after { left: 24px; } .bili-settings-panel::-webkit-scrollbar { width: 6px; } .bili-settings-panel::-webkit-scrollbar-thumb { background: #ddd; border-radius: 3px; } .bili-settings-panel::-webkit-scrollbar-track { background: #f5f5f5; } `; document.head.appendChild(style); function removeAds() { try { var feedCards = document.querySelectorAll('.feed-card'); feedCards.forEach(function(card) { var adLink = card.querySelector('a[href*="cm.bilibili.com/cm/api/fees"]'); if (adLink) { card.style.display = 'none'; return; } var statsText = card.querySelector('.bili-video-card__stats--text'); if (statsText && statsText.textContent.trim() === '广告') { card.style.display = 'none'; return; } var adImg = card.querySelector('img[src*="sycp/creative_img"], img[src*="hdslb.com/bfs/sycp"]'); if (adImg) { card.style.display = 'none'; return; } }); } catch(e) { console.error('removeAds error:', e); } } function hideSections() { try { var floorTitles = document.querySelectorAll('.floor-title'); floorTitles.forEach(function(floorTitle) { var title = floorTitle.textContent.trim(); var shouldShow = false; sections.forEach(function(section) { if (settings[section.key] && title === section.name) { shouldShow = true; } }); var feedCard = floorTitle.closest('.feed-card'); if (feedCard) { feedCard.style.display = shouldShow ? '' : 'none'; } }); } catch(e) { console.error('hideSections error:', e); } } var timer = null; function processPage() { removeAds(); hideSections(); } function debounceProcess() { clearTimeout(timer); timer = setTimeout(processPage, 300); } processPage(); var observer = new MutationObserver(function() { debounceProcess(); }); observer.observe(document.body, { childList: true, subtree: true }); function preloadVideos() { console.log('开始预加载视频...'); var currentScroll = window.scrollY; window.scrollTo(0, document.documentElement.scrollHeight); setTimeout(function() { window.scrollTo(0, document.documentElement.scrollHeight); setTimeout(function() { window.scrollTo(0, currentScroll); console.log('预加载完成'); }, 800); }, 800); } function createUI() { var settingsBtn = document.createElement('button'); settingsBtn.className = 'bili-settings-btn'; settingsBtn.innerHTML = ''; var panel = document.createElement('div'); panel.className = 'bili-settings-panel'; var panelHTML = '