YouTube 广告加速跳过

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

// ==UserScript==
// @name         YouTube 广告加速跳过 
// @namespace    http://tampermonkey.net/
// @version      2.3
// @description  模糊选择器 + 随机间隔执行 + 安全倍速加速广告 + 模拟点击避免检测 + 用户可选分辨率
// @match        *://www.youtube.com/*
// @match        *://www.youtube.com/embed/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
  "use strict";

  let lastState = "normal";
  let video = null;

  // 模拟用户点击
  function simulateClick(el) {
    if (!el) return;
    ["mouseover", "mousedown", "mouseup", "click"].forEach(type =>
      el.dispatchEvent(new MouseEvent(type, { bubbles: true }))
    );
  }

  // 模糊选择器寻找“跳过广告”按钮
  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);
      console.log("⏭️ 已跳过广告");
    }
  }

  // 广告检测
  function isAdPlaying() {
    if (!video) return false;
    return (
      document.querySelector(".ad-showing, .ytp-ad-player-overlay, .ytp-ad-text") ||
      (video.duration > 0 && video.duration < 40) // 一般广告较短
    );
  }

  // 加速广告逻辑
  function accelerateAd() {
    if (!video) return;

    if (isAdPlaying()) {
      if (video.playbackRate < 4 || video.playbackRate > 6) {
        video.playbackRate = 4 + Math.random() * 2; // 4~6x 随机加速
      }
      if (!video.muted) video.muted = true;

      if (lastState !== "ad") {
        console.log("🎬 广告中:倍速加速 + 静音");
        lastState = "ad";
      }
    } else {
      if (video.playbackRate !== 1) video.playbackRate = 1;
      if (video.muted) video.muted = false;

      if (lastState !== "normal") {
        console.log("✅ 已恢复正常播放");
        lastState = "normal";
      }
    }
  }

  // 用户自主选择分辨率
  function setQuality(label) {
    const menuButton = document.querySelector(".ytp-settings-button");
    if (!menuButton) return;

    simulateClick(menuButton);
    setTimeout(() => {
      const qualityMenu = [...document.querySelectorAll(".ytp-menuitem-label")];
      if (!qualityMenu.length) return;

      let target = qualityMenu.find(el => el.innerText.includes(label));
      if (target) {
        simulateClick(target);
        console.log(`⚙️ 分辨率已切换到: ${target.innerText.trim()}`);
      }
      // 关闭菜单
      simulateClick(menuButton);
    }, 500);
  }

  // 快捷键支持:Shift+8 切 8K,Shift+1 切 1080P
  document.addEventListener("keydown", e => {
    if (e.shiftKey && e.key === "8") {
      setQuality("4320"); // 8K
    } else if (e.shiftKey && e.key === "1") {
      setQuality("1080"); // 1080P
    }
  });

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

  function loopTask() {
    try {
      video = document.querySelector("video");
      if (video) {
        clickSkip();
        accelerateAd();
      }
    } catch (e) {
      console.warn("⚠️ 脚本运行异常:", e);
    }
    setTimeout(loopTask, randomDelay(800, 1600));
  }

  // 启动
  setTimeout(() => {
    video = document.querySelector("video");
    loopTask();
    console.log("🚀 YouTube 广告加速跳过脚本已启动 (支持自主分辨率切换)");
  }, 2000);
})();