YouTube Ad Skipper

Simple ad skip button for YouTube

目前為 2025-01-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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.5
// @description  Simple ad skip button for YouTube
// @match        *://*.youtube.com/*
// @grant        GM_addStyle
// @run-at       document-end
// @license      MIT
// @author       anassk
// @namespace https://greasyfork.org/users/1422975
// ==/UserScript==
// 
(function() {
    'use strict';
    
    const skipButton = document.createElement('button');
    skipButton.innerHTML = '>';
    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;
    `;

    let storedTime = 0;

    const reloadVideo = () => {
        const player = document.querySelector('video');
        const videoId = new URLSearchParams(window.location.search).get('v');
        
        if (player && videoId) {
            // Step 1: Store the exact time
            storedTime = player.currentTime;
            
            // Step 2: Reload video
            const moviePlayer = document.getElementById('movie_player');
            if (moviePlayer) {
                moviePlayer.loadVideoById(videoId);
                
                // Step 3: Set exact time after a brief moment
                const setStoredTime = () => {
                    moviePlayer.seekTo(storedTime, true);
                    player.currentTime = storedTime;
                };

                // Wait for player to be ready
                const checkAndSetTime = setInterval(() => {
                    if (player.readyState >= 3) { // HAVE_FUTURE_DATA or better
                        setStoredTime();
                        clearInterval(checkAndSetTime);
                    }
                }, 50);
            }
        }
    };

    skipButton.addEventListener('click', reloadVideo);

    const initializeButton = () => {
        const player = document.querySelector('video');
        if (player) {
            const playerContainer = player.closest('.html5-video-player');
            if (playerContainer && !playerContainer.querySelector('.skip-button')) {
                skipButton.classList.add('skip-button');
                playerContainer.appendChild(skipButton);
            }
        } else {
            setTimeout(initializeButton, 1000);
        }
    };

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

    initializeButton();
})();