Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable time query param in embed URL #858

Merged
merged 4 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions dist/player.es.js
Expand Up @@ -1070,6 +1070,66 @@ function initAppendVideoMetadata() {

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}
*/

function checkUrlTimeParam() {
var parent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;

// Prevent execution if users include the player.js script multiple times.
if (window.VimeoCheckedUrlTimeParam) {
return;
}

window.VimeoCheckedUrlTimeParam = true;

var handleError = function handleError(error) {
if ('console' in window && console.error) {
console.error("There was an error getting video Id: ".concat(error));
}
};

var onMessage = function onMessage(event) {
if (!isVimeoUrl(event.origin)) {
return;
}

var data = parseMessageData(event.data);

if (!data || data.event !== 'ready') {
return;
}

var iframes = parent.querySelectorAll('iframe');

for (var i = 0; i < iframes.length; i++) {
var iframe = iframes[i];
var isValidMessageSource = iframe.contentWindow === event.source;

if (isVimeoEmbed(iframe.src) && isValidMessageSource) {
(function () {
var player = new Player(iframe);
player.getVideoId().then(function (videoId) {
var matches = new RegExp("[?&]vimeo_t_".concat(videoId, "=([^&#]*)")).exec(window.location.href);

if (matches && matches[1]) {
var sec = decodeURI(matches[1]);
player.setCurrentTime(sec);
}

return;
}).catch(handleError);
})();
}
}
};

window.addEventListener('message', onMessage);
}

/* MIT License

Expand Down Expand Up @@ -2570,6 +2630,7 @@ if (!isNode) {
initializeEmbeds();
resizeEmbeds();
initAppendVideoMetadata();
checkUrlTimeParam();
}

export default Player;
61 changes: 61 additions & 0 deletions dist/player.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/player.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/player.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/player.min.js.map

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/lib/embed.js
Expand Up @@ -254,3 +254,57 @@ export function initAppendVideoMetadata(parent = document) {

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);
}
11 changes: 10 additions & 1 deletion src/player.js
Expand Up @@ -5,7 +5,15 @@ import Promise from 'native-promise-only';

import { storeCallback, getCallbacks, removeCallback, swapCallbacks } from './lib/callbacks';
import { getMethodName, isDomElement, isVimeoUrl, getVimeoUrl, isNode } from './lib/functions';
import { getOEmbedParameters, getOEmbedData, createEmbed, initializeEmbeds, resizeEmbeds, initAppendVideoMetadata } from './lib/embed';
import {
getOEmbedParameters,
getOEmbedData,
createEmbed,
initializeEmbeds,
resizeEmbeds,
initAppendVideoMetadata,
checkUrlTimeParam
} from './lib/embed';
import { parseMessageData, postMessage, processData } from './lib/postmessage';
import { initializeScreenfull } from './lib/screenfull.js';

Expand Down Expand Up @@ -1213,6 +1221,7 @@ if (!isNode) {
initializeEmbeds();
resizeEmbeds();
initAppendVideoMetadata();
checkUrlTimeParam();
}

export default Player;