Skip to content

Commit b256c10

Browse files
Froschsampotts
authored andcommitted
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
1 parent a3716fc commit b256c10

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/js/config/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ const defaults = {
398398
embed: {
399399
provider: 'data-plyr-provider',
400400
id: 'data-plyr-embed-id',
401+
hash: 'data-plyr-embed-hash',
401402
},
402403
},
403404

src/js/plugins/vimeo.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ function parseId(url) {
2828
return url.match(regex) ? RegExp.$2 : url;
2929
}
3030

31+
// Try to extract a hash for private videos from the URL
32+
function parseHash(url) {
33+
/* This regex matches a hexadecimal hash if given in any of these forms:
34+
* - [https://player.]vimeo.com/video/{id}/{hash}[?params]
35+
* - [https://player.]vimeo.com/video/{id}?h={hash}[&params]
36+
* - [https://player.]vimeo.com/video/{id}?[params]&h={hash}
37+
* - video/{id}/{hash}
38+
* If matched, the hash is available in the named group `hash`
39+
*/
40+
const regex = /^.*(?:vimeo.com\/|video\/)(?:\d+)(?:\?.*\&*h=|\/)+(?<hash>[\d,a-f]+)/
41+
const found = url.match(regex)
42+
43+
if (found) {
44+
return found.groups.hash
45+
}
46+
47+
return null
48+
}
49+
3150
// Set playback state and trigger change (only on actual change)
3251
function assurePlaybackState(play) {
3352
if (play && !this.embed.hasPlayed) {
@@ -72,6 +91,19 @@ const vimeo = {
7291
const config = player.config.vimeo;
7392
const { premium, referrerPolicy, ...frameParams } = config;
7493

94+
// Get the source URL or ID
95+
let source = player.media.getAttribute('src');
96+
let hash = ''
97+
// Get from <div> if needed
98+
if (is.empty(source)) {
99+
source = player.media.getAttribute(player.config.attributes.embed.id);
100+
// hash can also be set as attribute on the <div>
101+
hash = player.media.getAttribute(player.config.attributes.embed.hash);
102+
} else {
103+
hash = parseHash(source)
104+
}
105+
const hashParam = (!!hash) ? {h: hash} : {}
106+
75107
// If the owner has a pro or premium account then we can hide controls etc
76108
if (premium) {
77109
Object.assign(frameParams, {
@@ -87,17 +119,11 @@ const vimeo = {
87119
muted: player.muted,
88120
gesture: 'media',
89121
playsinline: !this.config.fullscreen.iosNative,
122+
// hash has to be added to iframe-URL
123+
...hashParam,
90124
...frameParams,
91125
});
92126

93-
// Get the source URL or ID
94-
let source = player.media.getAttribute('src');
95-
96-
// Get from <div> if needed
97-
if (is.empty(source)) {
98-
source = player.media.getAttribute(player.config.attributes.embed.id);
99-
}
100-
101127
const id = parseId(source);
102128
// Build an iframe
103129
const iframe = createElement('iframe');

0 commit comments

Comments
 (0)