Hide youtube google ad

hide youtube google ad,auto click "skip ad"

As of 2018-03-28. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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.1.0
// @description        hide youtube google ad,auto click "skip ad"
// @description:zh-CN  隐藏youtube显示的google广告,自动点击"skip ad"
// @author             vince ding
// @match              *://www.youtube.com/*
// ==/UserScript==

(function() {
    'use strict';
    var closeAd=function (){
        var css = '.video-ads .ad-container .adDisplay,#player-ads{ display: none!important; }',
            head = document.head || document.getElementsByTagName('head')[0],
            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(){
        //ytp-ad-preview-text
        //ytp-ad-skip-button
        var skiptxt=document.querySelector(".ytp-ad-preview-text");
        if(skiptxt){
            var skipText=skiptxt.innerText;
            log(skipText);
            var _textAry=skipText.split(" ");
            var sec=(_textAry.length>0?_textAry[_textAry.length-1]:1);
            log(sec);
            if((sec-1)>0){
                log("wait btn...");
                if(skipInt) {clearTimeout(skipInt);}
                skipInt=setTimeout(skipAd,500);
            }else{
                var skipbtn=document.querySelector(".ytp-ad-skip-button");
                if(skipbtn){
                    log("close ad");
                    skipbtn.click();
                }
                log("checking...");
                if(skipInt) {clearTimeout(skipInt);}
                skipInt=setTimeout(skipAd,500);
            }
        }else{
            log("checking skip text...");
            if(skipInt) {clearTimeout(skipInt);}
            skipInt=setTimeout(skipAd,500);
        }
    };

    closeAd();
    skipAd();

})();