Youtube AdBlock ban bypass

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

  1. // ==UserScript==
  2. // @name Youtube AdBlock ban bypass
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.6
  5. // @description Fix the "Ad blockers violate YouTube's Terms of Service" Error
  6. // @author Obelous
  7. // @contributors Master Racer, Insignia Malignia, 20excal07
  8. // @match https://www.youtube.com/*
  9. // @match https://www.youtube-nocookie.com/*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. let currentPageUrl = window.location.href;
  16. const delay = 200; // Milliseconds to wait after a failed attempt
  17. const maxTries = 100; // Maximum number of retries in milliseconds
  18. let tries = 0; // Current number of retries
  19.  
  20. window.addEventListener('beforeunload', function() {
  21. try {
  22. currentPageUrl = window.location.href;
  23. } catch(e) {
  24. console.error('AdBlock Bypass: Failed to preserve URL '+e);
  25. }
  26. });
  27.  
  28. document.addEventListener('yt-page-type-changed', function() {
  29. const newUrl = window.location.href;
  30. // remove the player iframe when the user navigates away from a "watch" page
  31. if (!newUrl.includes("watch")) {
  32. removeIframe();
  33. }
  34. });
  35.  
  36. document.addEventListener('yt-navigate-finish', function () {
  37. setTimeout(() => {
  38. try {
  39. const newUrl = window.location.href;
  40. createIframe(newUrl);
  41. } catch (e) {
  42. console.error('AdBlock Bypass: Failed to refresh player URL after delay', e);
  43. }
  44. }, 100); // 100ms delay to allow URL to update
  45. });
  46.  
  47. // Get the video ID from the URL
  48. function splitUrl(url) {
  49. try {
  50. const params = new URLSearchParams(new URL(url).search);
  51. const videoId = params.get('v');
  52. if (!videoId) {
  53. console.error('AdBlock Bypass: Failed to find video ID in URL');
  54. }
  55. return videoId;
  56. } catch (e) {
  57. console.error('AdBlock Bypass: Failed to parse video ID from URL', e);
  58. return null;
  59. }
  60. }
  61.  
  62.  
  63. // main function
  64. function run() {
  65. try {
  66. const block = document.querySelector('.yt-playability-error-supported-renderers');
  67. if (!block) {
  68. if (tries === maxTries) return;
  69. tries++;
  70. setTimeout(run, delay);
  71. } else {
  72. magic();
  73. }
  74. } catch(e) {
  75. console.error('AdBlock Bypass: Failed to run '+e);
  76. }
  77. }
  78.  
  79. // URL parser
  80. function extractParams(url) {
  81. const urlObj = new URL(url);
  82. const params = new URLSearchParams(urlObj.search);
  83. const videoId = params.get('v');
  84. const playlistId = params.get('list');
  85. const index = params.get('index');
  86. return { videoId, playlistId, index };
  87. }
  88.  
  89. function magic() {
  90. try{
  91. console.log("Loaded");
  92. // remove block screen
  93. const block = document.querySelector('.yt-playability-error-supported-renderers');
  94. if (!block) return;
  95. block.parentNode.removeChild(block);
  96. // get the url for the iframe
  97. const url = window.location.href;
  98. createIframe(url);
  99. console.log('Finished');
  100. } catch(e) {
  101. console.error('AdBlock Bypass: Failed to replace player '+e);
  102. }
  103. }
  104.  
  105. // get the timestamp tag from the video URL, if any
  106. function getTimestampFromUrl(str) {
  107. const timestamp = str.split("t=")[1];
  108. if (timestamp) {
  109. const timeArray = timestamp.split('&')[0].split(/h|m|s/);
  110. // we need to convert into seconds first, since "start=" only supports that unit
  111. if (timeArray.length < 3) {
  112. //seconds only, e.g. "t=30s" or "t=300"
  113. return "&start=" + timeArray[0];
  114. } else if (timeArray.length == 3) {
  115. // minutes & seconds, e.g. "t=1m30s"
  116. const timeInSeconds = (parseInt(timeArray[0]) * 60) + parseInt(timeArray[1]);
  117. return "&start=" + timeInSeconds;
  118. } else {
  119. // hours, minutes & seconds, e.g. "t=1h30m15s"
  120. const timeInSeconds = (parseInt(timeArray[0]) * 3600) + (parseInt(timeArray[1]) * 60) + parseInt(timeArray[2]);
  121. return "&start=" + timeInSeconds;
  122. }
  123. }
  124. return "";
  125. }
  126.  
  127. // bring the iframe to the front - this helps with switching between theater & default mode
  128. function bringToFront(target_id) {
  129. const all_z = [];
  130. document.querySelectorAll("*").forEach(function(elem) {
  131. all_z.push(elem.style.zIndex)
  132. })
  133. const max_index = Math.max.apply(null, all_z.map((x) => Number(x)));
  134. const new_max_index = max_index + 1;
  135. document.getElementById(target_id).style.zIndex = new_max_index;
  136. }
  137.  
  138. function createIframe(newUrl) {
  139. let url = "";
  140. const commonArgs = "autoplay=1&modestbranding=1";
  141.  
  142. const videoId = splitUrl(newUrl);
  143. if (!videoId) {
  144. console.error('AdBlock Bypass: Cannot create iframe, video ID is undefined');
  145. return;
  146. }
  147.  
  148. const timestamp = getTimestampFromUrl(newUrl);
  149. url = `https://www.youtube-nocookie.com/embed/${videoId}?${commonArgs}${timestamp}`;
  150.  
  151. console.log(`Iframe URL: ${url}`);
  152.  
  153. let player = document.getElementById("youtube-iframe");
  154. if (!player) {
  155. const oldplayer = document.getElementById("error-screen");
  156. if (!oldplayer) {
  157. console.error("AdBlock Bypass: Error screen element not found!");
  158. return;
  159. }
  160.  
  161. player = document.createElement('iframe');
  162. setYtPlayerAttributes(player, url);
  163. player.style = "height:100%;width:100%;border-radius:12px;";
  164. player.id = "youtube-iframe";
  165.  
  166. oldplayer.appendChild(player);
  167. } else {
  168. setYtPlayerAttributes(player, url);
  169. }
  170.  
  171. bringToFront("youtube-iframe");
  172. }
  173.  
  174. function removeIframe() {
  175. const player = document.getElementById("youtube-iframe");
  176. if (player && player.parentNode) {
  177. player.parentNode.removeChild(player);
  178. }
  179. }
  180.  
  181. function setYtPlayerAttributes(player, url){
  182. // set all the necessary player attributes here
  183. player.setAttribute('src', url);
  184. player.setAttribute('allow', 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share');
  185. player.setAttribute('frameborder', '0');
  186. player.setAttribute('allowfullscreen', "allowfullscreen");
  187. player.setAttribute('mozallowfullscreen', "mozallowfullscreen");
  188. player.setAttribute('msallowfullscreen', "msallowfullscreen");
  189. player.setAttribute('oallowfullscreen', "oallowfullscreen");
  190. player.setAttribute('webkitallowfullscreen', "webkitallowfullscreen");
  191. }
  192.  
  193. function removeDuplicate() {
  194. const iframes = document.querySelectorAll('#youtube-iframe');
  195. if (iframes.length > 1) {
  196. // Keep only the first iframe and remove the rest
  197. for (let i = 1; i < iframes.length; i++) {
  198. iframes[i].remove();
  199. }
  200. }
  201. }
  202.  
  203. setInterval(removeDuplicate, 5000);
  204. // Execute the code
  205. (function() {
  206. 'use strict';
  207. run();
  208. })();