YouTube自动点击跳过广告

油管自动点击跳过广告

// ==UserScript==
// @name         YouTube自动点击跳过广告
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  油管自动点击跳过广告
// @match        https://www.youtube.com/*
// @author       无名尸
// @license      MIT
// ==/UserScript==

(() => {
  'use strict';
  // 定义一个函数,用于启动定时器并监视目标元素的出现
  function startTimer() {
    // 执行定时器,每 500 毫秒检查一次是否存在指定的元素
        var intervalId = setInterval(function() {
            // 找到要点击的 div 元素
        var div = document.querySelector('.ytp-skip-ad-button');
        // 如果找到了元素,就模拟点击它
                if (div) {
                    console.log('已跳过!');
                    div.click();
                    clearInterval(intervalId); // 停止定时器
                }
            }
        , 500); // 每 500 毫秒执行一次
  }
    function init(){
   // 执行定时器,每 500 毫秒检查一次是否存在指定的元素
        var intervalId = setInterval(function() {
        var video = document.querySelector('video');
                if (video) {
                    video.addEventListener('loadeddata', startTimer); 
                    video.addEventListener('loadedmetadata', startTimer); // 有备无患
                    video.addEventListener('loadstart',startTimer); // 有备无患
                    clearInterval(intervalId); // 停止定时器
                }
            }
        , 500); // 每 500 毫秒执行一次
    }
    init();
})();