YouTube Ad Skipper2

Skip YouTube ads

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Ad Skipper2
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Skip YouTube ads
// @author       You
// @match        https://www.youtube.com/*
// @grant        none

// ==/UserScript==

(function() {
    'use strict';
// 対象のノードを選択または取得
var targetNode = null;
var movieNode = null;
var duration =null;
    function timeToSeconds(time) {
    var parts = time.split(':').map(Number);
    var seconds = 0;
    if (parts.length === 3) {
        seconds += parts[0] * 3600; // hours to seconds
        seconds += parts[1] * 60;   // minutes to seconds
        seconds += parts[2];        // seconds
    } else if (parts.length === 2) {
        seconds += parts[0] * 60;   // minutes to seconds
        seconds += parts[1];        // seconds
    }
    return seconds;
}


// オブザーバインスタンスを作成
var observer = new MutationObserver(function() {
    if (targetNode && targetNode.hasChildNodes()) {
        console.log("あった");
        duration = document.querySelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > div.ytp-time-display.notranslate > span:nth-child(2) > span.ytp-time-duration").textContent;

        movieNode.currentTime = timeToSeconds(duration);
        [...document.querySelector("#movie_player > div.video-ads.ytp-ad-module").getElementsByClassName('ytp-ad-skip-button-slot')].forEach(e => e.querySelector('button')?.click());
    } else {
        console.log("なかった");
    }
});

// オブザーバの設定
var config = { attributes: true, childList: true, subtree: true };

// 対象ノードとオブザーバの設定を渡す
var checkExist = setInterval(function() {
   targetNode = document.querySelector("#movie_player > div.video-ads.ytp-ad-module");
   movieNode = document.querySelector("#movie_player > div.html5-video-container > video");
   if (targetNode) {
      observer.observe(targetNode, config);
      clearInterval(checkExist);
   }
}, 100); // check every 100ms

})();