YouTube Screenshot

Take screenshots of YouTube videos with P key

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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);
})();