YAAS (YouTube Ad Auto Skip)

Automatically closes the banner ad or clicks the "Skip ad" button and likes video if you subscribe to the channel.

As of 11.04.2021. See ბოლო ვერსია.

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           YAAS (YouTube Ad Auto Skip)
// @version        2.0.0
// @description    Automatically closes the banner ad or clicks the "Skip ad" button and likes video if you subscribe to the channel.
// @description:uk Автоматично закриває рекламний банер чи клікає по кнопці "Пропустити рекламу" та ставить лайк, якщо є підписка на канал.
// @namespace      https://greasyfork.org/uk/users/741855
// @author         boboha
// @match          https://www.youtube.com/*
// ==/UserScript==

(function() {
    'use strict';

    const SEC = 1000;
    const TO_VIDEO = 1 * SEC;
    const TO_BANER = 0 * SEC;
    const TO_VIDEO_SUBSCIBED = 40 * SEC;
    const TO_BANER_SUBSCIBED = 15 * SEC;
    let skiped = true;
    const log = (msg) => {console.log('[YAAS]', msg);}
    const toggleSkiped = () => {skiped = !skiped;}
    const isSubscribed = () => document.querySelector('#subscribe-button paper-button[subscribed]') ? true : false;
    const checkLike = () => {
        const like = document.querySelector('#info #menu ytd-toggle-button-renderer');
        if (isSubscribed() && !like.className.endsWith('style-default-active')) {
            like.click();
            log('👍 Ok!');
        }
    }
    const skipVideo = (btn) => {
        const timeout = (isSubscribed()) ? TO_VIDEO_SUBSCIBED : TO_VIDEO;
        setTimeout(() => {
            skip(btn);
        }, timeout);
        log('Video skip timeout: ' + timeout/SEC + 'sec');
    }
    const skip = (btn) => {
        toggleSkiped();
        if (btn.nodeType === 1 && getComputedStyle(btn).display === 'inline-block') {
            btn.click();
            log('Video skiped!');
        }
        setTimeout(toggleSkiped, 5000);
    }
    const closeBaner = (btn) => {
        const timeout = (isSubscribed()) ? TO_BANER_SUBSCIBED : TO_BANER;
        setTimeout(() => {
            btn.click();
            log('Baner closed!');
        }, timeout);
        log('Baner close timeout: ' + timeout/SEC + 'sec');
    }
    const observer = new MutationObserver(mutations => {
        if (location.pathname == '/watch') {
            checkLike();
            for (const mutation of mutations) {
                if (skiped) {
                    try {
                        if (mutation.target.className === 'video-ads ytp-ad-module' && mutation.addedNodes.length) {
                            // Video loading
                            if (mutation.addedNodes[0].className === 'ytp-ad-player-overlay') {
                                log('Video loaded...');
                            }
                            // Baner loading
                            else if (mutation.addedNodes[0].className === 'ytp-ad-overlay-slot') {
                                log('Baner loaded...');
                                // Baner closing
                                const close_button = mutation.addedNodes[0].querySelector('.ytp-ad-overlay-close-container > .ytp-ad-overlay-close-button');
                                close_button && closeBaner(close_button);
                            }
                        }

                        // Video skiping
                        if (mutation.target.className === 'ytp-ad-skip-button-slot') {
                            const skip_button = mutation.target.querySelector('.ytp-ad-skip-button-container > .ytp-ad-skip-button');
                            skip_button && skipVideo(skip_button);
                        }
                    } catch (e) {
                        console.groupCollapsed(e.message, mutation.target);
                        console.log(mutation);
                        console.groupEnd();
                    }
                }
            }
        }
    });

    const init = () => {
        const player = document.querySelector('#movie_player');
        if (player) {
            log('Init');
            observer.observe(player, {childList: true, attributes: true, subtree: true});
        } else {
            setTimeout(init, 100);
        }
    }

    init();

})();