Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (159 loc) · 6.3 KB
/
Copy pathscript.js
File metadata and controls
181 lines (159 loc) · 6.3 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var section = document.getElementById('main');
var container = document.getElementById('result');
var videoURL = document.getElementById('video-url');
var getURL = document.getElementById('submit');
var focusInput = document.getElementById('focus-input');
focusInput.addEventListener('click', setFocus);
function setFocus (e) {
videoURL.focus();
e.preventDefault();
}
function parseURL (url) {
// - Supported YouTube URL formats:
// - http://www.youtube.com/watch?v=My2FRPA3Gf8
// - http://youtu.be/My2FRPA3Gf8
// - https://youtube.googleapis.com/v/My2FRPA3Gf8
// - https://m.youtube.com/watch?v=My2FRPA3Gf8
// - Supported Vimeo URL formats:
// - http://vimeo.com/25451551
// - http://player.vimeo.com/video/25451551
// - Also supports relative URLs:
// - //player.vimeo.com/video/25451551
var match = url.match(/(?:https?:)?\/\/(?:player\.|www\.|m\.)?(vimeo\.com|youtu(?:be\.com|\.be|be\.googleapis\.com))\/(?:video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]+)/);
var type;
var id;
if (match) {
var host = match[1];
id = match[2];
if (host.indexOf('youtu') > -1) {
type = 'youtube';
} else if (host.indexOf('vimeo') > -1) {
type = 'vimeo';
}
}
return {
type: type,
id: id
};
}
var requestCounter = 0;
var appendThumb = (function(e) {
var videoDetails = parseURL(videoURL.value);
var videoType = videoDetails.type;
var videoID = videoDetails.id;
// Clear previous results
container.innerHTML = "";
// Bail out early on unsupported URLs instead of rendering broken elements
if (!videoType) {
var errorMsg = document.createElement('p');
errorMsg.setAttribute('class', 'error');
errorMsg.textContent = "Sorry, this URL is not supported. Please paste a YouTube or Vimeo video link.";
container.appendChild(errorMsg);
section.appendChild(container);
e.preventDefault();
return;
}
var requestId = ++requestCounter;
// Build the result elements up front, so the async Vimeo callback below can
// fill them in without racing the DOM construction
var headline = document.createElement('h2');
headline.textContent = "Here you go:";
var thumbIMG = document.createElement('img');
thumbIMG.alt = "Thumbnail of Video";
var thumbLINK = document.createElement('a');
thumbLINK.title = "Download Thumbnail";
thumbLINK.textContent = "Download Thumbnail";
thumbLINK.setAttribute('class', 'download btn');
thumbLINK.setAttribute('target', '_blank');
thumbLINK.setAttribute('download', 'thumbnail');
var paragraph = document.createElement('p');
paragraph.setAttribute('class', 'additional');
// Build the link via DOM APIs instead of innerHTML, so a crafted URL
// in the input cannot inject markup (XSS)
var videoLink = document.createElement('a');
videoLink.href = videoURL.value;
videoLink.title = 'Link to video';
videoLink.target = '_blank';
videoLink.textContent = 'this video';
paragraph.appendChild(document.createTextNode('The Thumbnail is associated to '));
paragraph.appendChild(videoLink);
paragraph.appendChild(document.createTextNode('. Please use it according to the copyrights.'));
// Set image and download link together so they never get out of sync
function setThumb(src) {
thumbIMG.src = src;
thumbLINK.href = src;
}
function showError(message) {
var errorMsg = document.createElement('p');
errorMsg.setAttribute('class', 'error');
errorMsg.textContent = message;
container.appendChild(errorMsg);
}
if (videoType == 'youtube') {
// Quality levels in descending order; not every video has maxresdefault/sddefault
var ytQualities = ['maxresdefault', 'sddefault', 'hqdefault', 'mqdefault', 'default'];
var ytQualityIndex = 0;
setThumb('https://img.youtube.com/vi/' + videoID + '/' + ytQualities[0] + '.jpg');
// If maxresdefault/sddefault is missing, YouTube still returns HTTP 200 but
// serves a 120x90 placeholder. So only after loading, check the natural
// width and fall back to the next quality level if needed.
thumbIMG.onload = function () {
if (thumbIMG.naturalWidth <= 120 && ytQualityIndex < ytQualities.length - 1) {
ytQualityIndex++;
setThumb('https://img.youtube.com/vi/' + videoID + '/' + ytQualities[ytQualityIndex] + '.jpg');
}
};
thumbIMG.onerror = function () {
if (ytQualityIndex < ytQualities.length - 1) {
ytQualityIndex++;
setThumb('https://img.youtube.com/vi/' + videoID + '/' + ytQualities[ytQualityIndex] + '.jpg');
}
};
}
else if (videoType == 'vimeo') {
var xhr = new XMLHttpRequest();
// oEmbed endpoint: no access token needed, CORS-enabled, returns thumbnail_url
// already scaled to the requested width (capped by the video's own resolution)
var oembedURL = "https://vimeo.com/api/oembed.json?url=" +
encodeURIComponent("https://vimeo.com/" + videoID) + "&width=1280";
xhr.open("GET", oembedURL, true);
xhr.onload = function () {
// Ignore stale responses if a newer URL was submitted meanwhile
if (requestId !== requestCounter) return;
if (xhr.readyState === 4) {
if (xhr.status === 200) {
try {
var parsedData = JSON.parse(xhr.responseText);
if (parsedData.thumbnail_url) {
setThumb(parsedData.thumbnail_url);
} else {
showError("No thumbnail found for this Vimeo video.");
}
} catch (err) {
showError("Could not parse the Vimeo response.");
}
} else {
showError("Could not fetch the Vimeo thumbnail (HTTP " + xhr.status + ").");
}
}
};
xhr.onerror = function () {
if (requestId !== requestCounter) return;
showError("Could not fetch the Vimeo thumbnail. Please try again.");
};
xhr.send(null);
}
// Append headline to the container
container.appendChild(headline);
// Append image to the container
container.appendChild(thumbIMG);
// Append download-link
container.appendChild(thumbLINK);
// Append paragraph to the container
container.appendChild(paragraph);
// Append container to the section
section.appendChild(container);
// prevent the form to submit and reload page
e.preventDefault();
});
getURL.addEventListener('click', appendThumb);