From 0ef87f93a6e93abbdcf4cc7d949ed6938ab79e11 Mon Sep 17 00:00:00 2001 From: Guru Prasad Srinivasa Date: Sun, 30 Oct 2016 00:49:09 -0400 Subject: [PATCH] Added logic to parse youtube video id This commit is targetted at solving issue#345 and adds functionality to parse youtube video IDs from various types of youtube video URLs. Other embed types like vimeo/soundcloud can be extended by following a similar structure as implemented in this commit. --- src/js/plyr.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/js/plyr.js b/src/js/plyr.js index dd14f7674..4ebb060ad 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -3259,6 +3259,19 @@ } } + // Taken from https://gist.github.com/takien/4077195 + function parseYoutubeVideoId(url) { + var videoId; + url = url.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/); + if(url[2] !== undefined) { + videoId = url[2].split(/[^0-9a-z_\-]/i); + videoId = videoId[0]; + } else { + videoId = url; + } + return videoId; + } + // Setup a player function _init() { // Bail if the element is initialized @@ -3287,6 +3300,12 @@ plyr.type = media.getAttribute('data-type'); plyr.embedId = media.getAttribute('data-video-id'); + switch(plyr.type) { + case 'youtube': + plyr.embedId = parseYoutubeVideoId(plyr.embedId); + break; + } + // Clean up media.removeAttribute('data-type'); media.removeAttribute('data-video-id');