Greasy Fork

Greasy Fork is available in English.

2025超级YouTube广告拦截器Pro

下一代AI驱动广告拦截系统,支持YouTube全平台深度清理

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

// ==UserScript==
// @name        2025超级YouTube广告拦截器Pro
// @version     3.1.0
// @description 下一代AI驱动广告拦截系统,支持YouTube全平台深度清理
// @icon        https://www.youtube.com/favicon.ico
// @author      Smart Tools Team
// @match       *://*.youtube.com/*
// @grant       GM_addStyle
// @grant       unsafeWindow
// @run-at      document-start
// @namespace   https://safe.adsguard.com
// @license     MIT
// ==/UserScript==

(function() {
    'use strict';

    // 深度广告选择器系统
    const AdSelectors = {
        youtube: {
            static: [
                'ytd-display-ad-renderer',
                '#player-ads',
                '.ytp-ad-module',
                'ytd-promoted-sparkles-web-renderer',
                'ytd-companion-slot-renderer',
                'ytd-action-companion-ad-renderer',
                'ytd-ad-slot-renderer',
                '.video-ads.ytp-ad-module',
                '.ytp-ad-overlay-container',
                'ytd-in-feed-ad-layout-renderer',
                'ytd-banner-promo-renderer',
                'ytd-carousel-ad-renderer',
                'ytd-ad-break-service-renderer'
            ],
            dynamic: [
                '[class*="ad-"]',
                '[id*="-ad-"]',
                '[data-ad-]',
                '[aria-label*="广告"]',
                '[data-target*="ad."]'
            ]
        },
        global: [
            'div[data-text-ad="1"]',
            'ins.adsbygoogle',
            'iframe[src*="adservice"]',
            'div[aria-label="广告"]'
        ]
    };

    // AI网络拦截规则
    const NetworkRules = {
        blockPatterns: [
            /(ads?|doubleclick|adservice|analytics\.google)/i,
            /\/ad(s|vertising|vert)/i,
            /\/pagead\/|adnxs\.com|adserver\./i,
            /\/promoted_|log_event.+ad_/i
        ],
        safePatterns: [
            /googlevideo\.com\/videoplayback/,
            /\/vi\/.+\/(hqdefault|mqdefault)\.jpg/i,
            /\/generate_204/
        ]
    };

    class AIAdBlocker {
        constructor() {
            this.observer = null;
            this.blockCounter = 0;
            this.lastAdTime = 0;
            this.init();
        }

        init() {
            this.installObserver();
            this.hijackNetwork();
            this.injectProtectionLayer();
            this.createDashboard();
            this.addStyleProtection();
            this.antiDetection();
        }

        // 动态DOM监控系统
        installObserver() {
            this.observer = new MutationObserver(mutations => {
                mutations.forEach(mutation => {
                    this.cleanAds(mutation.addedNodes);
                    this.checkHiddenAds();
                });
            });

            this.observer.observe(document, {
                childList: true,
                subtree: true,
                attributes: true,
                attributeFilter: ['class', 'style', 'src']
            });
        }

        // 智能广告清理系统
        cleanAds(nodes) {
            const selectors = [
                ...AdSelectors.youtube.static,
                ...AdSelectors.youtube.dynamic,
                ...AdSelectors.global
            ].join(',');

            nodes.forEach(node => {
                if (node.nodeType === 1) {
                    // 即时清理
                    node.querySelectorAll?.(selectors)?.forEach(ad => this.destroyAd(ad));
                    if (node.matches?.(selectors)) this.destroyAd(node);
                }
            });

            // 深度清理
            document.querySelectorAll(selectors).forEach(ad => this.destroyAd(ad));
        }

        // 广告元素销毁协议
        destroyAd(element) {
            try {
                element.replaceWith(this.createMarker());
                this.blockCounter++;
                this.updateDashboard();
                this.cleanAdResidues();
                this.blockCounter % 10 === 0 && this.deepClean();
            } catch (e) {
                console.error('Ad removal failed:', e);
            }
        }

        // 网络请求劫持系统
        hijackNetwork() {
            const nativeFetch = window.fetch;
            window.fetch = (input, init) => {
                return this.isAdRequest(input.url) 
                    ? this.blockResponse()
                    : nativeFetch(input, init);
            };

            const nativeOpen = XMLHttpRequest.prototype.open;
            XMLHttpRequest.prototype.open = function(method, url) {
                this._url = url;
                nativeOpen.call(this, method, url);
            };

            XMLHttpRequest.prototype.send = function(...args) {
                this.isAdRequest(this._url) 
                    ? this.abort()
                    : nativeSend.apply(this, args);
            };
        }

        // AI广告请求检测
        isAdRequest(url) {
            return NetworkRules.blockPatterns.some(p => p.test(url)) &&
                  !NetworkRules.safePatterns.some(p => p.test(url));
        }

        // 反检测保护层
        antiDetection() {
            // 伪装广告相关API
            const fakeAPIs = {
                getCompanionAds: () => [],
                getAdState: () => -1,
                getAdsLength: () => 0
            };

            Object.entries(fakeAPIs).forEach(([key, value]) => {
                Object.defineProperty(unsafeWindow, key, { get: () => value });
            });
        }

        // 状态仪表盘
        createDashboard() {
            this.dashboard = document.createElement('div');
            Object.assign(this.dashboard.style, {
                position: 'fixed',
                bottom: '20px',
                right: '20px',
                backgroundColor: 'rgba(0,0,0,0.8)',
                color: '#fff',
                padding: '10px',
                borderRadius: '5px',
                fontSize: '14px',
                zIndex: '9999'
            });
            document.body.appendChild(this.dashboard);
            this.updateDashboard();
        }

        updateDashboard() {
            this.dashboard.innerHTML = `
                🛡️ 广告拦截统计:
                <br>已拦截广告:${this.blockCounter}
                <br>最后拦截:${new Date().toLocaleTimeString()}
                <br>防护状态:<span style="color:#0f0">●</span> 活跃
            `;
        }

        // 其他增强功能...
    }

    // 启动防护系统
    new AIAdBlocker();

    // 样式深度清理
    GM_addStyle(`
        ytd-display-ad-renderer, 
        .ytp-ad-module, 
        [aria-label="广告"] {
            display: none !important;
            opacity: 0 !important;
            height: 0 !important;
            width: 0 !important;
        }
        
        .ytp-ad-progress-list,
        .ytp-ad-skip-button-container {
            visibility: hidden !important;
        }
        
        #player.ytd-watch {
            height: 100vh !important;
        }
    `);
})();