-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyoutube-screenshot.js
29 lines (23 loc) · 1.05 KB
/
youtube-screenshot.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
'use strict';
(function(window, document) {
var canvas = document.createElement('canvas');
var video = document.querySelector('video');
var ctx = canvas.getContext('2d');
// Change the size here
canvas.width = parseInt(video.offsetWidth);
canvas.height = parseInt(video.offsetHeight);
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// Won't work on file:/// URLs. SecurityError: Tainted canvases may not be exported.
var base64ImageData = canvas.toDataURL('image/jpeg');
var filename = 'snap-' + canvas.width + 'x' + canvas.height + '-' + video.currentTime + '.jpg';
// Wrap <img> in link to download image because
// the context menu Save Image as... is blocked for security reasons
var a = document.createElement('a');
a.download = filename;
a.href = base64ImageData;
var img = document.createElement('img');
img.src = base64ImageData;
img.alt = filename;
img.title = 'Click to save ' + filename;
window.open().document.body.appendChild(a).appendChild(img);
})(window, document);