From b256c102e80a69dd9722632aa0a2db63b284783c Mon Sep 17 00:00:00 2001 From: Frosch Date: Wed, 29 Sep 2021 13:04:56 +0200 Subject: [PATCH] Vimeo private videos: check for hash in src and add as a param (#2322) * check for hash in src and add as a param * Enable different methods of passing hash --- src/js/config/defaults.js | 1 + src/js/plugins/vimeo.js | 42 +++++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index aa219bad..320b654e 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -398,6 +398,7 @@ const defaults = { embed: { provider: 'data-plyr-provider', id: 'data-plyr-embed-id', + hash: 'data-plyr-embed-hash', }, }, diff --git a/src/js/plugins/vimeo.js b/src/js/plugins/vimeo.js index 10246c66..28aa34f3 100644 --- a/src/js/plugins/vimeo.js +++ b/src/js/plugins/vimeo.js @@ -28,6 +28,25 @@ function parseId(url) { return url.match(regex) ? RegExp.$2 : url; } +// Try to extract a hash for private videos from the URL +function parseHash(url) { + /* This regex matches a hexadecimal hash if given in any of these forms: + * - [https://player.]vimeo.com/video/{id}/{hash}[?params] + * - [https://player.]vimeo.com/video/{id}?h={hash}[¶ms] + * - [https://player.]vimeo.com/video/{id}?[params]&h={hash} + * - video/{id}/{hash} + * If matched, the hash is available in the named group `hash` + */ + const regex = /^.*(?:vimeo.com\/|video\/)(?:\d+)(?:\?.*\&*h=|\/)+(?[\d,a-f]+)/ + const found = url.match(regex) + + if (found) { + return found.groups.hash + } + + return null +} + // Set playback state and trigger change (only on actual change) function assurePlaybackState(play) { if (play && !this.embed.hasPlayed) { @@ -72,6 +91,19 @@ const vimeo = { const config = player.config.vimeo; const { premium, referrerPolicy, ...frameParams } = config; + // Get the source URL or ID + let source = player.media.getAttribute('src'); + let hash = '' + // Get from
if needed + if (is.empty(source)) { + source = player.media.getAttribute(player.config.attributes.embed.id); + // hash can also be set as attribute on the
+ hash = player.media.getAttribute(player.config.attributes.embed.hash); + } else { + hash = parseHash(source) + } + const hashParam = (!!hash) ? {h: hash} : {} + // If the owner has a pro or premium account then we can hide controls etc if (premium) { Object.assign(frameParams, { @@ -87,17 +119,11 @@ const vimeo = { muted: player.muted, gesture: 'media', playsinline: !this.config.fullscreen.iosNative, + // hash has to be added to iframe-URL + ...hashParam, ...frameParams, }); - // Get the source URL or ID - let source = player.media.getAttribute('src'); - - // Get from
if needed - if (is.empty(source)) { - source = player.media.getAttribute(player.config.attributes.embed.id); - } - const id = parseId(source); // Build an iframe const iframe = createElement('iframe');