Improve youtube matching URL

closes #266
This commit is contained in:
Lorenz Hübschle-Schneider 2014-04-25 19:03:05 +01:00
parent 253e2ffc60
commit b91241744c
1 changed files with 11 additions and 6 deletions

View File

@ -143,15 +143,20 @@ plugins.factory('userPlugins', function() {
*/ */
var youtubePlugin = new Plugin(function(message) { var youtubePlugin = new Plugin(function(message) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var regExp = /((?:https?:\/\/)?(?:www\.)?(?:youtube.com|youtu.be)\/(?:v\/|embed\/|watch(?:\?v=|\/))?([a-zA-Z0-9-]+))/gm;
var match = message.match(regExp); var match = regExp.exec(message);
if (match && match[7].length==11){ var retval = '';
var token = match[7];
// iterate over all matches
while (match !== null){
var token = match[2];
var embedurl = "https://www.youtube.com/embed/" + token + "?html5=1&iv_load_policy=3&modestbranding=1&rel=0&showinfo=0"; var embedurl = "https://www.youtube.com/embed/" + token + "?html5=1&iv_load_policy=3&modestbranding=1&rel=0&showinfo=0";
return '<iframe width="560" height="315" src="'+ embedurl + '" frameborder="0" allowfullscreen frameborder="0"></iframe>'; retval += '<iframe width="560" height="315" src="'+ embedurl + '" frameborder="0" allowfullscreen frameborder="0"></iframe>';
// next match
match = regExp.exec(message);
} }
return null; return retval;
}); });
youtubePlugin.name = 'YouTube video'; youtubePlugin.name = 'YouTube video';