Youtube AdBlock ban bypass

Fix the "Ad blockers violate YouTube's Terms of Service" Error

< Feedback on Youtube AdBlock ban bypass

Review: OK - script works, but has bugs

§
Posted: 2023-11-03
Edited: 2023-11-03

Hi, it's me again. I went ahead and added support for timestamp tags. :)

I also took the time to clean up the code and moved the player.setAttribute() stuff into its own function, so it can be easily reused, in case such a need arises later.

Again, many thanks to you OP, and everyone else who have contributed so far!

// ==UserScript==
// @name          Youtube AdBlock ban bypass
// @namespace     http://tampermonkey.net/
// @version       1.3
// @description   Fix the "Ad blockers violate YouTube's Terms of Service" Error
// @author        Obelous
// @contributors  Master Racer, Insignia Malignia, 20excal07
// @match         https://www.youtube.com/*
// @match         https://www.youtube-nocookie.com/*
// @icon          https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant         none
// @license       MIT
// ==/UserScript==

let currentPageUrl = window.location.href;
const delay = 200; // Milliseconds to wait after a failed attempt
const maxTries = 100; // Maximum number of retries in milliseconds
let tries = 0; // Current number of retries

window.addEventListener('beforeunload', function() {
    currentPageUrl = window.location.href;
});

document.addEventListener('yt-navigate-finish', function() {
    const newUrl = window.location.href;
    if (newUrl !== currentPageUrl) {
        const url = "https://www.youtube-nocookie.com/embed/" + splitUrl(newUrl) + "?autoplay=1" + getTimestampFromUrl(newUrl);
        const player = document.getElementById("youtube-iframe");
        setYtPlayerAttributes(player, url);
    }
});

// returns the video ID
function splitUrl(str) {
    return str.split('=')[1].split('&')[0];
}

// get the timestamp tag from the video URL, if any
function getTimestampFromUrl(str) {
    const timestamp = str.split("t=")[1];
    if (timestamp) {
        const timeArray = timestamp.split('&')[0].split(/h|m|s/);
        // we need to convert into seconds first, since "start=" only supports that unit
        if (timeArray.length < 3) {
            //seconds only, e.g. "t=30s" or "t=300"
            return "&start=" + timeArray[0];
        } else if (timeArray.length == 3) {
            // minutes & seconds, e.g. "t=1m30s"
            const timeInSeconds = (parseInt(timeArray[0]) * 60) + parseInt(timeArray[1]);
            return "&start=" + timeInSeconds;
        } else {
            // hours, minutes & seconds, e.g. "t=1h30m15s"
            const timeInSeconds = (parseInt(timeArray[0]) * 3600) + (parseInt(timeArray[1]) * 60) + parseInt(timeArray[2]);
            return "&start=" + timeInSeconds;
        }
    }
    return "";
}

// main function
function run() {
    const block = document.querySelector('.yt-playability-error-supported-renderers');
    if (!block) {
        if (tries === maxTries) return;
        tries++;
        setTimeout(run, delay);
    } else {
        magic();
    }
}

function magic() {
    console.log("Loaded");
    // remove block screen
    const block = document.querySelector('.yt-playability-error-supported-renderers');
    if (!block) return;
    block.parentNode.removeChild(block);
    // get the url for the iframe
    const wLocHref = window.location.href;
    const url = "https://www.youtube-nocookie.com/embed/" + splitUrl(wLocHref) + "?autoplay=1" + getTimestampFromUrl(wLocHref);
    // get the mount point for the iframe
    const oldplayer = document.getElementById("error-screen");
    // create the iframe
    const player = document.createElement('iframe');
    setYtPlayerAttributes(player, url);
    player.style = "height:100%;width:100%;border-radius:12px;";
    player.id = "youtube-iframe";
    // append the elements to the DOM
    oldplayer.appendChild(player);
    console.log('Finished');
}

function setYtPlayerAttributes(player, url){
    // set all the necessary player attributes here
    player.setAttribute('src', url);
    player.setAttribute('allow', 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share');
    player.setAttribute('frameborder', '0');
    player.setAttribute('allowfullscreen', "allowfullscreen");
    player.setAttribute('mozallowfullscreen', "mozallowfullscreen");
    player.setAttribute('msallowfullscreen', "msallowfullscreen");
    player.setAttribute('oallowfullscreen', "oallowfullscreen");
    player.setAttribute('webkitallowfullscreen', "webkitallowfullscreen");
}

// Execute the code
(function() {
    'use strict';
    run();
})();
§
Posted: 2023-11-03

Oh, one more thing! Since this uses the embedded version of the YT player, I thought everyone here should know that it's not able to display age-restricted content. However, this other script helps to bypass that restriction, so it's best to use that script alongside this script :)

https://greasyfork.org/en/scripts/423851-simple-youtube-age-restriction-bypass

§
Posted: 2023-11-04
Edited: 2023-11-04

Another fix, haha. I fixed the issue where the video player would continue to play if the user navigates away from the "watch" page (without pausing first), by deleting the iframe anytime they navigate away from them. I added a new yt-page-type-changed listener that specifically works for this, and made sure that the iframe is recreated on the next video if it's been removed, which naturally meant some changes had to be made to the existing yt-navigate-finish listener.

document.addEventListener('yt-page-type-changed', function() {
    const newUrl = window.location.href;
    // remove the player iframe when the user navigates away from a "watch" page
    if (!newUrl.includes("watch")) {
        const player = document.getElementById("youtube-iframe");
        player.parentNode.removeChild(player);
    }
});

document.addEventListener('yt-navigate-finish', function() {
    const newUrl = window.location.href;
    if (newUrl !== currentPageUrl) {
        const url = "https://www.youtube-nocookie.com/embed/" + splitUrl(newUrl) + "?autoplay=1" + getTimestampFromUrl(newUrl);
        let player = document.getElementById("youtube-iframe");
        // recreate the player iframe if it was removed
        if(!player) {
            // get the mount point for the iframe
            const oldplayer = document.getElementById("error-screen");
            // create the iframe
            player = document.createElement('iframe');
            player.style = "height:100%;width:100%;border-radius:12px;";
            player.id = "youtube-iframe";
            // append the elements to the DOM
            oldplayer.appendChild(player);
        }
        setYtPlayerAttributes(player, url);
    }
});
0belousAuthor
§
Posted: 2023-11-11
Edited: 2023-11-11

Could you add your changes to the github at https://github.com/0belous/Youtube-AdBlock-ban-Bypass this just makes it easier to put everyones changes into the next update.
Incase you havent used github before you need to clone the project to download it, make your changes and create a pull request to add your changes, there are many tutorials on google if you need help.

Thank you for contributing by the way I can see you have added a lot.

§
Posted: 2023-11-18
Edited: 2023-11-18

Whoops, I just noticed this. Funny story actually, I do have a Github account, but didn't notice/realise you have this on there as well, so I sorta already created a new repo instead...? I'll get all the changes I have so far and add it to yours, then maybe delete my repo once you've merged everything on your end.

§
Posted: 2023-11-18

Okay, I went ahead and made a pull request on your dev branch. I hope you don't mind! I just liked your attempt to fix the playlist issue, so I wanted to incorporate those. :p

Post reply

Sign in to post a reply.