Youtube AdBlock ban bypass

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

< Σχολιασμός για τον κώδικα Youtube AdBlock ban bypass

Αναφορά: Εντάξει - ο κώδικας λειτουργεί αλλά έχει σφάλματα

§
Δημοσιεύτηκε: 31/10/2023

I fixed a problem in the code (the fully updated script is provided below).

What was the problem? The original script will fail if it executes too early.

What did I fix? I made it so that if the script executes too early, then it will wait X number of milliseconds (defined in the variable 'delay') before trying again (up to a maximum number of retires, defined in the variable 'maxTries').

Note: This is based on 20excal07's update (great job), but I excluded his updates relating to backwards browser compatibility (e.g., player.setAttribute('mozallowfullscreen', "mozallowfullscreen") is not needed for modern, updated browsers, as player.setAttribute('allowfullscreen', "allowfullscreen") will suffice). If you're running a severely outdated browser, then update!

// ==UserScript==
// @name Youtube AdBlock ban bypass
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Fix the "Ad blockers violate YouTube's Terms of Service" Error
// @author Obelous
// @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";
const player = document.getElementById("youtube-iframe");
player.setAttribute('src', url);
}
});

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

// 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 url = "https://www.youtube-nocookie.com/embed/" + splitUrl(window.location.href) + "?autoplay=1";
// get the mount point for the iframe
const oldplayer = document.getElementById("error-screen");
// create the iframe
const player = document.createElement('iframe');
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', true);
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');
}

// Execute the code
(function() {
'use strict';
//| |||
// RUN DELAY VVV
//setTimeout(run, 1000);
run();
})();

§
Δημοσιεύτηκε: 02/11/2023

at least enclose the code in preformatted text :p

you can easily do that with Markdown, like this:

```
// put your code here, etc.
// put your code here, etc.
```

Also the reason for the browser compatibility code was because I had reports that full-screen was broken on Firefox until those player attributes were added in. So I might as well put all those in just to be extra safe, even if they might be deprecated. It doesn't hurt the end-user anyway, so why the heck not.

Δημοσίευση απάντησης

Συνδεθείτε για να δημοσιεύσετε μια απάντηση.