Hide youtube google ad

hide youtube google ad,auto click "skip ad"

ของเมื่อวันที่ 03-05-2025 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey to install this script.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name               Hide youtube google ad
// @name:zh-CN         隐藏youtube google广告
// @namespace          vince.youtube
// @version            2.4.8
// @description        hide youtube google ad,auto click "skip ad"
// @description:zh-CN  隐藏youtube显示的google广告,自动点击"skip ad"
// @author             vince ding
// @match        https://*.youtube.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_info
// @grant        GM_getValue
// @grant        unsafeWindow
// @run-at       document-start
// @connect      googlevideo.com
// ==/UserScript==

(function() {
    'use strict';
    var closeAd=function (){
        //var css = '.video-ads,.video-ads .ad-container .adDisplay,#player-ads,.ytp-ad-module,.ytp-ad-image-overlay,#panels"{ display: none!important; }',
        var css = `
            .video-ads,
            .ytp-ad-overlay-container,
            .ytp-ad-overlay-image,
            .ytp-ad-skip-button-container,
            .ytp-ad-preview-container,
            .ytp-ad-message-container,
            #masthead-ad,
            #player-ads,
            ytd-display-ad-renderer,
            ytd-companion-slot-renderer,
            .ytd-video-masthead-ad-v3-renderer,
            .style-scope.ytd-in-feed-ad-layout-renderer,
            .ytd-banner-promo-renderer,
            #related ytd-promoted-sparkles-web-renderer,
            .ytd-promoted-sparkles-text-search-renderer,
            .ytd-display-ad-renderer,
            .ytd-statement-banner-renderer,
            #related ytd-compact-promoted-video-renderer {
                display: none!important;
            }`;
            var head = document.head || document.getElementsByTagName('head')[0];
            var style = document.createElement('style');

        style.type = 'text/css';
        if (style.styleSheet){
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }

        head.appendChild(style);
    };
    var skipInt;
    var log=function(msg){
       unsafeWindow.console.log (msg);
    };


   var skipAd = function(){
        const skipSelectors = [
            'button.ytp-ad-skip-button',
            'button.ytp-ad-skip-button-modern',
            '.ytp-ad-skip-button-container button',
            '.ytp-ad-skip-button-slot button',
            'button[class*="skip"]',
            'button[class*="Skip"]',
            '.videoAdUiSkipButton',
            '[data-skip-button]'
        ];

        const skipbtn = skipSelectors.reduce((found, selector) =>
            found || document.querySelector(selector), null);

        const video = document.querySelector('video');
        const isInAd = document.querySelector('.video-ads') ||
                      document.querySelector('.ytp-ad-player-overlay') ||
                      document.querySelector('[class*="ad-showing"]');

        if(video) {
            if(isInAd && skipbtn) {
                // 只在确实是广告且有跳过按钮时执行
                if(video.currentTime < video.duration - 1) { // 防止跳到视频末尾
                    video.playbackRate = 16;
                }
            } else {
                // 非广告状态,恢复正常
                if(video.playbackRate !== 1) {
                    video.playbackRate = 1;
                }
                // 如果不小心跳到结尾,重置到开始
                if(!isInAd && video.currentTime >= video.duration - 1) {
                    video.currentTime = 0;
                }
            }
        }

        if(skipbtn && isInAd){
            try {
                skipbtn.removeAttribute('disabled');
                skipbtn.click();
            } catch(e) {
                log("Skip error: " + e);
            }
        }

        setTimeout(() => {
            window.requestAnimationFrame(skipAd);
        }, 1000);
    };
    closeAd();
    skipAd();

})();