YAAS (YouTube Ads Auto Skip)

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

目前為 2021-05-01 提交的版本,檢視 最新版本

// ==UserScript==
// @name           YAAS (YouTube Ads Auto Skip)
// @description    Automatically closes the banner ad or clicks the "Skip ad" button and likes video if you subscribe to the channel.
// @name:uk        YAAS (Автоматичний пропуск реклами на YouTube)
// @description:uk Автоматично закриває рекламний банер чи клікає по кнопці "Пропустити рекламу" та ставить лайк, якщо є підписка на канал.
// @version        2.2.3
// @namespace      https://gf.qytechs.cn/uk/users/741855
// @author         boboha
// @match          https://www.youtube.com/*
// ==/UserScript==

(function() {
    'use strict';

    let like = false;
    let skiped = true;
    const SEC = 1000,
          TO = 0 * SEC,
          TO_SUBSCIBED = 45 * SEC,
          log = (msg) => {console.log('[YAAS]', msg);},
          isSubscribed = () => document.querySelector('#subscribe-button [subscribed]') ? true : false,
          isLiked = () => document.querySelector('#info #menu ytd-toggle-button-renderer').className.endsWith('style-default-active') ? true : false,
          getTimeout = () => (isSubscribed() || isLiked()) ? TO_SUBSCIBED : TO,
          checkLike = () => {
              if (isSubscribed() && !isLiked()) {
                  document.querySelector('#info #menu ytd-toggle-button-renderer').click();
                  like = true;
                  log('👍 Ok!');
             }
          },
          skipVideo = (btn) => {
              skiped = false;
              btn.addEventListener('click', setSkiped, false);
              setTimeout(() => {
                  skip(btn);
              }, getTimeout());
          },
          setSkiped = () => {skiped = true; log('Video skiped!')},
          skip = (btn) => {
              if (btn.nodeType === 1 && getComputedStyle(btn).display === 'inline-block') {
                  btn.click();
              } else {
                  setTimeout(() => {
                      !skiped && skip(btn);
                  }, 100);
              }
          },
          closeBaner = (btn) => {
              btn.addEventListener('click', setClosed, false);
              setTimeout(() => {
                  btn.click();
              }, getTimeout());
          },
          setClosed = () => {log('Baner closed!');},
          observer = new MutationObserver(mutations => {
              if (location.pathname == '/watch') {
                  !like && checkLike();
                  for (const mutation of mutations) {
                      try {
                          if (mutation.target.className === 'video-ads ytp-ad-module') {
                              if (mutation.addedNodes.length) {
                                  // Video loading
                                  if (mutation.addedNodes[0].className === 'ytp-ad-player-overlay') {
                                      log('Video is loaded...');
                                  }
                                  // Baner loading
                                  else if (mutation.addedNodes[0].className === 'ytp-ad-overlay-slot') {
                                      log('Baner is 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);
                                  }
                              } else if (mutation.removedNodes.length) {
                                  mutation.removedNodes[0].id.startsWith('player-overlay') && log('Video ended');
                              }
                          }

                          // 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);
                          log(mutation);
                          console.groupEnd();
                      }
                  }
              }
          }),
          init = () => {
              const player = document.querySelector('#movie_player');
              if (player) {
                  observer.observe(player, {childList: true, attributes: true, subtree: true});
                  log('Init');
              } else {
                  setTimeout(init, 100);
              }
          }

    init();

})();

QingJ © 2025

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