YouTube 广告加速跳过 (隐蔽版 v2.1)

模糊选择器 + 随机间隔执行 + 安全倍速加速广告 + 模拟点击避免检测

// ==UserScript==
// @name         YouTube 广告加速跳过 (隐蔽版 v2.1)
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  模糊选择器 + 随机间隔执行 + 安全倍速加速广告 + 模拟点击避免检测
// @match        *://www.youtube.com/*
// @match        *://www.youtube.com/embed/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
  'use strict';

  // 模拟用户点击
  function simulateClick(el) {
    if (!el) return;
    el.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
    el.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
    el.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
    el.dispatchEvent(new MouseEvent("click", { bubbles: true }));
    console.log("⏭️ 模拟点击跳过按钮");
  }

  // 模糊选择器寻找“跳过广告”按钮
  function findAdSkipButton() {
    return [...document.querySelectorAll("button, .ytp-ad-skip-button, .ytp-ad-skip-button-modern")]
      .find(btn => btn && btn.innerText && (btn.innerText.includes("跳过") || btn.innerText.toLowerCase().includes("skip")));
  }

  // 点击跳过广告
  function clickSkip() {
    const btn = findAdSkipButton();
    if (btn) {
      simulateClick(btn);
    }
  }

  // 加速广告逻辑
  function accelerateAd() {
    const video = document.querySelector("video");
    if (!video) return;

    const isAd = document.querySelector(".ad-showing, .ytp-ad-player-overlay");
    const shortAd = video.duration > 0 && video.duration < 40; // 广告一般较短

    if (isAd || shortAd) {
      if (video.playbackRate < 4 || video.playbackRate > 6) {
        video.playbackRate = 4 + Math.random() * 2; // 4~6x 随机加速
      }
      video.muted = true;
      console.log("🎬 广告中:倍速加速 + 静音");
    } else {
      if (video.playbackRate !== 1) video.playbackRate = 1;
      if (video.muted) video.muted = false;
    }
  }

  // 随机间隔执行
  function randomDelay(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  function loopTask() {
    try {
      clickSkip();
      accelerateAd();
    } catch (e) {
      console.warn("⚠️ 脚本运行异常:", e);
    }
    setTimeout(loopTask, randomDelay(800, 1600)); // 0.8s~1.6s 随机执行
  }

  // 启动
  setTimeout(loopTask, 2000); // 延迟启动,避免刚进入页面就触发

})();