Greasy Fork is available in English.

YouTube AdBlock Ban Bypass

Bypass YouTube Adblock Ban

  1. // ==UserScript==
  2. // @name YouTube AdBlock Ban Bypass
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.1
  5. // @description Bypass YouTube Adblock Ban
  6. // @author JJJ
  7. // @match https://www.youtube.com/*
  8. // @match https://www.youtube-nocookie.com/embed/*
  9. // @exclude https://www.youtube.com/*/community
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_registerMenuCommand
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17. (() => {
  18. 'use strict';
  19.  
  20. const CONSTANTS = {
  21. IFRAME_ID: 'adblock-bypass-player',
  22. DELAY: 300,
  23. MAX_TRIES: 150,
  24. DUPLICATE_CHECK_INTERVAL: 7000
  25. };
  26.  
  27. const SELECTORS = {
  28. PLAYABILITY_ERROR: '.yt-playability-error-supported-renderers',
  29. ERROR_SCREEN: '#error-screen',
  30. PLAYER_CONTAINER: '#movie_player'
  31. };
  32.  
  33. let currentUrl = window.location.href;
  34. let tries = 0;
  35.  
  36. const urlUtils = {
  37. extractParams(url) {
  38. try {
  39. const params = new URL(url).searchParams;
  40. return { videoId: params.get('v') };
  41. } catch (e) {
  42. console.error('Failed to extract URL parameters:', e);
  43. return {};
  44. }
  45. },
  46.  
  47. getEmbedUrl(videoId) {
  48. return `https://www.youtube-nocookie.com/embed/${videoId}?autoplay=1&modestbranding=1`;
  49. }
  50. };
  51.  
  52. const playerManager = {
  53. createIframe(videoId) {
  54. const iframe = document.createElement('iframe');
  55. iframe.id = CONSTANTS.IFRAME_ID;
  56. iframe.src = urlUtils.getEmbedUrl(videoId);
  57. iframe.allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture';
  58. iframe.allowFullscreen = true;
  59. iframe.style.cssText = `
  60. height: 100%;
  61. width: 100%;
  62. border: none;
  63. display: block;
  64. margin: 0;
  65. padding: 0;
  66. `;
  67. return iframe;
  68. },
  69.  
  70. replacePlayer(videoId) {
  71. const errorScreen = document.querySelector(SELECTORS.ERROR_SCREEN);
  72. if (!errorScreen) return;
  73.  
  74. let iframe = document.getElementById(CONSTANTS.IFRAME_ID);
  75. if (!iframe) {
  76. iframe = this.createIframe(videoId);
  77. errorScreen.appendChild(iframe);
  78. }
  79. },
  80.  
  81. removeDuplicateIframes() {
  82. const iframes = document.querySelectorAll(`#${CONSTANTS.IFRAME_ID}`);
  83. if (iframes.length > 1) {
  84. Array.from(iframes).slice(1).forEach(iframe => iframe.remove());
  85. }
  86. }
  87. };
  88.  
  89. function handleAdBlockError() {
  90. const playabilityError = document.querySelector(SELECTORS.PLAYABILITY_ERROR);
  91. if (playabilityError) {
  92. playabilityError.remove();
  93. const { videoId } = urlUtils.extractParams(currentUrl);
  94. if (videoId) {
  95. playerManager.replacePlayer(videoId);
  96. }
  97. } else if (tries < CONSTANTS.MAX_TRIES) {
  98. tries++;
  99. setTimeout(handleAdBlockError, CONSTANTS.DELAY);
  100. }
  101. }
  102.  
  103. function setupEventListeners() {
  104. document.addEventListener('yt-navigate-finish', () => {
  105. const newUrl = window.location.href;
  106. if (newUrl !== currentUrl) {
  107. currentUrl = newUrl;
  108. handleAdBlockError();
  109. }
  110. });
  111.  
  112. const observer = new MutationObserver((mutations) => {
  113. for (const mutation of mutations) {
  114. if (mutation.type === 'childList' &&
  115. document.querySelector(SELECTORS.PLAYABILITY_ERROR)) {
  116. handleAdBlockError();
  117. return;
  118. }
  119. }
  120. });
  121. observer.observe(document.body, { childList: true, subtree: true });
  122.  
  123. setInterval(() => playerManager.removeDuplicateIframes(), CONSTANTS.DUPLICATE_CHECK_INTERVAL);
  124. }
  125.  
  126. function initialize() {
  127. setupEventListeners();
  128. handleAdBlockError();
  129. }
  130.  
  131. initialize();
  132. })();