Replace YouTube Player with iFrame

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();