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

fix: allow unlisted vimeo video urls when loading using data attributes (#2504) #2505

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Or the `<div>` non progressively enhanced method:
<div id="player" data-plyr-provider="vimeo" data-plyr-embed-id="76979871"></div>
```

_Note_: The `data-plyr-embed-id` can either be the video ID or URL for the media.

## JavaScript

You can use Plyr as an ES6 module as follows:
Expand Down
18 changes: 13 additions & 5 deletions src/js/plugins/vimeo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ function parseId(url) {
return url;
}

const regex = /^.*(vimeo.com\/|video\/)(\d+).*/;
return url.match(regex) ? RegExp.$2 : url;
const regex = /^(.*vimeo.com\/|.*video\/)?(\d+).*/;
const found = url.match(regex);

return !found ? url : found[found.length - 1];
}

// Try to extract a hash for private videos from the URL
Expand All @@ -35,12 +37,13 @@ function parseHash(url) {
* - [https://player.]vimeo.com/video/{id}?h={hash}[&params]
* - [https://player.]vimeo.com/video/{id}?[params]&h={hash}
* - video/{id}/{hash}
* If matched, the hash is available in capture group 4
* - {id}/{hash}
* If matched, the hash is available in the captured group
*/
const regex = /^.*(vimeo.com\/|video\/)(\d+)(\?.*&*h=|\/)+([\d,a-f]+)/;
const regex = /^(.*vimeo.com\/|.*video\/)?(\d+)(\?.*&*h=|\/)+([\d,a-f]+)/;
const found = url.match(regex);

return found && found.length === 5 ? found[4] : null;
return !found ? url : found[found.length - 1];
}

// Set playback state and trigger change (only on actual change)
Expand Down Expand Up @@ -94,6 +97,11 @@ const vimeo = {
source = player.media.getAttribute(player.config.attributes.embed.id);
// hash can also be set as attribute on the <div>
hash = player.media.getAttribute(player.config.attributes.embed.hash);
// In case hash is not explicitly set as attribute we try to parse it from the id attribute
if (!hash) {
hash = parseHash(source);
alert(hash);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to remove the alert() here?

}
} else {
hash = parseHash(source);
}
Expand Down