Merge pull request #408 from gurupras/urlparser

Added logic to parse youtube video id
This commit is contained in:
Sam Potts
2016-11-02 16:59:28 +11:00
committed by GitHub

View File

@ -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');