AdBlock for YouTube

removes ads and annoying popups from youtube.

当前为 2024-07-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AdBlock for YouTube
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.5
  5. // @description removes ads and annoying popups from youtube.
  6. // @author FairyRoot
  7. // @match *://*.youtube.com/*
  8. // @exclude *://music.youtube.com/*
  9. // @exclude *://*.music.youtube.com/*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  11. // @grant none
  12. // @license MIT
  13. // @namespace https://github.com/fairy-root
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. // Config
  18. const adblocker = true;
  19. const removePopup = false;
  20. const debugMessages = true;
  21. const fixTimestamps = true;
  22.  
  23. // Variables
  24. let currentUrl = window.location.href;
  25.  
  26. // Setup
  27. log("Script started");
  28.  
  29. if (adblocker) removeAds();
  30. if (removePopup) popupRemover();
  31. if (fixTimestamps) timestampFix();
  32. removeUnwantedElements();
  33.  
  34. function popupRemover() {
  35. setInterval(() => {
  36. const modalOverlay = document.querySelector("tp-yt-iron-overlay-backdrop");
  37. const popup = document.querySelector(".style-scope ytd-enforcement-message-view-model");
  38. const popupButton = document.getElementById("dismiss-button");
  39.  
  40. const video = document.querySelector('video');
  41.  
  42. const bodyStyle = document.body.style;
  43. bodyStyle.setProperty('overflow-y', 'auto', 'important');
  44.  
  45. if (modalOverlay) {
  46. modalOverlay.removeAttribute("opened");
  47. modalOverlay.remove();
  48. }
  49.  
  50. if (popup) {
  51. log("Popup detected, removing...");
  52.  
  53. if (popupButton) popupButton.click();
  54.  
  55. popup.remove();
  56. video.play();
  57.  
  58. setTimeout(() => {
  59. video.play();
  60. }, 500);
  61.  
  62. log("Popup removed");
  63. }
  64.  
  65. if (!video.paused) return;
  66. video.play();
  67. }, 500);
  68. }
  69.  
  70. function removeAds() {
  71. log("removeAds()");
  72.  
  73. let currentUrl = window.location.href;
  74. let isShortsPage = currentUrl.includes("shorts");
  75. let video = document.querySelector('video');
  76.  
  77. // Initial check and setup
  78. if (video && !video.paused && !isShortsPage) {
  79. video.pause();
  80. video.play();
  81. }
  82.  
  83. // Function to handle removing ads and checking video playback
  84. function checkVideoAndRemoveAds() {
  85. const currentHref = window.location.href;
  86.  
  87. // Check if URL has changed
  88. if (currentHref !== currentUrl) {
  89. currentUrl = currentHref;
  90. isShortsPage = currentHref.includes("shorts");
  91. video = document.querySelector('video'); // Re-fetch video element if necessary
  92. }
  93.  
  94. // Check if on YouTube shorts page, ignore if true
  95. if (isShortsPage) {
  96. log("YouTube shorts detected, ignoring...");
  97. return;
  98. }
  99.  
  100. // Check if video element exists and is playing
  101. if (video && !video.paused) {
  102. log("Video detected playing, pausing and playing...");
  103. video.pause();
  104. video.play();
  105. }
  106. }
  107.  
  108. // Initial call to remove ads
  109. checkVideoAndRemoveAds();
  110. removePageAds();
  111. }
  112.  
  113. function removePageAds() {
  114. const sponsor = document.querySelectorAll("div#player-ads.style-scope.ytd-watch-flexy, div#panels.style-scope.ytd-watch-flexy");
  115. const style = document.createElement('style');
  116.  
  117. style.textContent = `
  118. ytd-action-companion-ad-renderer,
  119. ytd-display-ad-renderer,
  120. ytd-video-masthead-ad-advertiser-info-renderer,
  121. ytd-video-masthead-ad-primary-video-renderer,
  122. ytd-in-feed-ad-layout-renderer,
  123. ytd-ad-slot-renderer,
  124. yt-about-this-ad-renderer,
  125. #masthead-ad,
  126. .ytd-promoted-sparkles-text-search-renderer,
  127. .ytd-promoted-sparkles-web-renderer,
  128. .ytd-compact-promoted-video-renderer,
  129. .ytd-video-masthead-ad-v3-renderer,
  130. .ytd-promoted-sparkles-web-renderer,
  131. .ytd-action-companion-ad-renderer,
  132. .ytd-promoted-sparkles-text-search-renderer,
  133. .video-ads,
  134. #player-ads,
  135. #panels
  136. {
  137. display: none !important;
  138. }
  139. `;
  140.  
  141. document.head.appendChild(style);
  142.  
  143. sponsor.forEach((el) => el.style.setProperty("display", "none", "important"));
  144. }
  145.  
  146. function removeUnwantedElements() {
  147. setInterval(() => {
  148. const enforcementMessage = document.querySelector("ytd-enforcement-message-view-model");
  149. if (enforcementMessage) {
  150. log("Enforcement message detected, removing...");
  151. enforcementMessage.remove();
  152. log("Enforcement message removed");
  153. }
  154.  
  155. const ironOverlayBackdrop = document.querySelector("tp-yt-iron-overlay-backdrop");
  156. if (ironOverlayBackdrop) {
  157. log("Iron overlay backdrop detected, removing...");
  158. ironOverlayBackdrop.remove();
  159. log("Iron overlay backdrop removed");
  160. }
  161. }, 5000);
  162. }
  163.  
  164. function log(message) {
  165. if (debugMessages) console.log(`[AdBlock for YouTube] ${message}`);
  166. }
  167. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址