Greasy Fork is available in English.

YouTube Screenshot

Take screenshots of YouTube videos with P key

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         YouTube Screenshot
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Take screenshots of YouTube videos with P key
// @author       rushplayer
// @match        https://www.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function getVideoElement() {
        return document.querySelector('video');
    }

    function captureFrame(video) {
        const canvas = document.createElement('canvas');
        canvas.width = video.videoWidth;
        canvas.height = video.videoHeight;
        const ctx = canvas.getContext('2d');
        
        // 设置跨域属性
        video.crossOrigin = 'anonymous';
        
        try {
            ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
            // 检查是否截图成功
            const imageData = ctx.getImageData(0, 0, 1, 1).data;
            if (imageData[0] === 0 && imageData[1] === 0 && imageData[2] === 0) {
                throw new Error('Black frame detected');
            }
            return canvas;
        } catch (e) {
            console.error('截图失败:', e);
            alert('截图失败: 请确保视频已加载并尝试再次按P键');
            return null;
        }
    }

    function downloadImage(canvas) {
        const now = new Date();
        const filename = `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}${String(now.getDate()).padStart(2,'0')}.jpg`;
        
        const link = document.createElement('a');
        link.download = filename;
        link.href = canvas.toDataURL('image/jpeg', 0.8);
        link.click();
    }

    function handleKeyPress(e) {
        if (e.key.toLowerCase() === 'p') {
            const video = getVideoElement();
            if (video) {
                const canvas = captureFrame(video);
                if (canvas) downloadImage(canvas);
            }
        }
    }

    document.addEventListener('keydown', handleKeyPress);
})();