Advertisement
Kelsondre69

needed youtube ad block

May 19th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.60 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Remove Adblock Thing
  3. // @namespace http://tampermonkey.net/
  4. // @version 5.8
  5. // @description Removes Adblock Thing
  6. // @author JoelMatic
  7. // @match https://www.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @updateURL https://github.com/TheRealJoelmatic/RemoveAdblockThing/raw/main/Youtube-Ad-blocker-Reminder-Remover.user.js
  10. // @downloadURL https://github.com/TheRealJoelmatic/RemoveAdblockThing/raw/main/Youtube-Ad-blocker-Reminder-Remover.user.js
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function()
  15. {
  16. //
  17. // Config
  18. //
  19.  
  20. // Enable The Undetected Adblocker
  21. const adblocker = true;
  22.  
  23. // Enable The Popup remover (pointless if you have the Undetected Adblocker)
  24. const removePopup = false;
  25.  
  26. // Checks for updates (Removes the popup)
  27. const updateCheck = true;
  28.  
  29. // Enable debug messages into the console
  30. const debugMessages = true;
  31.  
  32. // Fix timestamps in the youtube comments for new method
  33. const fixTimestamps = true;
  34.  
  35. // Enable custom modal
  36. // Uses SweetAlert2 library (https://cdn.jsdelivr.net/npm/sweetalert2@11) for the update version modal.
  37. // When set to false, the default window popup will be used. And the library will not be loaded.
  38. const updateModal = {
  39. enable: true, // if true, replaces default window popup with a custom modal
  40. timer: 5000, // timer: number | false
  41. };
  42.  
  43.  
  44. //
  45. // CODE
  46. //
  47. // If you have any suggestions, bug reports,
  48. // or want to contribute to this userscript,
  49. // feel free to create issues or pull requests in the GitHub repository.
  50. //
  51. // GITHUB: https://github.com/TheRealJoelmatic/RemoveAdblockThing
  52.  
  53. //
  54. // Varables used for adblock
  55. //
  56.  
  57. // Store the initial URL
  58. let currentUrl = window.location.href;
  59.  
  60. // Used for after the player is updated
  61. let isVideoPlayerModified = false;
  62.  
  63. //
  64. // Variables used for updater
  65. //
  66.  
  67. let hasIgnoredUpdate = false;
  68.  
  69. //
  70. // Setup
  71. //
  72.  
  73. //Set everything up here
  74. log("Script started");
  75.  
  76. if (adblocker) removeAds();
  77. if (removePopup) popupRemover();
  78. if (updateCheck) checkForUpdate();
  79. if (fixTimestamps) timestampFix();
  80.  
  81. // Remove Them pesski popups
  82. function popupRemover() {
  83.  
  84. setInterval(() => {
  85. const modalOverlay = document.querySelector("tp-yt-iron-overlay-backdrop");
  86. const popup = document.querySelector(".style-scope ytd-enforcement-message-view-model");
  87. const popupButton = document.getElementById("dismiss-button");
  88.  
  89. var video = document.querySelector('video');
  90.  
  91. const bodyStyle = document.body.style;
  92. bodyStyle.setProperty('overflow-y', 'auto', 'important');
  93.  
  94. if (modalOverlay) {
  95. modalOverlay.removeAttribute("opened");
  96. modalOverlay.remove();
  97. }
  98.  
  99. if (popup) {
  100. log("Popup detected, removing...");
  101.  
  102. if(popupButton) popupButton.click();
  103.  
  104. popup.remove();
  105. video.play();
  106.  
  107. setTimeout(() => {
  108. video.play();
  109. }, 500);
  110.  
  111. log("Popup removed");
  112. }
  113. // Check if the video is paused after removing the popup
  114. if (!video.paused) return;
  115. // UnPause The Video
  116. video.play();
  117.  
  118. }, 1000);
  119. }
  120.  
  121. // undetected adblocker method
  122. // undetected adblocker method
  123. function removeAds() {
  124. log("removeAds()");
  125.  
  126. setInterval(() => {
  127.  
  128. if (window.location.href !== currentUrl) {
  129. currentUrl = window.location.href;
  130. isVideoPlayerModified = false;
  131. clearAllPlayers();
  132. removePageAds();
  133. }
  134.  
  135. // Fix for youtube shorts
  136. if (window.location.href.includes("shorts")) {
  137. log("Youtube shorts detected, ignoring...");
  138. return;
  139. }
  140.  
  141. if (isVideoPlayerModified){
  142. removeAllDuplicateVideos();
  143. return;
  144. }
  145.  
  146. log("Video replacement started!");
  147.  
  148. //
  149. // remove ad audio
  150. //
  151.  
  152. var video = document.querySelector('video');
  153. if (video) video.volume = 0;
  154. if (video) video.pause();
  155. if (video) video.remove();
  156.  
  157. //
  158. // Remove the current player
  159. //
  160.  
  161. if (!clearAllPlayers()) {
  162. return;
  163. }
  164.  
  165. /**
  166. * remove the "Ad blockers violate YouTube's Terms of Service" screen for safari
  167. */
  168. let errorScreen = document.querySelector("#error-screen");
  169. if (errorScreen) {
  170. errorScreen.remove();
  171. }
  172.  
  173. //
  174. // Get the video ID from the URL
  175. //
  176.  
  177. let videoID = '';
  178. let playList = '';
  179. let timeStamp = '';
  180. const url = new URL(window.location.href);
  181. const urlParams = new URLSearchParams(url.search);
  182.  
  183. if (urlParams.has('v')) {
  184. videoID = urlParams.get('v');
  185. } else {
  186. const pathSegments = url.pathname.split('/');
  187. const liveIndex = pathSegments.indexOf('live');
  188. if (liveIndex !== -1 && liveIndex + 1 < pathSegments.length) {
  189. videoID = pathSegments[liveIndex + 1];
  190. }
  191. }
  192.  
  193. if (urlParams.has('list')) {
  194. playList = "&listType=playlist&list=" + urlParams.get('list');
  195. }
  196.  
  197. if (urlParams.has('t')) {
  198. timeStamp = "&start=" + urlParams.get('t').replace('s', '');
  199. }
  200.  
  201. if (!videoID) {
  202. log("YouTube video URL not found.", "e");
  203. return null;
  204. }
  205.  
  206. log("Video ID: " + videoID);
  207.  
  208. //
  209. // Create new frame for the video
  210. //
  211.  
  212. const startOfUrl = "https://www.youtube-nocookie.com/embed/";
  213.  
  214. const endOfUrl = "?autoplay=1&modestbranding=1&rel=0";
  215. const finalUrl = startOfUrl + videoID + endOfUrl;
  216.  
  217.  
  218. const iframe = document.createElement('iframe');
  219.  
  220. iframe.setAttribute('src', finalUrl);
  221. iframe.setAttribute('frameborder', '0');
  222. iframe.setAttribute('allow', 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share');
  223. iframe.setAttribute('allowfullscreen', true);
  224. iframe.setAttribute('mozallowfullscreen', "mozallowfullscreen");
  225. iframe.setAttribute('msallowfullscreen', "msallowfullscreen");
  226. iframe.setAttribute('oallowfullscreen', "oallowfullscreen");
  227. iframe.setAttribute('webkitallowfullscreen', "webkitallowfullscreen");
  228.  
  229. iframe.style.width = '100%';
  230. iframe.style.height = '100%';
  231. iframe.style.position = 'absolute';
  232. iframe.style.top = '0';
  233. iframe.style.left = '0';
  234. iframe.style.zIndex = '9999';
  235. iframe.style.pointerEvents = 'all';
  236.  
  237. const videoPlayerElement = document.querySelector('.html5-video-player');
  238. videoPlayerElement.appendChild(iframe);
  239. log("Finished");
  240.  
  241. isVideoPlayerModified = true;
  242. }, 500);
  243. removePageAds();
  244. }
  245. //
  246. // logic functionm
  247. //
  248.  
  249. function removeAllDuplicateVideos() {
  250. const videos = document.querySelectorAll('video');
  251.  
  252. videos.forEach(video => {
  253. if (video.src.includes('www.youtube.com')) {
  254. video.muted = true;
  255. video.pause();
  256. video.addEventListener('volumechange', function() {
  257. if (!video.muted) {
  258. video.muted = true;
  259. video.pause();
  260. log("Video unmuted detected and remuted");
  261. }
  262. });
  263. video.addEventListener('play', function() {
  264. video.pause();
  265. log("Video play detected and repaused");
  266. });
  267.  
  268. log("Duplicate video found and muted");
  269. }
  270. });
  271. }
  272.  
  273. function clearAllPlayers() {
  274.  
  275. const videoPlayerElements = document.querySelectorAll('.html5-video-player');
  276.  
  277. if (videoPlayerElements.length === 0) {
  278. console.error("No elements with class 'html5-video-player' found.");
  279. return false;
  280. }
  281.  
  282. videoPlayerElements.forEach(videoPlayerElement => {
  283. const iframes = videoPlayerElement.querySelectorAll('iframe');
  284. iframes.forEach(iframe => {
  285. iframe.remove();
  286. });
  287. });
  288.  
  289. console.log("Removed all current players!");
  290. return true;
  291. }
  292.  
  293. //removes ads on the page (not video player ads)
  294. function removePageAds(){
  295.  
  296. const sponsor = document.querySelectorAll("div#player-ads.style-scope.ytd-watch-flexy, div#panels.style-scope.ytd-watch-flexy");
  297. const style = document.createElement('style');
  298.  
  299. style.textContent = `
  300. ytd-action-companion-ad-renderer,
  301. ytd-display-ad-renderer,
  302. ytd-video-masthead-ad-advertiser-info-renderer,
  303. ytd-video-masthead-ad-primary-video-renderer,
  304. ytd-in-feed-ad-layout-renderer,
  305. ytd-ad-slot-renderer,
  306. yt-about-this-ad-renderer,
  307. yt-mealbar-promo-renderer,
  308. ytd-statement-banner-renderer,
  309. ytd-ad-slot-renderer,
  310. ytd-in-feed-ad-layout-renderer,
  311. ytd-banner-promo-renderer-background
  312. statement-banner-style-type-compact,
  313. .ytd-video-masthead-ad-v3-renderer,
  314. div#root.style-scope.ytd-display-ad-renderer.yt-simple-endpoint,
  315. div#sparkles-container.style-scope.ytd-promoted-sparkles-web-renderer,
  316. div#main-container.style-scope.ytd-promoted-video-renderer,
  317. div#player-ads.style-scope.ytd-watch-flexy,
  318. ad-slot-renderer,
  319. ytm-promoted-sparkles-web-renderer,
  320. masthead-ad,
  321. tp-yt-iron-overlay-backdrop,
  322.  
  323. #masthead-ad {
  324. display: none !important;
  325. }
  326. `;
  327.  
  328. document.head.appendChild(style);
  329.  
  330. sponsor?.forEach((element) => {
  331. if (element.getAttribute("id") === "rendering-content") {
  332. element.childNodes?.forEach((childElement) => {
  333. if (childElement?.data.targetId && childElement?.data.targetId !=="engagement-panel-macro-markers-description-chapters"){
  334. //Skipping the Chapters section
  335. element.style.display = 'none';
  336. }
  337. });
  338. }
  339. });
  340.  
  341. log("Removed page ads (✔️)");
  342. }
  343.  
  344. function changeTimestamp(timestamp) {
  345. const videoPlayerElements = document.querySelectorAll('.html5-video-player');
  346. videoPlayerElements.forEach(videoPlayerElement => {
  347. const iframes = videoPlayerElement.querySelectorAll('iframe');
  348. iframes.forEach(iframe => {
  349. if (iframe.src.includes("&start=")) {
  350. iframe.src = iframe.src.replace(/&start=\d+/, "&start=" + timestamp);
  351. } else {
  352. iframe.src += "&start=" + timestamp;
  353. }
  354. });
  355. });
  356. }
  357.  
  358. function timestampFix() {
  359. document.addEventListener('click', function(event) {
  360. const target = event.target;
  361.  
  362. if (target.classList.contains('yt-core-attributed-string__link') && target.href.includes('&t=')) {
  363. event.preventDefault();
  364. const timestamp = target.href.split('&t=')[1].split('s')[0];
  365. log(`Timestamp link clicked: ${timestamp} seconds`);
  366. changeTimestamp(timestamp);
  367. }
  368. });
  369. }
  370.  
  371. function observerCallback(mutations) {
  372. let isVideoAdded = mutations.some(mutation =>
  373. Array.from(mutation.addedNodes).some(node => node.tagName === 'VIDEO')
  374. );
  375.  
  376. if (isVideoAdded) {
  377. log("New video detected, checking for duplicates.");
  378. // Ignore for youtube shorts
  379. if (window.location.href.includes("shorts")) {
  380. log("Youtube shorts detected, ignoring...");
  381. return;
  382. }
  383. removeAllDuplicateVideos();
  384. }
  385. }
  386.  
  387. const observer = new MutationObserver(observerCallback);
  388. observer.observe(document.body, { childList: true, subtree: true });
  389.  
  390. //
  391. // Update check
  392. //
  393.  
  394. function checkForUpdate(){
  395.  
  396. if (window.top !== window.self && !(window.location.href.includes("youtube.com"))){
  397. return;
  398. }
  399.  
  400. if (hasIgnoredUpdate){
  401. return;
  402. }
  403.  
  404. const scriptUrl = 'https://raw.githubusercontent.com/TheRealJoelmatic/RemoveAdblockThing/main/Youtube-Ad-blocker-Reminder-Remover.user.js';
  405.  
  406. fetch(scriptUrl)
  407. .then(response => response.text())
  408. .then(data => {
  409. // Extract version from the script on GitHub
  410. const match = data.match(/@version\s+(\d+\.\d+)/);
  411. if (!match) {
  412. log("Unable to extract version from the GitHub script.", "e")
  413. return;
  414. }
  415.  
  416. const githubVersion = parseFloat(match[1]);
  417. const currentVersion = parseFloat(GM_info.script.version);
  418.  
  419. if (githubVersion <= currentVersion) {
  420. log('You have the latest version of the script. ' + githubVersion + " : " + currentVersion);
  421. return;
  422. }
  423.  
  424. console.log('Remove Adblock Thing: A new version is available. Please update your script. ' + githubVersion + " : " + currentVersion);
  425.  
  426. if(updateModal.enable){
  427. // if a version is skipped, don't show the update message again until the next version
  428. if (parseFloat(localStorage.getItem('skipRemoveAdblockThingVersion')) === githubVersion) {
  429. return;
  430. }
  431. // If enabled, include the SweetAlert2 library
  432. const script = document.createElement('script');
  433. script.src = 'https://cdn.jsdelivr.net/npm/sweetalert2@11';
  434. document.head.appendChild(script);
  435.  
  436. const style = document.createElement('style');
  437. style.textContent = '.swal2-container { z-index: 2400; }';
  438. document.head.appendChild(style);
  439.  
  440. // Wait for SweetAlert to be fully loaded
  441. script.onload = function () {
  442.  
  443. Swal.fire({
  444. position: "top-end",
  445. backdrop: false,
  446. title: 'Remove Adblock Thing: New version is available.',
  447. text: 'Do you want to update?',
  448. showCancelButton: true,
  449. showDenyButton: true,
  450. confirmButtonText: 'Update',
  451. denyButtonText:'Skip',
  452. cancelButtonText: 'Close',
  453. timer: updateModal.timer ?? 5000,
  454. timerProgressBar: true,
  455. didOpen: (modal) => {
  456. modal.onmouseenter = Swal.stopTimer;
  457. modal.onmouseleave = Swal.resumeTimer;
  458. }
  459. }).then((result) => {
  460. if (result.isConfirmed) {
  461. window.location.replace(scriptUrl);
  462. } else if(result.isDenied) {
  463. localStorage.setItem('skipRemoveAdblockThingVersion', githubVersion);
  464. }
  465. });
  466. };
  467.  
  468. script.onerror = function () {
  469. var result = window.confirm("Remove Adblock Thing: A new version is available. Please update your script.");
  470. if (result) {
  471. window.location.replace(scriptUrl);
  472. }
  473. }
  474. } else {
  475. var result = window.confirm("Remove Adblock Thing: A new version is available. Please update your script.");
  476.  
  477. if (result) {
  478. window.location.replace(scriptUrl);
  479. }
  480. }
  481. })
  482. .catch(error => {
  483. hasIgnoredUpdate = true;
  484. log("Error checking for updates:", "e", error)
  485. });
  486. hasIgnoredUpdate = true;
  487. }
  488.  
  489. // Used for debug messages
  490. function log(log, level, ...args) {
  491.  
  492. if(!debugMessages)
  493. return;
  494.  
  495. const prefix = '🔧 Remove Adblock Thing:';
  496. const message = `${prefix} ${log}`;
  497. switch (level) {
  498. case 'error':
  499. console.error(`❌ ${message}`, ...args);
  500. break;
  501. case 'log':
  502. console.log(`✅ ${message}`, ...args);
  503. break;
  504. case 'warning':
  505. console.warn(`⚠️ ${message}`, ...args);
  506. break;
  507. default:
  508. console.info(`ℹ️ ${message}`, ...args);
  509. }
  510. }
  511.  
  512. })();
  513.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement