Greasy Fork

来自缓存

Greasy Fork is available in English.

YouTube广告加速跳过增强版

只在广告时加速播放,视频内容正常播放,不影响视频显示。

当前为 2025-07-02 提交的版本,查看 最新版本

// ==UserScript==
// @name         YouTube广告加速跳过增强版
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  只在广告时加速播放,视频内容正常播放,不影响视频显示。
// @author       little fool
// @match        *://www.youtube.com/*
// @match        *://m.youtube.com/*
// @match        *://music.youtube.com/*
// @match        *://www.youtube-nocookie.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function () {
    'use strict';

    const SHOW_LOG = false;

    // 创建提示UI
    const createNotice = () => {
        const notice = document.createElement('div');
        notice.id = 'ad-skip-notice';
        notice.textContent = '⏩ 正在跳过广告...';
        notice.style = `
            position: fixed;
            bottom: 30px;
            right: 30px;
            background: rgba(0, 0, 0, 0.85);
            color: #fff;
            padding: 10px 20px;
            font-size: 14px;
            border-radius: 6px;
            z-index: 999999;
            display: none;
            border-left: 5px solid #e74c3c;
            font-family: sans-serif;
        `;
        document.body.appendChild(notice);
        return notice;
    };

    let noticeEl;
    const showNotice = () => noticeEl && (noticeEl.style.display = 'block');
    const hideNotice = () => noticeEl && (noticeEl.style.display = 'none');

    const isAdPlaying = () => {
        return (
            document.querySelector('.ad-showing') ||
            document.querySelector('.ytp-ad-player-overlay') ||
            document.querySelector('.ytp-ad-image') ||
            document.querySelector('.ytp-ad-module') ||
            document.querySelector('div[class*="ad-interrupting"]')
        );
    };

    const trySkip = () => {
        const skipBtn = document.querySelector('.ytp-ad-skip-button, .ytp-ad-skip-button-modern');
        if (skipBtn) {
            skipBtn.click();
            SHOW_LOG && console.log('⏩ 点击跳过按钮');
        }
    };

    const handleAds = () => {
        const video = document.querySelector('video');
        if (!video) return;

        if (isAdPlaying()) {
            // 广告播放时加速,不影响画面
            showNotice();
            video.playbackRate = 16;  // 设置广告加速
            video.muted = true;       // 静音广告
            trySkip();                // 尝试跳过广告
        } else {
            // 恢复视频播放,确保不干扰视频画面
            hideNotice();
            video.playbackRate = 1;   // 恢复正常播放速度
            video.muted = false;      // 恢复音量
        }
    };

    // 启动逻辑(等DOM加载完成)
    window.addEventListener('DOMContentLoaded', () => {
        noticeEl = createNotice();

        setInterval(() => {
            handleAds();
        }, 500); // 更频繁地检测广告,提高跳过率

        SHOW_LOG && console.log('✅ YouTube广告加速跳过脚本已运行');
    });
})();