YouTube Auto Skip Ads

autoclicks on youtube's skip ad buttons once they appear, also auto-closes banner ads

Tính đến 13-08-2024. Xem phiên bản mới nhất.

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

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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         YouTube Auto Skip Ads
// @namespace    none
// @version      0.8
// @description  autoclicks on youtube's skip ad buttons once they appear, also auto-closes banner ads
// @author       RustyPrimeLUX
// @match        https://www.youtube.com/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var currentVideoTime = 0;

    var videoPlayer = document.querySelector(".video-stream.html5-main-video");
    var skipAdBtn = document.querySelector("div.ytp-ad-text.ytp-ad-skip-button-text") || document.querySelector(".ytp-skip-ad-button") || document.querySelector(".ytp-skip-ad-button__text");
    var closeBannerBtn = document.querySelector("button.ytp-ad-overlay-close-button");
    var errorScreen = document.querySelector("#error-screen");

    setInterval(function(){
        if (getVideoPlayer() && isVisible(videoPlayer)){
            var currentTime = Math.floor(videoPlayer.currentTime);
            if (currentTime > 30)
            {
                currentVideoTime = currentTime;
            }
        }

        if(getSkipAdsButton() && isVisible(skipAdBtn)){
            skipAdBtn.click();
        }

        if(getCloseBannerButton() && isVisible(closeBannerBtn)){
            closeBannerBtn.click();
        }

        if (getErrorScreen() && isVisible(errorScreen)){
            if (currentVideoTime == 0)
            {
                window.location.reload();
            }
            else{
                var currentLink = window.location.href;
                var playAtTimestampQuery = "&t=";

                if (currentLink.includes(playAtTimestampQuery)){
                    currentLink = currentLink.split(playAtTimestampQuery)[0];
                }

                var linkWithTimestamp = currentLink + playAtTimestampQuery + currentVideoTime;
                window.location.href = linkWithTimestamp;
            }
        }
    }, 1000);

    function getVideoPlayer(){
        if (videoPlayer == null || (videoPlayer != null && !document.body.contains(videoPlayer))){
            videoPlayer = document.querySelector(".video-stream.html5-main-video");
        }
        return videoPlayer != null;
    }

    function getSkipAdsButton() {
        if (skipAdBtn == null || (skipAdBtn != null && !document.body.contains(skipAdBtn))){
            skipAdBtn = document.querySelector("div.ytp-ad-text.ytp-ad-skip-button-text") || document.querySelector(".ytp-skip-ad-button") || document.querySelector(".ytp-skip-ad-button__text");
        }
        return skipAdBtn != null;
    }

    function getCloseBannerButton() {
        if (closeBannerBtn == null || (closeBannerBtn != null && !document.body.contains(closeBannerBtn))){
            closeBannerBtn = document.querySelector("button.ytp-ad-overlay-close-button");
        }
        return closeBannerBtn != null;
    }

    function getErrorScreen(){
        if (errorScreen == null || (errorScreen != null && !document.body.contains(errorScreen))){
            errorScreen = document.querySelector("#error-screen");
        }
        return errorScreen != null;
    }

    function isVisible(el) {
        return (el.offsetParent !== null)
    }
})();