🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

YouTube Ad Blocker (Updated)

Blocks YouTube ads effectively without detection.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         YouTube Ad Blocker (Updated)
// @namespace    https://example.com/
// @version      1.0
// @description  Blocks YouTube ads effectively without detection.
// @author       hunter
// @match        *://*.youtube.com/*
// @grant        GM_addStyle
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const adSelectors = [
        '.ytp-ad-player-overlay',
        '.ytp-ad-module',
        '.ytp-ad-text',
        '.ad-interrupting',
        '.video-ads',
        '.ytp-ad-image-overlay'
    ];

    const adPatterns = [
        'googleads.g.doubleclick.net',
        'youtube.com/api/stats/playback',
        'youtube.com/get_video_info'
    ];

    // Block network ads (fetch and XHR)
    const blockNetworkAds = () => {
        const originalFetch = window.fetch;
        window.fetch = function(url, options) {
            if (adPatterns.some(pattern => url.includes(pattern))) {
                console.log('Blocked fetch request:', url);
                return new Promise(() => {});
            }
            return originalFetch.apply(this, arguments);
        };

        const originalXhrOpen = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function(method, url) {
            if (adPatterns.some(pattern => url.includes(pattern))) {
                console.log('Blocked XMLHttpRequest:', url);
                return;
            }
            return originalXhrOpen.apply(this, arguments);
        };
    };

    // MutationObserver to hide ads
    const observer = new MutationObserver(() => {
        adSelectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(ad => {
                ad.style.display = 'none';  // Hide the ad instead of removing it
            });
        });
    });

    const startObserver = () => {
        observer.observe(document.body, { childList: true, subtree: true });
    };

    // Initialize the script after the video player is loaded
    const initializeObserver = () => {
        const player = document.querySelector('video');
        if (player) {
            startObserver();
        } else {
            setTimeout(initializeObserver, 1000); // Retry after 1 second if player isn't found
        }
    };

    // Inject styles to hide ads without affecting the video player
    GM_addStyle(`
        ${adSelectors.filter(selector => !selector.includes('video')).join(', ')} {
            display: none !important;
            visibility: hidden !important;
        }
    `);

    // Initialize the script
    blockNetworkAds();
    initializeObserver();

})();