YouTube 广告加速器 v1.7 优化多广告检测版

强制检测所有广告类型,自动加速16x并静音,模拟点击跳过,支持短片广告与不可跳过广告,作者:little fool

// ==UserScript==
// @name         YouTube 广告加速器 v1.7 优化多广告检测版
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  强制检测所有广告类型,自动加速16x并静音,模拟点击跳过,支持短片广告与不可跳过广告,作者:little fool
// @author       little fool
// @match        *://www.youtube.com/*
// @match        *://m.youtube.com/*
// @match        *://music.youtube.com/*
// @match        *://www.youtube-nocookie.com/*
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-end
// ==/UserScript==

(function () {
  'use strict';

  const log = (...args) => console.log('[YT广告增强 v1.7]', ...args);
  const getVideo = () => document.querySelector('video');

  let adStartTime = null;
  let adTimeTotal = GM_getValue('yt_ad_time', 0);

  // 主检测函数:判断是否广告
  function isAdPlaying() {
    const player = document.getElementById('movie_player');
    const video = getVideo();
    if (!video) return false;

    // 方法 1: YouTube 标记
    if (player && player.classList.contains('ad-showing')) return true;

    // 方法 2: 广告容器是否可见
    const adOverlay = document.querySelector('.ytp-ad-player-overlay, .ytp-ad-overlay-container, .ytp-ad-text-overlay');
    if (adOverlay && adOverlay.offsetParent !== null) return true;

    // 方法 3: 短广告(小于60s,且 player 没有进度条)
    if (video.duration > 0 && video.duration <= 60 && !document.querySelector('.ytp-progress-bar')) {
      return true;
    }

    return false;
  }

  function forceClickSkip() {
    const btn = document.querySelector('.ytp-ad-skip-button');
    if (btn && btn.offsetParent !== null) {
      btn.click();
      log('⏭️ 跳过广告按钮已点击');
    }
  }

  function handleAdPlayback() {
    const video = getVideo();
    if (!video) return;

    if (isAdPlaying()) {
      if (!adStartTime) adStartTime = Date.now();

      // 广告加速 + 静音
      if (video.playbackRate < 15) video.playbackRate = 16;
      if (!video.muted) video.muted = true;

      // 模拟点击跳过按钮
      forceClickSkip();

      log('🎬 广告中:加速16x + 静音(多模式检测)');

    } else {
      if (adStartTime) {
        const elapsed = Math.floor((Date.now() - adStartTime) / 1000);
        adTimeTotal += elapsed;
        GM_setValue('yt_ad_time', adTimeTotal);
        log(`✅ 广告结束,跳过 ${elapsed}s,总计 ${adTimeTotal}s`);
        adStartTime = null;
      }

      // 恢复正常
      if (video.playbackRate !== 1) video.playbackRate = 1;
      if (video.muted) video.muted = false;
    }
  }

  function loop() {
    handleAdPlayback();
  }

  function init() {
    setInterval(loop, 300); // 0.3 秒检测一次
    log('🚀 v1.7 多广告优化版已启动');
  }

  init();
})();