// ==UserScript==
// @name HTML5视频截图器
// @namespace indefined
// @supportURL https://github.com/indefined/UserScripts/issues
// @version 0.2
// @description 基于HTML5的原生javascript视频截图器
// @author indefined
// @include *://*
// @run-at document-idle
// @grant GM_registerMenuCommand
// @license MIT
// @downloadURL none
// ==/UserScript==
function init(){
'use strict';
if (document.querySelector('#HTML5VideoCapture')) return;
function shot(){
if (!video) return;
const canvas = document.createElement("canvas");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.style = "max-width:100%";
canvas.getContext('2d')
.drawImage(video, 0, 0, canvas.width, canvas.height);
try{
if (captureDown!=this) throw `i don't want to do it.`;
const a = document.createElement('a');
a.href = canvas.toDataURL('image/jpeg', 0.95);
a.download = 'capture.jpg';
document.head.appendChild(a);
a.click();
a.remove();
}catch(e){
const imgWin = open("",'_blank');
imgWin.document.body.appendChild(canvas);
}
}
function videoPlay(){
if (!video) return;
this.value = video.paused?'暂停':'播放';
video.paused?video.play():video.pause();
}
function videoStep(){
if (!video) return;
if (!video.paused){
video.pause();
play.value='播放';
}
if (preFrame==this) video.currentTime -= 1/30;
else video.currentTime += 1/30;
}
function dialogMove(ev){
if (ev.type=='mousedown'){
panel.t = ev.pageY-panel.offsetTop;
panel.l = ev.pageX-panel.offsetLeft;
panel.addEventListener('mousemove',dialogMove);
}
else if (ev.type=='mouseup'){
panel.removeEventListener('mousemove',dialogMove);
}
else{
panel.style.top = ev.pageY-panel.t+'px';
panel.style.left = ev.pageX-panel.l+'px';
}
}
function detechVideo(){
while(selector.firstChild) selector.removeChild(selector.firstChild);
videos = document.querySelectorAll('video');
if (videos.length){
for (let i=0;itoast.remove(),2000);
}
console.log(videos);
}
let videos,video;
const panel = document.createElement('div');
panel.id = "HTML5VideoCapture";
panel.style = `position: fixed;top: 50px;left: 20px;z-index: 999999;padding: 10px;background: darkcyan;`
panel.innerHTML = `HTML5视频截图器
\
\
\
\
`
const {0:title,1:detech,2:selector,3:play,4:preFrame,5:capture,6:captureDown,7:nextFrame,8:close} = panel.childNodes;
title.onmousedown = dialogMove;
title.onmouseup = dialogMove;
selector.onchange = function(){
video = videos[this.value]||video;
play.value = video.paused?"播放":"暂停";
video.scrollIntoView()
};
detech.onclick = detechVideo;
play.onclick = videoPlay;
preFrame.onclick = videoStep;
nextFrame.onclick = videoStep;
capture.onclick = shot;
captureDown.onclick = shot;
close.onclick = ()=>panel.remove();
document.body.appendChild(panel);
detechVideo();
}
if ('function'==typeof(GM_registerMenuCommand)){
if (top==window) GM_registerMenuCommand('启用HTML5视频截图器',init);
}else init();