Skip to content

Commit 0986559

Browse files
committed
Refactor
1 parent 527fe1f commit 0986559

9 files changed

+62
-163
lines changed

dist/player.es.js

+18-52
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,6 @@ function isInteger(value) {
8080
function isVimeoUrl(url) {
8181
return /^(https?:)?\/\/((player|www)\.)?vimeo\.com(?=$|\/)/.test(url);
8282
}
83-
/**
84-
* Check to see if the URL is for a Vimeo embed.
85-
*
86-
* @param {string} url The url string.
87-
* @return {boolean}
88-
*/
89-
90-
function isVimeoEmbed(url) {
91-
var expr = /^https:\/\/player\.vimeo\.com\/video\/\d+/;
92-
return expr.test(url);
93-
}
9483
/**
9584
* Get the Vimeo URL from an element.
9685
* The element must have either a data-vimeo-id or data-vimeo-url attribute.
@@ -1029,13 +1018,11 @@ function resizeEmbeds() {
10291018
/**
10301019
* Add chapters to existing metadata for Google SEO
10311020
*
1032-
* @param {HTMLElement} [parent=document] The parent element.
1021+
* @param {Object} [player] The player object.
10331022
* @return {void}
10341023
*/
10351024

1036-
function initAppendVideoMetadata() {
1037-
var parent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
1038-
1025+
function initAppendVideoMetadata(player) {
10391026
// Prevent execution if users include the player.js script multiple times.
10401027
if (window.VimeoSeoMetadataAppended) {
10411028
return;
@@ -1054,17 +1041,8 @@ function initAppendVideoMetadata() {
10541041
return;
10551042
}
10561043

1057-
var iframes = parent.querySelectorAll('iframe');
1058-
1059-
for (var i = 0; i < iframes.length; i++) {
1060-
var iframe = iframes[i]; // Initiate appendVideoMetadata if iframe is a Vimeo embed
1061-
1062-
var isValidMessageSource = iframe.contentWindow === event.source;
1063-
1064-
if (isVimeoEmbed(iframe.src) && isValidMessageSource) {
1065-
var player = new Player(iframe);
1066-
player.callMethod('appendVideoMetadata', window.location.href);
1067-
}
1044+
if (player.element.contentWindow === event.source) {
1045+
player.callMethod('appendVideoMetadata', window.location.href);
10681046
}
10691047
};
10701048

@@ -1073,13 +1051,11 @@ function initAppendVideoMetadata() {
10731051
/**
10741052
* Seek to time indicated by vimeo_t query parameter if present in URL
10751053
*
1076-
* @param {HTMLElement} [parent=document] The parent element.
1054+
* @param {Object} [player] The player object.
10771055
* @return {void}
10781056
*/
10791057

1080-
function checkUrlTimeParam() {
1081-
var parent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
1082-
1058+
function checkUrlTimeParam(player) {
10831059
// Prevent execution if users include the player.js script multiple times.
10841060
if (window.VimeoCheckedUrlTimeParam) {
10851061
return;
@@ -1089,7 +1065,7 @@ function checkUrlTimeParam() {
10891065

10901066
var handleError = function handleError(error) {
10911067
if ('console' in window && console.error) {
1092-
console.error("There was an error getting video Id: ".concat(error));
1068+
console.error("There was an error getting video id: ".concat(error));
10931069
}
10941070
};
10951071

@@ -1104,27 +1080,17 @@ function checkUrlTimeParam() {
11041080
return;
11051081
}
11061082

1107-
var iframes = parent.querySelectorAll('iframe');
1083+
if (player.element.contentWindow === event.source) {
1084+
player.getVideoId().then(function (videoId) {
1085+
var matches = new RegExp("[?&]vimeo_t_".concat(videoId, "=([^&#]*)")).exec(window.location.href);
11081086

1109-
for (var i = 0; i < iframes.length; i++) {
1110-
var iframe = iframes[i];
1111-
var isValidMessageSource = iframe.contentWindow === event.source;
1112-
1113-
if (isVimeoEmbed(iframe.src) && isValidMessageSource) {
1114-
(function () {
1115-
var player = new Player(iframe);
1116-
player.getVideoId().then(function (videoId) {
1117-
var matches = new RegExp("[?&]vimeo_t_".concat(videoId, "=([^&#]*)")).exec(window.location.href);
1118-
1119-
if (matches && matches[1]) {
1120-
var sec = decodeURI(matches[1]);
1121-
player.setCurrentTime(sec);
1122-
}
1087+
if (matches && matches[1]) {
1088+
var sec = decodeURI(matches[1]);
1089+
player.setCurrentTime(sec);
1090+
}
11231091

1124-
return;
1125-
}).catch(handleError);
1126-
})();
1127-
}
1092+
return;
1093+
}).catch(handleError);
11281094
}
11291095
};
11301096

@@ -1387,6 +1353,8 @@ var Player = /*#__PURE__*/function () {
13871353
screenfull.on('fullscreenchange', this.fullscreenchangeHandler);
13881354
}
13891355

1356+
initAppendVideoMetadata(this);
1357+
checkUrlTimeParam(this);
13901358
return this;
13911359
}
13921360
/**
@@ -2629,8 +2597,6 @@ if (!isNode) {
26292597
screenfull = initializeScreenfull();
26302598
initializeEmbeds();
26312599
resizeEmbeds();
2632-
initAppendVideoMetadata();
2633-
checkUrlTimeParam();
26342600
}
26352601

26362602
export default Player;

dist/player.js

+18-52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/player.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/player.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/player.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/embed.js

+20-36
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* @module lib/embed
33
*/
44

5-
import Player from '../player';
6-
import { isVimeoUrl, isVimeoEmbed, getVimeoUrl } from './functions';
5+
import { isVimeoUrl, getVimeoUrl } from './functions';
76
import { parseMessageData } from './postmessage';
87

98
const oEmbedParameters = [
@@ -219,10 +218,10 @@ export function resizeEmbeds(parent = document) {
219218
/**
220219
* Add chapters to existing metadata for Google SEO
221220
*
222-
* @param {HTMLElement} [parent=document] The parent element.
221+
* @param {Object} [player] The player object.
223222
* @return {void}
224223
*/
225-
export function initAppendVideoMetadata(parent = document) {
224+
export function initAppendVideoMetadata(player) {
226225
// Prevent execution if users include the player.js script multiple times.
227226
if (window.VimeoSeoMetadataAppended) {
228227
return;
@@ -239,16 +238,8 @@ export function initAppendVideoMetadata(parent = document) {
239238
return;
240239
}
241240

242-
const iframes = parent.querySelectorAll('iframe');
243-
for (let i = 0; i < iframes.length; i++) {
244-
const iframe = iframes[i];
245-
246-
// Initiate appendVideoMetadata if iframe is a Vimeo embed
247-
const isValidMessageSource = iframe.contentWindow === event.source;
248-
if (isVimeoEmbed(iframe.src) && isValidMessageSource) {
249-
const player = new Player(iframe);
250-
player.callMethod('appendVideoMetadata', window.location.href);
251-
}
241+
if (player.element.contentWindow === event.source) {
242+
player.callMethod('appendVideoMetadata', window.location.href);
252243
}
253244
};
254245

@@ -258,10 +249,10 @@ export function initAppendVideoMetadata(parent = document) {
258249
/**
259250
* Seek to time indicated by vimeo_t query parameter if present in URL
260251
*
261-
* @param {HTMLElement} [parent=document] The parent element.
252+
* @param {Object} [player] The player object.
262253
* @return {void}
263254
*/
264-
export function checkUrlTimeParam(parent = document) {
255+
export function checkUrlTimeParam(player) {
265256
// Prevent execution if users include the player.js script multiple times.
266257
if (window.VimeoCheckedUrlTimeParam) {
267258
return;
@@ -270,7 +261,7 @@ export function checkUrlTimeParam(parent = document) {
270261

271262
const handleError = (error) => {
272263
if ('console' in window && console.error) {
273-
console.error(`There was an error getting video Id: ${error}`);
264+
console.error(`There was an error getting video id: ${error}`);
274265
}
275266
};
276267

@@ -284,25 +275,18 @@ export function checkUrlTimeParam(parent = document) {
284275
return;
285276
}
286277

287-
const iframes = parent.querySelectorAll('iframe');
288-
for (let i = 0; i < iframes.length; i++) {
289-
const iframe = iframes[i];
290-
const isValidMessageSource = iframe.contentWindow === event.source;
291-
292-
if (isVimeoEmbed(iframe.src) && isValidMessageSource) {
293-
const player = new Player(iframe);
294-
player
295-
.getVideoId()
296-
.then((videoId) => {
297-
const matches = new RegExp(`[?&]vimeo_t_${videoId}=([^&#]*)`).exec(window.location.href);
298-
if (matches && matches[1]) {
299-
const sec = decodeURI(matches[1]);
300-
player.setCurrentTime(sec);
301-
}
302-
return;
303-
})
304-
.catch(handleError);
305-
}
278+
if (player.element.contentWindow === event.source) {
279+
player
280+
.getVideoId()
281+
.then((videoId) => {
282+
const matches = new RegExp(`[?&]vimeo_t_${videoId}=([^&#]*)`).exec(window.location.href);
283+
if (matches && matches[1]) {
284+
const sec = decodeURI(matches[1]);
285+
player.setCurrentTime(sec);
286+
}
287+
return;
288+
})
289+
.catch(handleError);
306290
}
307291
};
308292

src/lib/functions.js

-11
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@ export function isVimeoUrl(url) {
6161
return (/^(https?:)?\/\/((player|www)\.)?vimeo\.com(?=$|\/)/).test(url);
6262
}
6363

64-
/**
65-
* Check to see if the URL is for a Vimeo embed.
66-
*
67-
* @param {string} url The url string.
68-
* @return {boolean}
69-
*/
70-
export function isVimeoEmbed(url) {
71-
const expr = /^https:\/\/player\.vimeo\.com\/video\/\d+/;
72-
return expr.test(url);
73-
}
74-
7564
/**
7665
* Get the Vimeo URL from an element.
7766
* The element must have either a data-vimeo-id or data-vimeo-url attribute.

0 commit comments

Comments
 (0)