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',
            '[data-ad-metadata]',
            'div#player-ads.ytd-watch:not(#movie_player)',
            'ytd-rich-section-renderer:has(ytd-ad-)',
            'div[class*="-ad-"]:not([data-legitimate])',
            'div.ytp-ad-module'
        ],
        attributes: [
            'ad-showing',
            'data-ad-status',
            'ad-type'
        ]
    };

    // 新版CSS布局系统
    const createSmartCSS = () => {
        const style = document.createElement('style');
        style.textContent = `
            /* 广告隐藏规则 */
            ${stealthRules.selectors.join(',')} {
                transform: scale(0) !important;
                height: 0 !important;
                width: 0 !important;
                opacity: 0 !important;
                pointer-events: none !important;
                position: absolute !important;
                z-index: -1 !important;
            }

            /* 智能播放器定位 */
            #movie_player, .html5-video-player {
                position: relative !important;
                width: 100% !important;
                height: auto !important;
                min-height: 480px;
                z-index: 999 !important;
                transform: none !important;
            }

            /* 视频自适应 */
            video.html5-main-video {
                width: 100% !important;
                height: auto !important;
                max-height: 90vh;
                object-fit: contain !important;
            }

            /* 修复剧场模式 */
            ytd-watch-flexy[theater] #player.ytd-watch-flexy {
                top: 0 !important;
                height: calc(100vh - 60px) !important;
            }

            /* 保留广告空间防止布局错位 */
            ytd-rich-item-renderer:empty {
                height: 0 !important;
                min-height: 0 !important;
            }
        `;
        document.head.appendChild(style);
    };

    // 智能布局守护系统
    const layoutGuard = {
        init() {
            this.observePlayer();
            this.preventFullscreenOverride();
        },

        observePlayer() {
            new MutationObserver((mutations) => {
                mutations.forEach(mutation => {
                    if (mutation.attributeName === 'class') {
                        this.handlePlayerClassChange(mutation.target);
                    }
                });
            }).observe(document.getElementById('movie_player'), {
                attributes: true
            });
        },

        handlePlayerClassChange(player) {
            // 当YouTube自身触发全屏时保持原功能
            if (player.classList.contains('ytp-fullscreen')) {
                player.style.cssText = '';
            } else {
                player.style.cssText = `
                    position: relative !important;
                    width: 100% !important;
                    height: auto !important;
                `;
            }
        },

        preventFullscreenOverride() {
            Object.defineProperty(document, 'fullscreenElement', {
                get: () => null,
                configurable: true
            });
        }
    };

    // 启动系统
    createSmartCSS();
    layoutGuard.init();

    // 其他原有广告拦截逻辑保持不变...
})();