YouTube DeBlock

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

目前為 2023-10-10 提交的版本,檢視 最新版本

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

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

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

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

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

})();