YouTube Ad Skipper

Simple ad skip button for YouTube with enhanced SPA support

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Ad Skipper
// @version      1.6
// @description  Simple ad skip button for YouTube with enhanced SPA support
// @match        *://*.youtube.com/*
// @grant        GM_addStyle
// @run-at       document-end
// @license      MIT
// @author       anassk
// @namespace https://greasyfork.org/users/1422975
// ==/UserScript==
//



(function() {
    'use strict';

    let currentVideoId = null;
    let storedTime = 0;
    let initAttempts = 0;
    const MAX_ATTEMPTS = 20;

    const createSkipButton = () => {
        const skipButton = document.createElement('button');
        skipButton.innerHTML = '>';
        skipButton.classList.add('skip-button');
        skipButton.style.cssText = `
            position: absolute;
            bottom: 70px;
            right: 5px;
            z-index: 9999;
            background: rgba(0, 0, 0, 0.7);
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        `;
        return skipButton;
    };

    const reloadVideo = () => {
        const player = document.querySelector('video');
        const videoId = new URLSearchParams(window.location.search).get('v');
        if (player && videoId) {
            storedTime = player.currentTime;
            const moviePlayer = document.getElementById('movie_player');
            if (moviePlayer) {
                moviePlayer.loadVideoById(videoId);
                const setStoredTime = () => {
                    moviePlayer.seekTo(storedTime, true);
                    player.currentTime = storedTime;
                };

                const checkAndSetTime = setInterval(() => {
                    if (player.readyState >= 3) {
                        setStoredTime();
                        clearInterval(checkAndSetTime);
                    }
                }, 50);
            }
        }
    };

    const waitForElement = (selector) => {
        return new Promise(resolve => {
            if (document.querySelector(selector)) {
                return resolve(document.querySelector(selector));
            }

            const observer = new MutationObserver(mutations => {
                if (document.querySelector(selector)) {
                    observer.disconnect();
                    resolve(document.querySelector(selector));
                }
            });

            observer.observe(document.documentElement, {
                childList: true,
                subtree: true
            });
        });
    };

    const initializeButton = async () => {
        if (initAttempts >= MAX_ATTEMPTS) {
            initAttempts = 0;
            return;
        }

        const videoId = new URLSearchParams(window.location.search).get('v');
        if (!videoId || videoId === currentVideoId) {
            return;
        }

        // Wait for both video and player container
        try {
            const player = await waitForElement('video');
            const playerContainer = await waitForElement('.html5-video-player');

            if (player && playerContainer) {
                // Remove any existing skip buttons
                document.querySelectorAll('.skip-button').forEach(btn => btn.remove());

                const skipButton = createSkipButton();
                skipButton.addEventListener('click', reloadVideo);
                playerContainer.appendChild(skipButton);
                currentVideoId = videoId;
                initAttempts = 0;
            }
        } catch (error) {
            initAttempts++;
            setTimeout(initializeButton, 250);
        }
    };

    // Style for hover effect
    GM_addStyle(`
        .skip-button:hover {
            background: rgba(0, 0, 0, 0.9) !important;
        }
    `);

    // Multiple initialization triggers
    const initializeTriggers = () => {
        initializeButton();

        // Watch for URL changes
        let lastUrl = location.href;
        new MutationObserver(() => {
            const url = location.href;
            if (url !== lastUrl) {
                lastUrl = url;
                currentVideoId = null;
                setTimeout(initializeButton, 100);
            }
        }).observe(document, { subtree: true, childList: true });

        // YouTube specific events
        window.addEventListener('yt-navigate-finish', () => {
            currentVideoId = null;
            setTimeout(initializeButton, 100);
        });

        // Regular page events
        window.addEventListener('load', initializeButton);
        document.addEventListener('DOMContentLoaded', initializeButton);
    };

    // Start initialization
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initializeTriggers);
    } else {
        initializeTriggers();
    }
})();