Replace YouTube Player with iFrame

繞過 YouTube 禁用廣告攔截器 (簡易版)

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Replace YouTube Player with iFrame
// @version      1.0
// @description  繞過 YouTube 禁用廣告攔截器 (簡易版)
// @license      MIT
// @homepage     https://github.com/Jacky97s
// @homepageURL  https://github.com/Jacky97s
// @website      https://github.com/Jacky97s
// @source       https://github.com/Jacky97s/bypass-youtube-adblock-blocker/raw/main/src/ReplaceYouTubePlayerWithIFrame.js
// @namespace    https://github.com/Jacky97s/bypass-youtube-adblock-blocker/raw/main/src/ReplaceYouTubePlayerWithIFrame.js
// @match        https://www.youtube.com/watch*
// @author       Jacky97s
// @run-at       document-idle
// @icon         https://www.google.com/s2/favicons?sz=64&domain=sinopac.com
// ==/UserScript==

(function() {
    'use strict';

    // Check if player element exists
    function isPlayerExists() {
        const playerXPath = '//*[@id="player"]';
        const player = document.evaluate(playerXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

        if (player) {
            return player;
        }
    }

    // Replace Player with iframe
    function replacePlayer() {
        const player = isPlayerExists();

        if (player == null) {
            return;
        }

        // Define the video ID from the URL
        const urlParams = new URLSearchParams(window.location.search);
        const videoId = urlParams.get('v');

        // Create the iFrame element
        const iframe = document.createElement('iframe');
        const width = player.offsetWidth;
        const height = width * 0.56;

        iframe.id = "iframe-player"
        iframe.width = '100%';
        iframe.height = height.toString();
        iframe.src = `https://www.youtube.com/embed/${videoId}?feature=oembed&autoplay=1`;
        iframe.frameBorder = '0';
        iframe.allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture';
        iframe.allowFullscreen = true;

        // Replace player with iframe
        player.parentNode.replaceChild(iframe, player);
    }

    // Add a debounce function to limit the rate of execution
    function debounce(func, delay) {
        let timeout;
        return function () {
            const context = this;
            const args = arguments;
            clearTimeout(timeout);
            timeout = setTimeout(() => {
                func.apply(context, args);
            }, delay);
        };
    }

    const debouncedReplacePlayer = debounce(replacePlayer, 500); // Adjust the delay as needed

    // Listen for the 'DOMNodeRemoved' event
    document.addEventListener('DOMNodeRemoved', debouncedReplacePlayer);
})();