YouTube AdBlock Ban Bypass

Bypass YouTube Adblock Ban

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name           YouTube AdBlock Ban Bypass
// @namespace      http://tampermonkey.net/
// @version        0.1.1
// @description    Bypass YouTube Adblock Ban
// @author         JJJ
// @match          https://www.youtube.com/*
// @match          https://www.youtube-nocookie.com/embed/*
// @exclude        https://www.youtube.com/*/community
// @icon           https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant          GM_getValue
// @grant          GM_setValue
// @grant          GM_registerMenuCommand
// @license        MIT
// ==/UserScript==

(() => {
    'use strict';

    const CONSTANTS = {
        IFRAME_ID: 'adblock-bypass-player',
        DELAY: 300,
        MAX_TRIES: 150,
        DUPLICATE_CHECK_INTERVAL: 7000
    };

    const SELECTORS = {
        PLAYABILITY_ERROR: '.yt-playability-error-supported-renderers',
        ERROR_SCREEN: '#error-screen',
        PLAYER_CONTAINER: '#movie_player'
    };

    let currentUrl = window.location.href;
    let tries = 0;

    const urlUtils = {
        extractParams(url) {
            try {
                const params = new URL(url).searchParams;
                return { videoId: params.get('v') };
            } catch (e) {
                console.error('Failed to extract URL parameters:', e);
                return {};
            }
        },

        getEmbedUrl(videoId) {
            return `https://www.youtube-nocookie.com/embed/${videoId}?autoplay=1&modestbranding=1`;
        }
    };

    const playerManager = {
        createIframe(videoId) {
            const iframe = document.createElement('iframe');
            iframe.id = CONSTANTS.IFRAME_ID;
            iframe.src = urlUtils.getEmbedUrl(videoId);
            iframe.allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture';
            iframe.allowFullscreen = true;
            iframe.style.cssText = `
                height: 100%;
                width: 100%;
                border: none;
                display: block;
                margin: 0;
                padding: 0;
            `;
            return iframe;
        },

        replacePlayer(videoId) {
            const errorScreen = document.querySelector(SELECTORS.ERROR_SCREEN);
            if (!errorScreen) return;

            let iframe = document.getElementById(CONSTANTS.IFRAME_ID);
            if (!iframe) {
                iframe = this.createIframe(videoId);
                errorScreen.appendChild(iframe);
            }
        },

        removeDuplicateIframes() {
            const iframes = document.querySelectorAll(`#${CONSTANTS.IFRAME_ID}`);
            if (iframes.length > 1) {
                Array.from(iframes).slice(1).forEach(iframe => iframe.remove());
            }
        }
    };

    function handleAdBlockError() {
        const playabilityError = document.querySelector(SELECTORS.PLAYABILITY_ERROR);
        if (playabilityError) {
            playabilityError.remove();
            const { videoId } = urlUtils.extractParams(currentUrl);
            if (videoId) {
                playerManager.replacePlayer(videoId);
            }
        } else if (tries < CONSTANTS.MAX_TRIES) {
            tries++;
            setTimeout(handleAdBlockError, CONSTANTS.DELAY);
        }
    }

    function setupEventListeners() {
        document.addEventListener('yt-navigate-finish', () => {
            const newUrl = window.location.href;
            if (newUrl !== currentUrl) {
                currentUrl = newUrl;
                handleAdBlockError();
            }
        });

        const observer = new MutationObserver((mutations) => {
            for (const mutation of mutations) {
                if (mutation.type === 'childList' &&
                    document.querySelector(SELECTORS.PLAYABILITY_ERROR)) {
                    handleAdBlockError();
                    return;
                }
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });

        setInterval(() => playerManager.removeDuplicateIframes(), CONSTANTS.DUPLICATE_CHECK_INTERVAL);
    }

    function initialize() {
        setupEventListeners();
        handleAdBlockError();
    }

    initialize();
})();