diff options
Diffstat (limited to 'src/js/plugins/youtube.js')
-rw-r--r-- | src/js/plugins/youtube.js | 63 |
1 files changed, 59 insertions, 4 deletions
diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index da127bed..a46773a0 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -23,6 +23,22 @@ const youtube = { // Set ID this.media.setAttribute('id', utils.generateId(this.type)); + // Get the media title via Google API + const key = this.config.keys.google; + if (utils.is.string(key) && !utils.is.empty(key)) { + const url = `https://www.googleapis.com/youtube/v3/videos?id=${videoId}&key=${key}&fields=items(snippet(title))&part=snippet`; + + fetch(url) + .then(response => (response.ok ? response.json() : null)) + .then(result => { + if (result !== null && utils.is.object(result)) { + this.config.title = result.items[0].snippet.title; + ui.setTitle.call(this); + } + }) + .catch(() => {}); + } + // Setup API if (utils.is.object(window.YT)) { youtube.ready.call(this, videoId); @@ -81,10 +97,47 @@ const youtube = { }, events: { onError(event) { - utils.dispatchEvent.call(player, player.media, 'error', true, { + // If we've already fired an error, don't do it again + // YouTube fires onError twice + if (utils.is.object(player.media.error)) { + return; + } + + const detail = { code: event.data, - embed: event.target, - }); + }; + + // Messages copied from https://developers.google.com/youtube/iframe_api_reference#onError + switch (event.data) { + case 2: + detail.message = + 'The request contains an invalid parameter value. For example, this error occurs if you specify a video ID that does not have 11 characters, or if the video ID contains invalid characters, such as exclamation points or asterisks.'; + break; + + case 5: + detail.message = + 'The requested content cannot be played in an HTML5 player or another error related to the HTML5 player has occurred.'; + break; + + case 100: + detail.message = + 'The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.'; + break; + + case 101: + case 150: + detail.message = + 'The owner of the requested video does not allow it to be played in embedded players.'; + break; + + default: + detail.message = 'An unknown error occured'; + break; + } + + player.media.error = detail; + + utils.dispatchEvent.call(player, player.media, 'error'); }, onPlaybackQualityChange(event) { // Get the instance @@ -207,7 +260,9 @@ const youtube = { } // Set title - player.config.title = instance.getVideoData().title; + if (utils.is.function(instance.getVideoData)) { + player.config.title = instance.getVideoData().title; + } // Set the tabindex to avoid focus entering iframe if (player.supported.ui) { |