YouTube DeBlock

Semi-working 2023 adblock for YouTube video. Uses "yout-ube.com" and replaces all videos with embeds.

当前为 2023-10-10 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube DeBlock
// @description  Semi-working 2023 adblock for YouTube video. Uses "yout-ube.com" and replaces all videos with embeds.
// @author       YelloNolo
// @version      0.9
// @date         2023-10-10
// @namespace    https://yello.zip
// @homepage     https://github.com/YelloNolo/YouTube-Adblock
// @match        *://www.youtube.com/*
// @grant        none
// ==/UserScript==

(function () {
    // Function remove the "ad" from the page. It finds the "ad" by searching for the class name.
    function removeElementsByClassName(className) {
        const elements = document.querySelectorAll('.' + className);
        elements.forEach(element => {
            element.remove();
        });
    }

    // Function that replaces the video with an embed of "yout-ube.com". It finds the video by searching for the class name.
    function replaceVideoToEmbed(className) {
        const currentURL = window.location.href;
        const newURL = getNewURL();
        const elements = document.querySelectorAll("." + className);

        elements.forEach(element => {
            const iframe = document.createElement('iframe');
            iframe.src = newURL;
            iframe.width = '100%';
            iframe.height = '100%';
            iframe.zIndex = '9999';
            iframe.allowfullscreen = 'true';

            // Replace the existing element with the custom URL
            element.parentNode.replaceChild(iframe, element);
            console.log("Modified URL:", newURL);
        });
    }

    // Function to replace "youtube.com" with "yout-ube.com"
    function getNewURL() {
        const currentURL = window.location.href;
        if (currentURL.includes("youtube.com")) {
            const newURL = currentURL.replace("youtube.com", "yout-ube.com");
            return newURL;
        }
    }

    // Check for text "This video is unavailable" and reloads script if found 5 times.
    function checkForUnavailable() {
        const textToFind = "This video is unavailable";
        const pageText = document.body.textContent;
        const iframe = document.getElementById('iframe');
        const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
        const iframeText = iframeDocument.body.textContent;

        setTimeout(function() {
            if (iframeText.includes(textToFind)) {
                iframe.contentWindow.location.reload();
                console.log("A");

            }
        }, 1000);
    }

    function isBlocked() {

        return false;
    }

    // Just for testing...
    function test() {
        const testNewURL = getNewURL();
        console.log(testNewURL);
    }

    // Main function for organising actions
    function removeAndReplace() {
        removeElementsByClassName('ytd-enforcement-message-view-model');
        replaceVideoToEmbed('yt-playability-error-supported-renderers');
        //setTimeout(function() { checkForUnavailable(); }, 3000); checkForUnavailable();
    }

    // Function to run when the button is clicked
    function onButtonClick() {
        removeAndReplace();
        //test();
    }

    // Function that checks if the page is even blocked
    function onCheckForBlock() {
        null;
    }

    //window.addEventListener('load', onCheckForBlock);

    // Button for testing. I've yet to get the page to auto load. It also helps with debugging.
    const button = document.createElement('button');
    button.textContent = 'Remove Enforcement';
    button.style.position = 'fixed';
    button.style.top = '10px';
    button.style.right = '10px';
    button.style.zIndex = '9999';
    button.addEventListener('click', onButtonClick);

    document.body.appendChild(button);

})();