From 03c9b53232aeab78a7c592e1bcf387312f77a569 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Wed, 24 Oct 2018 22:31:35 +1100 Subject: [PATCH] Allow custom download URL (for streaming, etc) --- src/js/config/defaults.js | 3 ++- src/js/controls.js | 22 ++++++++++++---------- src/js/listeners.js | 2 +- src/js/plyr.js | 9 +++++++++ src/js/utils/is.js | 5 +++++ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 5e2fc4a9..7d0ca7d0 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -133,7 +133,7 @@ const defaults = { 'settings', 'pip', 'airplay', - 'download', + // 'download', 'fullscreen', ], settings: ['captions', 'quality', 'speed'], @@ -186,6 +186,7 @@ const defaults = { // URLs urls: { + download: null, vimeo: { sdk: 'https://player.vimeo.com/api/player.js', iframe: 'https://player.vimeo.com/video/{0}?{1}', diff --git a/src/js/controls.js b/src/js/controls.js index efc45e8a..4f453e6a 100644 --- a/src/js/controls.js +++ b/src/js/controls.js @@ -1229,11 +1229,15 @@ const controls = { // Set the download link setDownloadLink() { - // Set download link - const { download } = this.elements.buttons; - if (is.element(download)) { - download.setAttribute('href', this.source); + const button = this.elements.buttons.download; + + // Bail if no button + if (!is.element(button)) { + return; } + + // Set download link + button.setAttribute('href', this.download); }, // Build the default HTML @@ -1516,15 +1520,13 @@ const controls = { if (this.config.controls.includes('download')) { const attributes = { element: 'a', - href: this.source, + href: this.download, target: '_blank', }; - if (this.isHTML5) { - extend(attributes, { - download: '', - }); - } else if (this.isEmbed) { + const { download } = this.config.urls; + + if (!is.url(download) && this.isEmbed) { extend(attributes, { icon: `logo-${this.provider}`, label: this.provider, diff --git a/src/js/listeners.js b/src/js/listeners.js index 31d74af6..f21e3357 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -431,7 +431,7 @@ class Listeners { controls.updateSetting.call(player, 'quality', null, event.detail.quality); }); - // Update download link + // Update download link when ready and if quality changes on.call(player, player.media, 'ready qualitychange', () => { controls.setDownloadLink.call(player); }); diff --git a/src/js/plyr.js b/src/js/plyr.js index 77582dd7..d549b177 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -788,6 +788,15 @@ class Plyr { return this.media.currentSrc; } + /** + * Get a download URL (either source or custom) + */ + get download() { + const { download } = this.config.urls; + + return is.url(download) ? download : this.source; + } + /** * Set the poster image for a video * @param {input} - the URL for the new poster image diff --git a/src/js/utils/is.js b/src/js/utils/is.js index 2952d486..ab28f2ab 100644 --- a/src/js/utils/is.js +++ b/src/js/utils/is.js @@ -31,6 +31,11 @@ const isUrl = input => { return true; } + // Must be string from here + if (!isString(input)) { + return false; + } + // Add the protocol if required let string = input; if (!input.startsWith('http://') || !input.startsWith('https://')) {