自动跳过 YouTube 广告

立即自动跳过 YouTube 广告。不会被 YouTube 广告拦截器警告检测到。

当前为 2025-01-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Auto Skip YouTube Ads
  3. // @name:ar تخطي إعلانات YouTube تلقائيًا
  4. // @name:es Saltar Automáticamente Anuncios De YouTube
  5. // @name:fr Ignorer Automatiquement Les Publicités YouTube
  6. // @name:hi YouTube विज्ञापन स्वचालित रूप से छोड़ें
  7. // @name:id Lewati Otomatis Iklan YouTube
  8. // @name:ja YouTube 広告を自動スキップ
  9. // @name:ko YouTube 광고 자동 건너뛰기
  10. // @name:nl YouTube-Advertenties Automatisch Overslaan
  11. // @name:pt-BR Pular Automaticamente Anúncios Do YouTube
  12. // @name:ru Автоматический Пропуск Рекламы На YouTube
  13. // @name:vi Tự Động Bỏ Qua Quảng Cáo YouTube
  14. // @name:zh-CN 自动跳过 YouTube 广告
  15. // @name:zh-TW 自動跳過 YouTube 廣告
  16. // @namespace https://github.com/tientq64/userscripts
  17. // @version 6.0.0
  18. // @description Automatically skip YouTube ads instantly. Undetected by YouTube ad blocker warnings.
  19. // @description:ar تخطي إعلانات YouTube تلقائيًا على الفور. دون أن يتم اكتشاف ذلك من خلال تحذيرات أداة حظر الإعلانات في YouTube.
  20. // @description:es Omite automáticamente los anuncios de YouTube al instante. Sin que te detecten las advertencias del bloqueador de anuncios de YouTube.
  21. // @description:fr Ignorez automatiquement et instantanément les publicités YouTube. Non détecté par les avertissements du bloqueur de publicités YouTube.
  22. // @description:hi YouTube विज्ञापनों को स्वचालित रूप से तुरंत छोड़ दें। YouTube विज्ञापन अवरोधक चेतावनियों द्वारा पता नहीं लगाया गया।
  23. // @description:id Lewati iklan YouTube secara otomatis secara instan. Tidak terdeteksi oleh peringatan pemblokir iklan YouTube.
  24. // @description:ja YouTube 広告を即座に自動的にスキップします。YouTube 広告ブロッカーの警告には検出されません。
  25. // @description:ko YouTube 광고를 즉시 자동으로 건너뜁니다. YouTube 광고 차단 경고에 감지되지 않습니다.
  26. // @description:nl Sla YouTube-advertenties direct automatisch over. Ongemerkt door YouTube-adblockerwaarschuwingen.
  27. // @description:pt-BR Pule anúncios do YouTube instantaneamente. Não detectado pelos avisos do bloqueador de anúncios do YouTube.
  28. // @description:ru Автоматически пропускать рекламу YouTube мгновенно. Не обнаруживается предупреждениями блокировщиков рекламы YouTube.
  29. // @description:vi Tự động bỏ qua quảng cáo YouTube ngay lập tức. Không bị phát hiện bởi cảnh báo trình chặn quảng cáo của YouTube.
  30. // @description:zh-CN 立即自动跳过 YouTube 广告。不会被 YouTube 广告拦截器警告检测到。
  31. // @description:zh-TW 立即自動跳過 YouTube 廣告。 YouTube 廣告攔截器警告未被偵測到。
  32. // @author tientq64
  33. // @icon https://cdn-icons-png.flaticon.com/64/2504/2504965.png
  34. // @match https://www.youtube.com/*
  35. // @match https://m.youtube.com/*
  36. // @grant none
  37. // @license MIT
  38. // @compatible firefox
  39. // @compatible chrome
  40. // @compatible opera
  41. // @compatible safari
  42. // @compatible edge
  43. // @noframes
  44. // @homepage https://github.com/tientq64/userscripts/tree/main/scripts/Auto-Skip-YouTube-Ads
  45. // ==/UserScript==
  46.  
  47. function skipAd() {
  48. const isYouTubeShort = checkIsYouTubeShort()
  49. if (isYouTubeShort) return
  50.  
  51. const hasAd = checkHasAd()
  52. if (!hasAd) return
  53.  
  54. const player = getYouTubePlayer()
  55. if (player === null) return
  56.  
  57. const videoData = player.getVideoData()
  58. const videoId = videoData.video_id
  59. const startTime = Math.floor(player.getCurrentTime())
  60. player.loadVideoById(videoId, startTime)
  61.  
  62. console.log('Ad skipped!', videoId, startTime, videoData.title)
  63. }
  64.  
  65. /**
  66. * Check if there are any ads interrupting the video.
  67. */
  68. function checkHasAd() {
  69. // This element appears when a video ad appears.
  70. const adShowing = document.querySelector('.ad-showing')
  71. if (adShowing !== null) return true
  72.  
  73. // Timed pie countdown ad.
  74. const pieCountdown = document.querySelector('.ytp-ad-timed-pie-countdown-container')
  75. if (pieCountdown !== null) return true
  76.  
  77. return false
  78. }
  79.  
  80. function checkIsYouTubeShort() {
  81. return location.pathname.startsWith('/shorts/')
  82. }
  83.  
  84. /**
  85. * Finds and returns the current YouTube video player.
  86. *
  87. * @returns The current YouTube video player, or `null` if not found.
  88. */
  89. function getYouTubePlayer() {
  90. let player
  91. if (isYouTubeMobile) {
  92. const playerEl = document.querySelector('#movie_player')
  93. player = playerEl
  94. } else {
  95. const playerEl = document.querySelector('#ytd-player')
  96. if (playerEl === null) return null
  97. player = playerEl.getPlayer()
  98. }
  99. return player
  100. }
  101.  
  102. function addCss() {
  103. const adsSelectors = [
  104. // Ad banner in the upper right corner, above the video playlist.
  105. '#player-ads',
  106.  
  107. // Masthead ad on home page.
  108. '#masthead-ad',
  109.  
  110. 'ytd-ad-slot-renderer',
  111. '.ytp-suggested-action',
  112. '.yt-mealbar-promo-renderer',
  113.  
  114. // Featured product ad banner at the bottom left of the video.
  115. '.ytp-featured-product',
  116.  
  117. // Products shelf ad banner below the video description.
  118. 'ytd-merch-shelf-renderer',
  119.  
  120. // YouTube Music Premium trial promotion dialog, bottom left corner.
  121. 'ytmusic-mealbar-promo-renderer',
  122.  
  123. // YouTube Music Premium trial promotion banner on home page.
  124. 'ytmusic-statement-banner-renderer'
  125. ]
  126. const adsSelector = adsSelectors.join(',')
  127. const css = `${adsSelector} { display: none !important; }`
  128. const style = document.createElement('style')
  129. style.textContent = css
  130. document.head.appendChild(style)
  131. }
  132.  
  133. /**
  134. * Remove ad elements using JavaScript because these selectors require the use of the CSS
  135. * `:has` selector which is not supported in older browser versions.
  136. */
  137. function removeAdElements() {
  138. const adSelectors = [
  139. // Ad banner in the upper right corner, above the video playlist.
  140. ['#panels', 'ytd-engagement-panel-section-list-renderer[target-id="engagement-panel-ads"]'],
  141.  
  142. // Sponsored ad video items on home page.
  143. ['ytd-rich-item-renderer', '.ytd-ad-slot-renderer'],
  144.  
  145. ['ytd-rich-section-renderer', '.ytd-statement-banner-renderer'],
  146.  
  147. // Ad videos on YouTube Short.
  148. ['ytd-reel-video-renderer', '.ytd-ad-slot-renderer'],
  149.  
  150. // Ad blocker warning dialog.
  151. ['tp-yt-paper-dialog', '#feedback.ytd-enforcement-message-view-model'],
  152.  
  153. // Survey dialog on home page, located at bottom right.
  154. ['tp-yt-paper-dialog', ':scope > ytd-checkbox-survey-renderer'],
  155.  
  156. // Survey to rate suggested content, located at bottom right.
  157. ['tp-yt-paper-dialog', ':scope > ytd-single-option-survey-renderer']
  158. ]
  159. for (const adSelector of adSelectors) {
  160. const adEl = document.querySelector(adSelector[0])
  161. if (adEl === null) continue
  162. const neededEl = document.querySelector(adSelector[1])
  163. if (neededEl === null) continue
  164. adEl.remove()
  165. }
  166. }
  167.  
  168. const isYouTubeMobile = location.hostname === 'm.youtube.com'
  169.  
  170. window.setInterval(skipAd, 500)
  171. window.setInterval(removeAdElements, 1000)
  172.  
  173. addCss()
  174. skipAd()