// ==UserScript== // @name b站自动删除空白视频格 // @namespace https://greasyfork.org/zh-CN/scripts // @version 2024-04-26 // @description 净化b站 AdGuard拦截广告后的空白格子 // @author cccq and Copilot and 通义灵码 // @match https://www.bilibili.com/ // @icon https://ts1.cn.mm.bing.net/th?id=OIP-C.t_km_I0O-asr3a-bNrejjQHaHa&w=204&h=204&c=8&rs=1&qlt=90&o=6&pid=3.1&rm=2 // @grant none // @license cccq // @downloadURL none // ==/UserScript== (function () { // 观察container是否新增dom const container = document.querySelector('.container'); // 删除元素函数 function removeHiddenElements() { // 广告出现在这个dom中,Adguard会将广告使用css隐藏 let allElements = document.querySelectorAll('.bili-video-card__wrap.__scale-wrap'); let hiddenElements = Array.from(allElements).filter(el => { return window.getComputedStyle(el).display === 'none'; }); hiddenElements.forEach(el => { // 找到广告的父元素 let biliVideoCard = el.closest('.bili-video-card'); // 如果广告父元素上还有一层则删除最上层的dom if (biliVideoCard) { let feedCard = biliVideoCard.closest('.feed-card'); if (feedCard) { feedCard.remove(); } else { biliVideoCard.remove(); } } }); }; // 创建观察器 const observer = new MutationObserver((mutationsList, observer) => { // 当子节点发生变动时 mutationsList.forEach((mutationRecord) => { if (mutationRecord.type === 'childList') { mutationRecord.addedNodes.forEach((addedNode) => { if (addedNode.nodeType === Node.ELEMENT_NODE) { // 执行删除元素 removeHiddenElements(); // 退出foreach循环,等待下次节点新增 return; } }); } }); }); const config = { childList: true, subtree: true, }; observer.observe(container, config); })();