-
Notifications
You must be signed in to change notification settings - Fork 263
/
embed.js
executable file
·328 lines (282 loc) · 9.25 KB
/
embed.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* @module lib/embed
*/
import Player from '../player';
import { isVimeoUrl, isVimeoEmbed, getVimeoUrl, getOembedDomain } from './functions';
import { parseMessageData } from './postmessage';
const oEmbedParameters = [
'airplay',
'audio_tracks',
'autopause',
'autoplay',
'background',
'byline',
'cc',
'chapter_id',
'chapters',
'chromecast',
'color',
'colors',
'controls',
'dnt',
'end_time',
'fullscreen',
'height',
'id',
'interactive_params',
'keyboard',
'loop',
'maxheight',
'maxwidth',
'muted',
'play_button_position',
'playsinline',
'portrait',
'progress_bar',
'quality_selector',
'responsive',
'speed',
'start_time',
'texttrack',
'title',
'transcript',
'transparent',
'url',
'vimeo_logo',
'volume',
'watch_full_video',
'width'
];
/**
* Get the 'data-vimeo'-prefixed attributes from an element as an object.
*
* @param {HTMLElement} element The element.
* @param {Object} [defaults={}] The default values to use.
* @return {Object<string, string>}
*/
export function getOEmbedParameters(element, defaults = {}) {
return oEmbedParameters.reduce((params, param) => {
const value = element.getAttribute(`data-vimeo-${param}`);
if (value || value === '') {
params[param] = value === '' ? 1 : value;
}
return params;
}, defaults);
}
/**
* Create an embed from oEmbed data inside an element.
*
* @param {object} data The oEmbed data.
* @param {HTMLElement} element The element to put the iframe in.
* @return {HTMLIFrameElement} The iframe embed.
*/
export function createEmbed({ html }, element) {
if (!element) {
throw new TypeError('An element must be provided');
}
if (element.getAttribute('data-vimeo-initialized') !== null) {
return element.querySelector('iframe');
}
const div = document.createElement('div');
div.innerHTML = html;
element.appendChild(div.firstChild);
element.setAttribute('data-vimeo-initialized', 'true');
return element.querySelector('iframe');
}
/**
* Make an oEmbed call for the specified URL.
*
* @param {string} videoUrl The vimeo.com url for the video.
* @param {Object} [params] Parameters to pass to oEmbed.
* @param {HTMLElement} element The element.
* @return {Promise}
*/
export function getOEmbedData(videoUrl, params = {}, element) {
return new Promise((resolve, reject) => {
if (!isVimeoUrl(videoUrl)) {
throw new TypeError(`“${videoUrl}” is not a vimeo.com url.`);
}
const domain = getOembedDomain(videoUrl);
let url = `https://${domain}/api/oembed.json?url=${encodeURIComponent(videoUrl)}`;
for (const param in params) {
if (params.hasOwnProperty(param)) {
url += `&${param}=${encodeURIComponent(params[param])}`;
}
}
const xhr = 'XDomainRequest' in window ? new XDomainRequest() : new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function() {
if (xhr.status === 404) {
reject(new Error(`“${videoUrl}” was not found.`));
return;
}
if (xhr.status === 403) {
reject(new Error(`“${videoUrl}” is not embeddable.`));
return;
}
try {
const json = JSON.parse(xhr.responseText);
// Check api response for 403 on oembed
if (json.domain_status_code === 403) {
// We still want to create the embed to give users visual feedback
createEmbed(json, element);
reject(new Error(`“${videoUrl}” is not embeddable.`));
return;
}
resolve(json);
}
catch (error) {
reject(error);
}
};
xhr.onerror = function() {
const status = xhr.status ? ` (${xhr.status})` : '';
reject(new Error(`There was an error fetching the embed code from Vimeo${status}.`));
};
xhr.send();
});
}
/**
* Initialize all embeds within a specific element
*
* @param {HTMLElement} [parent=document] The parent element.
* @return {void}
*/
export function initializeEmbeds(parent = document) {
const elements = [].slice.call(parent.querySelectorAll('[data-vimeo-id], [data-vimeo-url]'));
const handleError = (error) => {
if ('console' in window && console.error) {
console.error(`There was an error creating an embed: ${error}`);
}
};
elements.forEach((element) => {
try {
// Skip any that have data-vimeo-defer
if (element.getAttribute('data-vimeo-defer') !== null) {
return;
}
const params = getOEmbedParameters(element);
const url = getVimeoUrl(params);
getOEmbedData(url, params, element).then((data) => {
return createEmbed(data, element);
}).catch(handleError);
}
catch (error) {
handleError(error);
}
});
}
/**
* Resize embeds when messaged by the player.
*
* @param {HTMLElement} [parent=document] The parent element.
* @return {void}
*/
export function resizeEmbeds(parent = document) {
// Prevent execution if users include the player.js script multiple times.
if (window.VimeoPlayerResizeEmbeds_) {
return;
}
window.VimeoPlayerResizeEmbeds_ = true;
const onMessage = (event) => {
if (!isVimeoUrl(event.origin)) {
return;
}
// 'spacechange' is fired only on embeds with cards
if (!event.data || event.data.event !== 'spacechange') {
return;
}
const iframes = parent.querySelectorAll('iframe');
for (let i = 0; i < iframes.length; i++) {
if (iframes[i].contentWindow !== event.source) {
continue;
}
// Change padding-bottom of the enclosing div to accommodate
// card carousel without distorting aspect ratio
const space = iframes[i].parentElement;
space.style.paddingBottom = `${event.data.data[0].bottom}px`;
break;
}
};
window.addEventListener('message', onMessage);
}
/**
* Add chapters to existing metadata for Google SEO
*
* @param {HTMLElement} [parent=document] The parent element.
* @return {void}
*/
export function initAppendVideoMetadata(parent = document) {
// Prevent execution if users include the player.js script multiple times.
if (window.VimeoSeoMetadataAppended) {
return;
}
window.VimeoSeoMetadataAppended = true;
const onMessage = (event) => {
if (!isVimeoUrl(event.origin)) {
return;
}
const data = parseMessageData(event.data);
if (!data || data.event !== 'ready') {
return;
}
const iframes = parent.querySelectorAll('iframe');
for (let i = 0; i < iframes.length; i++) {
const iframe = iframes[i];
// Initiate appendVideoMetadata if iframe is a Vimeo embed
const isValidMessageSource = iframe.contentWindow === event.source;
if (isVimeoEmbed(iframe.src) && isValidMessageSource) {
const player = new Player(iframe);
player.callMethod('appendVideoMetadata', window.location.href);
}
}
};
window.addEventListener('message', onMessage);
}
/**
* Seek to time indicated by vimeo_t query parameter if present in URL
*
* @param {HTMLElement} [parent=document] The parent element.
* @return {void}
*/
export function checkUrlTimeParam(parent = document) {
// Prevent execution if users include the player.js script multiple times.
if (window.VimeoCheckedUrlTimeParam) {
return;
}
window.VimeoCheckedUrlTimeParam = true;
const handleError = (error) => {
if ('console' in window && console.error) {
console.error(`There was an error getting video Id: ${error}`);
}
};
const onMessage = (event) => {
if (!isVimeoUrl(event.origin)) {
return;
}
const data = parseMessageData(event.data);
if (!data || data.event !== 'ready') {
return;
}
const iframes = parent.querySelectorAll('iframe');
for (let i = 0; i < iframes.length; i++) {
const iframe = iframes[i];
const isValidMessageSource = iframe.contentWindow === event.source;
if (isVimeoEmbed(iframe.src) && isValidMessageSource) {
const player = new Player(iframe);
player
.getVideoId()
.then((videoId) => {
const matches = new RegExp(`[?&]vimeo_t_${videoId}=([^&#]*)`).exec(window.location.href);
if (matches && matches[1]) {
const sec = decodeURI(matches[1]);
player.setCurrentTime(sec);
}
return;
})
.catch(handleError);
}
}
};
window.addEventListener('message', onMessage);
}