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

CORS support for preview thumbnails #2777

Open
wants to merge 2 commits into
base: develop
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,19 @@ TextTrack captions are also hosted on another domain, you will need to add this
on CORS checkout the MDN docs:
[https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)

If you need to attach credentials (such as cookies, authorization headers, or certificates) to preview thumbnail requests, use the `withCredentials` option as
illustrated below:

```js
const player = new Plyr(video, {
previewThumbnails: {
enabled: true,
src: 'https://cdn.example.com/storyboard.vtt',
withCredentials: true,
},
});
```

## Captions

WebVTT captions are supported. To add a caption track, check the HTML example above and look for the `<track>` element. Be sure to
Expand Down Expand Up @@ -412,7 +425,7 @@ Note the single quotes encapsulating the JSON and double quotes on the object ke
| `urls` | Object | See source. | If you wish to override any API URLs then you can do so here. You can also set a custom download URL for the download button. |
| `vimeo` | Object | `{ byline: false, portrait: false, title: false, speed: true, transparent: false }` | See [Vimeo embed options](https://github.com/vimeo/player.js/#embed-options). Some are set automatically based on other config options, namely: `loop`, `autoplay`, `muted`, `gesture`, `playsinline` |
| `youtube` | Object | `{ noCookie: false, rel: 0, showinfo: 0, iv_load_policy: 3, modestbranding: 1 }` | See [YouTube embed options](https://developers.google.com/youtube/player_parameters#Parameters). The only custom option is `noCookie` to use an alternative to YouTube that doesn't use cookies (useful for GDPR, etc). Some are set automatically based on other config options, namely: `autoplay`, `hl`, `controls`, `disablekb`, `playsinline`, `cc_load_policy`, `cc_lang_pref`, `widget_referrer` |
| `previewThumbnails` | Object | `{ enabled: false, src: '' }` | `enabled`: Whether to enable the preview thumbnails (they must be generated by you). `src` must be either a string or an array of strings representing URLs for the VTT files containing the image URL(s). Learn more about [preview thumbnails](#preview-thumbnails) below. |
| `previewThumbnails` | Object | `{ enabled: false, src: '', withCredentials: false }` | `enabled`: Whether to enable the preview thumbnails (they must be generated by you). `src`: Must be either a string or an array of strings representing URLs for the VTT files containing the image URL(s). Learn more about [preview thumbnails](#preview-thumbnails) below. `withCredentials`: Whether to attach credentials (such as cookies and authorization headers) to the requests. |
| `mediaMetadata` | Object | `{ title: '', artist: '', album: '', artwork: [] }` | The [MediaMetadata](https://developer.mozilla.org/en-US/docs/Web/API/MediaMetadata) interface of the Media Session API allows a web page to provide rich media metadata for display in a platform UI. |
| `markers` | Object | `{ enabled: false, points: [] }` | `enabled`: Whether to enable markers. `points` is an array of `{ time: number; label: string; }` objects where `time` represents the marker position in seconds and `label` is the HTML string to be displayed. |

Expand Down
1 change: 1 addition & 0 deletions src/js/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ const defaults = {
previewThumbnails: {
enabled: false,
src: '',
withCredentials: false,
},

// Vimeo plugin
Expand Down
2 changes: 1 addition & 1 deletion src/js/plugins/preview-thumbnails.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class PreviewThumbnails {
// Process individual VTT file
getThumbnail = (url) => {
return new Promise((resolve) => {
fetch(url).then((response) => {
fetch(url, undefined, this.player.config.previewThumbnails.withCredentials).then((response) => {
const thumbnail = {
frames: parseVtt(response),
height: null,
Expand Down
1 change: 1 addition & 0 deletions src/js/plyr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ declare namespace Plyr {
interface PreviewThumbnailsOptions {
enabled?: boolean;
src?: string | string[];
withCredentials?: boolean;
}

interface MediaMetadataArtwork {
Expand Down
7 changes: 6 additions & 1 deletion src/js/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Using XHR to avoid issues with older browsers
// ==========================================================================

export default function fetch(url, responseType = 'text') {
export default function fetch(url, responseType = 'text', withCredentials = false) {
return new Promise((resolve, reject) => {
try {
const request = new XMLHttpRequest();
Expand All @@ -13,6 +13,11 @@ export default function fetch(url, responseType = 'text') {
return;
}

// Set to true if needed for CORS
if (withCredentials) {
request.withCredentials = true;
}

request.addEventListener('load', () => {
if (responseType === 'text') {
try {
Expand Down