YouTube Capture Frame

Adds button to copy current video frame to the clipboard

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Capture Frame
// @namespace    ycf.smz.k
// @version      1.1
// @description  Adds button to copy current video frame to the clipboard
// @author       Abdelrahman Khalil
// @match        *.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function capture() {
        let video = document.querySelector("video")
        let w = video.videoWidth;
        let h = video.videoHeight;
        let canvas = document.createElement('canvas');
        canvas.width  = w;
        canvas.height = h;
        let ctx = canvas.getContext('2d');
        ctx.drawImage(video, 0, 0, w, h);

        canvas.toBlob(blob => {
            if(navigator.clipboard && navigator.clipboard.write)
                navigator.clipboard.write([new ClipboardItem({'image/png': blob})])
            else console.warn("Copying images is not supported in your browser")
        })
    }

    let button = document.createElement("button")
    button.classList.add("ytp-button")
    button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="25px" height="100%"><path d="M0 0h24v24H0V0z" fill="none"/><circle cx="12" cy="12" r="3"/><path d="M20 4h-3.17l-1.24-1.35c-.37-.41-.91-.65-1.47-.65H9.88c-.56 0-1.1.24-1.48.65L7.17 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm-8 13c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5z"/></svg>'
    button.style.width = "auto"
    button.addEventListener("click", capture)

    if(document.querySelector(".ytp-right-controls"))
       document.querySelector(".ytp-right-controls").insertBefore(button, document.querySelector(".ytp-settings-button"))

    addEventListener('yt-page-data-updated', () => {
        document.querySelector(".ytp-right-controls").insertBefore(button, document.querySelector(".ytp-settings-button"))
    })
})();