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.

目前为 2021-04-11 提交的版本。查看 最新版本

// ==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://gf.qytechs.cn/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();

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址