Greasy Fork

Greasy Fork is available in English.

YouTube视觉隐身广告拦截器(播放器修复版)

无API依赖的极简高效广告拦截方案

目前为 2025-03-30 提交的版本。查看 最新版本

// ==UserScript==
// @name        YouTube视觉隐身广告拦截器(播放器修复版)
// @namespace   pure-dom
// @version     4.0
// @match       *://*.youtube.com/*
// @grant       none
// @run-at      document-start
// @description 无API依赖的极简高效广告拦截方案
// ==/UserScript==

(function() {
    'use strict';

    // 动态元素特征库(增加播放器保护白名单)
    const stealthRules = {
        selectors: [
            'ytd-ad-slot-renderer', 
            'div.ytd-promoted-sparkles-web-renderer',
            'ytm-companion-ad-renderer',
            '[data-ad-metadata]',
            'div#player-ads.ytd-watch:not(#movie_player)', // 排除播放器本体
            'ytd-rich-section-renderer:has(ytd-ad-)',
            'div[class*="-ad-"]:not([data-legitimate]):not(.html5-video-player)', // 保护播放器
            'ytd-action-companion-ad-renderer',
            'div.ytp-ad-module'
        ],
        attributes: [
            'ad-showing',
            'data-ad-status',
            'ad-type',
            'data-request-slot'
        ]
    };

    // 增强版三维隐藏系统
    const createGhostCSS = () => {
        const style = document.createElement('style');
        style.id = '__yt_legit_style';
        style.textContent = `
            /* 广告隐藏规则 */
            ${stealthRules.selectors.join(',')} {
                transform: scale(0) !important;
                height: 0 !important;
                width: 0 !important;
                opacity: 0 !important;
                overflow: hidden !important;
                contain: strict !important;
                margin: 0 !important;
                padding: 0 !important;
                pointer-events: none !important;
                visibility: hidden !important;
                position: absolute !important;
                z-index: -99999 !important;
            }

            /* 播放器保护规则 */
            #movie_player, .html5-video-player {
                position: fixed !important;
                top: 0 !important;
                left: 0 !important;
                width: 100vw !important;
                height: 100vh !important;
                z-index: 9999 !important;
                transform: none !important;
                overflow: visible !important;
                background: #000 !important;
            }

            video.html5-main-video {
                object-fit: contain !important;
                width: 100% !important;
                height: 100% !important;
                display: block !important;
            }

            /* 消除广告残留空间 */
            ytd-rich-item-renderer:empty {
                display: none !important;
            }

            /* 修复剧场模式 */
            ytd-watch-flexy[theater] #player.ytd-watch-flexy {
                top: 0 !important;
                height: 100vh !important;
                padding-top: 0 !important;
            }
        `;
        document.head.appendChild(style);
    };

    // 优化版元素消毒系统
    const elementSanitizer = {
        observer: null,

        init() {
            this.setupObserver();
            this.initialCleanup();
            this.enableEnhancedLayoutProtection();
        },

        setupObserver() {
            this.observer = new MutationObserver(mutations => {
                mutations.forEach(mutation => {
                    if (mutation.type === 'attributes') {
                        this.safeNeutralize(mutation.target);
                    } else if (mutation.addedNodes) {
                        mutation.addedNodes.forEach(node => {
                            if (node.nodeType === 1) this.deepClean(node);
                        });
                    }
                });
            });
            this.observer.observe(document.documentElement, {
                childList: true,
                subtree: true,
                attributeFilter: stealthRules.attributes
            });
        },

        safeNeutralize(element) {
            if (element.id === 'movie_player' || 
                element.classList.contains('html5-video-player')) return;

            if (!element.dataset.stealthProcessed) {
                element.replaceChildren();
                element.style.cssText = `
                    transform: scale(0);
                    height: 0;
                    width: 0;
                    opacity: 0;
                    pointer-events: none;
                `;
                element.dataset.stealthProcessed = "1";
            }
        },

        // 其他原有方法保持但增加播放器保护判断...
    };

    // 新增播放器守护进程
    const playerGuardian = {
        interval: null,
        
        start() {
            this.interval = setInterval(() => {
                const player = document.querySelector('#movie_player');
                if (player) {
                    // 强制重置关键样式
                    player.style.cssText = `
                        position: fixed !important;
                        top: 0 !important;
                        left: 0 !important;
                        width: 100vw !important;
                        height: 100vh !important;
                        z-index: 9999 !important;
                        transform: none !important;
                    `;
                    
                    // 确保视频元素可见
                    const video = player.querySelector('video');
                    if (video) {
                        video.style.cssText = `
                            width: 100% !important;
                            height: 100% !important;
                            object-fit: contain !important;
                        `;
                    }
                }
            }, 3000);
        }
    };

    // 启动系统
    createGhostCSS();
    elementSanitizer.init();
    playerGuardian.start();

    // 其他原有代码保持...
})();