隐藏youtube google广告

隐藏youtube显示的google广告,自动点击"skip ad"

当前为 2025-05-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Hide youtube google ad
  3. // @name:zh-CN 隐藏youtube google广告
  4. // @namespace vince.youtube
  5. // @version 2.4.8
  6. // @description hide youtube google ad,auto click "skip ad"
  7. // @description:zh-CN 隐藏youtube显示的google广告,自动点击"skip ad"
  8. // @author vince ding
  9. // @match https://*.youtube.com/*
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_info
  12. // @grant GM_getValue
  13. // @grant unsafeWindow
  14. // @run-at document-start
  15. // @connect googlevideo.com
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20. var closeAd=function (){
  21. //var css = '.video-ads,.video-ads .ad-container .adDisplay,#player-ads,.ytp-ad-module,.ytp-ad-image-overlay,#panels"{ display: none!important; }',
  22. var css = `
  23. .video-ads,
  24. .ytp-ad-overlay-container,
  25. .ytp-ad-overlay-image,
  26. .ytp-ad-skip-button-container,
  27. .ytp-ad-preview-container,
  28. .ytp-ad-message-container,
  29. #masthead-ad,
  30. #player-ads,
  31. ytd-display-ad-renderer,
  32. ytd-companion-slot-renderer,
  33. .ytd-video-masthead-ad-v3-renderer,
  34. .style-scope.ytd-in-feed-ad-layout-renderer,
  35. .ytd-banner-promo-renderer,
  36. #related ytd-promoted-sparkles-web-renderer,
  37. .ytd-promoted-sparkles-text-search-renderer,
  38. .ytd-display-ad-renderer,
  39. .ytd-statement-banner-renderer,
  40. #related ytd-compact-promoted-video-renderer {
  41. display: none!important;
  42. }`;
  43. var head = document.head || document.getElementsByTagName('head')[0];
  44. var style = document.createElement('style');
  45.  
  46. style.type = 'text/css';
  47. if (style.styleSheet){
  48. style.styleSheet.cssText = css;
  49. } else {
  50. style.appendChild(document.createTextNode(css));
  51. }
  52.  
  53. head.appendChild(style);
  54. };
  55. var skipInt;
  56. var log=function(msg){
  57. unsafeWindow.console.log (msg);
  58. };
  59.  
  60.  
  61. var skipAd = function(){
  62. const skipSelectors = [
  63. 'button.ytp-ad-skip-button',
  64. 'button.ytp-ad-skip-button-modern',
  65. '.ytp-ad-skip-button-container button',
  66. '.ytp-ad-skip-button-slot button',
  67. 'button[class*="skip"]',
  68. 'button[class*="Skip"]',
  69. '.videoAdUiSkipButton',
  70. '[data-skip-button]'
  71. ];
  72.  
  73. const skipbtn = skipSelectors.reduce((found, selector) =>
  74. found || document.querySelector(selector), null);
  75.  
  76. const video = document.querySelector('video');
  77. const isInAd = document.querySelector('.video-ads') ||
  78. document.querySelector('.ytp-ad-player-overlay') ||
  79. document.querySelector('[class*="ad-showing"]');
  80.  
  81. if(video) {
  82. if(isInAd && skipbtn) {
  83. // 只在确实是广告且有跳过按钮时执行
  84. if(video.currentTime < video.duration - 1) { // 防止跳到视频末尾
  85. video.playbackRate = 16;
  86. }
  87. } else {
  88. // 非广告状态,恢复正常
  89. if(video.playbackRate !== 1) {
  90. video.playbackRate = 1;
  91. }
  92. // 如果不小心跳到结尾,重置到开始
  93. if(!isInAd && video.currentTime >= video.duration - 1) {
  94. video.currentTime = 0;
  95. }
  96. }
  97. }
  98.  
  99. if(skipbtn && isInAd){
  100. try {
  101. skipbtn.removeAttribute('disabled');
  102. skipbtn.click();
  103. } catch(e) {
  104. log("Skip error: " + e);
  105. }
  106. }
  107.  
  108. setTimeout(() => {
  109. window.requestAnimationFrame(skipAd);
  110. }, 1000);
  111. };
  112. closeAd();
  113. skipAd();
  114.  
  115. })();