forked from deactivated/video-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.js
42 lines (34 loc) · 921 Bytes
/
snapshot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function snapshot() {
var video = document.getElementsByTagName("video")[0];
if (!video) {
console.log("No video found");
return;
}
var isPlaying = (video.currentTime > 0) && !video.paused && !video.ended && (video.readyState > 2);
if (isPlaying) {
video.pause();
}
try {
var cvs = document.createElement("canvas");
cvs.setAttribute("width", video.videoWidth);
cvs.setAttribute("height", video.videoHeight);
var ctx = cvs.getContext('2d');
ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
var dataUrl = cvs.toDataURL();
var loadInBackground = 1;
if (loadInBackground) {
chrome.runtime.sendMessage(
null, {dataUrl: dataUrl}, null,
function(response) {
console.log('Screenshot save: ' + response);
});
} else {
window.open(dataUrl, '_blank');
}
} finally {
if (isPlaying) {
video.play();
}
}
}
snapshot();