2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* This file contains the plugin definitions
|
|
|
|
*/
|
|
|
|
|
2013-10-09 00:57:43 +02:00
|
|
|
plugins = angular.module('plugins', []);
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* Definition of a user provided plugin with sensible default values
|
|
|
|
*
|
|
|
|
* User plugins are created by providing a contentForMessage function
|
|
|
|
* that parses a string and return any additional content.
|
|
|
|
*/
|
2013-10-09 00:57:43 +02:00
|
|
|
var Plugin = function(contentForMessage) {
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-10-09 00:57:43 +02:00
|
|
|
return {
|
|
|
|
contentForMessage: contentForMessage,
|
|
|
|
exclusive: false,
|
2013-10-13 20:57:54 +02:00
|
|
|
name: "additional content"
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
|
|
|
};
|
2013-10-08 23:11:05 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* This service provides access to the plugin manager
|
|
|
|
*
|
|
|
|
* The plugin manager is where the various user provided plugins
|
|
|
|
* are registered. It is responsible for finding additional content
|
|
|
|
* to display when messages are received.
|
|
|
|
*
|
|
|
|
*/
|
2013-10-17 13:36:54 +02:00
|
|
|
plugins.service('plugins', ['userPlugins', '$sce', function(userPlugins, $sce) {
|
2013-10-08 23:11:05 +02:00
|
|
|
|
2013-10-14 17:02:15 +02:00
|
|
|
var nsfwRegexp = new RegExp('nsfw', 'i');
|
2013-10-13 20:44:37 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* Defines the plugin manager object
|
|
|
|
*/
|
2013-10-08 23:55:30 +02:00
|
|
|
var PluginManagerObject = function() {
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
var plugins = [];
|
2013-10-09 01:07:55 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Register the user provides plugins
|
|
|
|
*
|
|
|
|
* @param userPlugins user provided plugins
|
|
|
|
*/
|
2013-10-09 00:57:43 +02:00
|
|
|
var registerPlugins = function(userPlugins) {
|
|
|
|
for (var i = 0; i < userPlugins.length; i++) {
|
|
|
|
plugins.push(userPlugins[i]);
|
2013-12-17 21:39:22 +01:00
|
|
|
}
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* Iterates through all the registered plugins
|
|
|
|
* and run their contentForMessage function.
|
|
|
|
*/
|
2013-10-28 13:21:51 +01:00
|
|
|
var contentForMessage = function(message, visible) {
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-15 18:58:00 +02:00
|
|
|
message.metadata = [];
|
2013-10-08 23:55:30 +02:00
|
|
|
for (var i = 0; i < plugins.length; i++) {
|
2013-10-13 20:44:37 +02:00
|
|
|
|
2013-10-13 20:49:38 +02:00
|
|
|
var nsfw = false;
|
2013-10-15 18:58:00 +02:00
|
|
|
if (message.text.match(nsfwRegexp)) {
|
2013-12-17 21:48:43 +01:00
|
|
|
nsfw = true;
|
2013-10-28 13:21:51 +01:00
|
|
|
visible = false;
|
2013-10-13 20:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 18:58:00 +02:00
|
|
|
var pluginContent = plugins[i].contentForMessage(message.text);
|
2013-10-08 23:55:30 +02:00
|
|
|
if (pluginContent) {
|
2013-12-17 21:48:43 +01:00
|
|
|
pluginContent = {'visible': visible,
|
2013-10-13 20:49:38 +02:00
|
|
|
'content': $sce.trustAsHtml(pluginContent),
|
2013-10-13 20:57:54 +02:00
|
|
|
'nsfw': nsfw,
|
2013-12-17 21:35:36 +01:00
|
|
|
'name': plugins[i].name };
|
2013-10-13 20:33:09 +02:00
|
|
|
|
2013-10-15 18:58:00 +02:00
|
|
|
message.metadata.push(pluginContent);
|
|
|
|
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
if (plugins[i].exclusive) {
|
|
|
|
break;
|
|
|
|
}
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-15 18:58:00 +02:00
|
|
|
|
|
|
|
return message;
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
return {
|
2013-10-09 00:57:43 +02:00
|
|
|
registerPlugins: registerPlugins,
|
2013-10-08 23:55:30 +02:00
|
|
|
contentForMessage: contentForMessage
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
|
|
|
};
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
// Instanciates and registers the plugin manager.
|
2013-10-08 23:55:30 +02:00
|
|
|
this.PluginManager = new PluginManagerObject();
|
2013-10-09 00:57:43 +02:00
|
|
|
this.PluginManager.registerPlugins(userPlugins.plugins);
|
2013-10-09 01:07:55 +02:00
|
|
|
|
2013-10-08 23:06:47 +02:00
|
|
|
}]);
|
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* This factory exposes the collection of user provided plugins.
|
|
|
|
*
|
|
|
|
* To create your own plugin, you need to:
|
|
|
|
*
|
|
|
|
* 1. Define it's contentForMessage function. The contentForMessage
|
|
|
|
* function takes a string as a parameter and returns a HTML string.
|
|
|
|
*
|
|
|
|
* 2. Instanciate a Plugin object with contentForMessage function as it's
|
|
|
|
* argument.
|
|
|
|
*
|
|
|
|
* 3. Add it to the plugins array.
|
|
|
|
*
|
|
|
|
*/
|
2013-10-09 00:57:02 +02:00
|
|
|
plugins.factory('userPlugins', function() {
|
|
|
|
|
2013-10-13 21:14:43 +02:00
|
|
|
var urlRegexp = RegExp(/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/);
|
|
|
|
|
2013-10-18 16:47:53 +02:00
|
|
|
/*
|
|
|
|
* Spotify Embedded Player
|
|
|
|
*
|
|
|
|
* See: https://developer.spotify.com/technologies/widgets/spotify-play-button/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var spotifyPlugin = new Plugin(function(message) {
|
|
|
|
var addMatch = function(match) {
|
|
|
|
var ret = '';
|
2013-12-17 21:55:41 +01:00
|
|
|
for(var i in match) {
|
2013-10-18 16:47:53 +02:00
|
|
|
var id = match[i].substr(match[i].length-22, match[i].length);
|
|
|
|
ret += '<iframe src="//embed.spotify.com/?uri=spotify:track:'+id+'" width="300" height="80" frameborder="0" allowtransparency="true"></iframe>';
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
var match = message.match(/spotify:track:([a-zA-Z-0-9]{22})/g);
|
|
|
|
var ret = addMatch(match);
|
|
|
|
ret += addMatch(message.match(/open.spotify.com\/track\/([a-zA-Z-0-9]{22})/g));
|
|
|
|
return ret;
|
|
|
|
});
|
2013-12-17 21:35:36 +01:00
|
|
|
spotifyPlugin.name = 'Spotify track';
|
2013-10-18 16:47:53 +02:00
|
|
|
|
2013-10-17 22:11:31 +02:00
|
|
|
/*
|
|
|
|
* YouTube Embedded Player
|
|
|
|
*
|
|
|
|
* See: https://developers.google.com/youtube/player_parameters
|
|
|
|
*/
|
2013-10-09 00:57:02 +02:00
|
|
|
var youtubePlugin = new Plugin(function(message) {
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2014-04-25 20:03:05 +02:00
|
|
|
var regExp = /((?:https?:\/\/)?(?:www\.)?(?:youtube.com|youtu.be)\/(?:v\/|embed\/|watch(?:\?v=|\/))?([a-zA-Z0-9-]+))/gm;
|
|
|
|
var match = regExp.exec(message);
|
|
|
|
var retval = '';
|
|
|
|
|
|
|
|
// iterate over all matches
|
|
|
|
while (match !== null){
|
|
|
|
var token = match[2];
|
2014-01-09 22:45:00 +01:00
|
|
|
var embedurl = "https://www.youtube.com/embed/" + token + "?html5=1&iv_load_policy=3&modestbranding=1&rel=0&showinfo=0";
|
2014-04-25 20:03:05 +02:00
|
|
|
retval += '<iframe width="560" height="315" src="'+ embedurl + '" frameborder="0" allowfullscreen frameborder="0"></iframe>';
|
|
|
|
// next match
|
|
|
|
match = regExp.exec(message);
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:03:05 +02:00
|
|
|
return retval;
|
2013-10-09 00:57:02 +02:00
|
|
|
});
|
2013-10-17 22:11:31 +02:00
|
|
|
youtubePlugin.name = 'YouTube video';
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-21 22:10:10 +02:00
|
|
|
/*
|
|
|
|
* Dailymotion Embedded Player
|
|
|
|
*
|
|
|
|
* See: http://www.dailymotion.com/doc/api/player.html
|
|
|
|
*/
|
|
|
|
var dailymotionPlugin = new Plugin(function(message) {
|
|
|
|
|
|
|
|
var rPath = /dailymotion.com\/.*video\/([^_?# ]+)/;
|
|
|
|
var rAnchor = /dailymotion.com\/.*#video=([^_& ]+)/;
|
|
|
|
var rShorten = /dai.ly\/([^_?# ]+)/;
|
|
|
|
|
|
|
|
var match = message.match(rPath) || message.match(rAnchor) || message.match(rShorten);
|
|
|
|
if (match) {
|
|
|
|
var id = match[1];
|
2014-04-04 20:43:13 +02:00
|
|
|
var embedurl = 'https://www.dailymotion.com/embed/video/' + id + '?html&controls=html&startscreen=html&info=0&logo=0&related=0';
|
2013-10-21 22:10:10 +02:00
|
|
|
return '<iframe frameborder="0" width="480" height="270" src="' + embedurl + '"></iframe>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
dailymotionPlugin.name = 'Dailymotion video';
|
|
|
|
|
2013-10-22 22:28:04 +02:00
|
|
|
/*
|
|
|
|
* AlloCine Embedded Player
|
|
|
|
*/
|
|
|
|
var allocinePlugin = new Plugin(function(message) {
|
|
|
|
|
|
|
|
var rVideokast = /allocine.fr\/videokast\/video-(\d+)/;
|
|
|
|
var rCmedia = /allocine.fr\/.*cmedia=(\d+)/;
|
|
|
|
|
|
|
|
var match = message.match(rVideokast) || message.match(rCmedia);
|
|
|
|
if (match) {
|
|
|
|
var id = match[1];
|
|
|
|
var embedurl = 'http://www.allocine.fr/_video/iblogvision.aspx?cmedia=' + id;
|
|
|
|
return '<iframe frameborder="0" width="480" height="270" src="' + embedurl + '"></iframe>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
allocinePlugin.name = 'AlloCine video';
|
|
|
|
|
2013-10-17 22:11:31 +02:00
|
|
|
/*
|
|
|
|
* Image Preview
|
|
|
|
*/
|
2013-10-09 00:57:02 +02:00
|
|
|
var imagePlugin = new Plugin(function(message) {
|
2013-10-18 00:20:04 +02:00
|
|
|
|
2013-10-13 21:14:43 +02:00
|
|
|
var url = message.match(urlRegexp);
|
2013-10-18 00:20:04 +02:00
|
|
|
var content = null;
|
2013-10-13 21:14:43 +02:00
|
|
|
|
2013-10-18 00:20:04 +02:00
|
|
|
if (url) {
|
2013-12-17 21:48:43 +01:00
|
|
|
url = url[0]; /* Actually parse one url per message */
|
2014-03-05 16:53:13 +01:00
|
|
|
if (url.match(/\.(png|gif|jpg|jpeg)$/)) {
|
2013-10-18 00:20:04 +02:00
|
|
|
|
|
|
|
/* A fukung.net URL may end by an image extension but is not a direct link. */
|
2014-03-05 16:53:13 +01:00
|
|
|
if (url.indexOf("^https?://fukung.net/v/") != -1) {
|
2013-12-17 21:35:36 +01:00
|
|
|
url = url.replace(/.*\//, "http://media.fukung.net/imgs/");
|
2014-03-05 16:53:13 +01:00
|
|
|
} else if (url.match(/^http:\/\/(i\.)?imgur\.com\//i)) {
|
|
|
|
// remove protocol specification to load over https if used by g-b
|
|
|
|
url = url.replace(/http:/, "");
|
2013-10-18 00:20:04 +02:00
|
|
|
}
|
|
|
|
|
2013-10-28 13:25:36 +01:00
|
|
|
content = '<a target="_blank" href="'+url+'"><img class="embed" src="' + url + '"></a>';
|
2013-10-18 00:20:04 +02:00
|
|
|
}
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
2013-10-18 00:20:04 +02:00
|
|
|
|
|
|
|
return content;
|
2013-10-09 00:57:02 +02:00
|
|
|
});
|
2013-10-13 21:00:00 +02:00
|
|
|
imagePlugin.name = 'image';
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-18 02:14:39 +02:00
|
|
|
/*
|
|
|
|
* Cloud Music Embedded Players
|
|
|
|
*/
|
|
|
|
var cloudmusicPlugin = new Plugin(function(message) {
|
|
|
|
|
|
|
|
var match = message.match(urlRegexp);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
var url = match[0];
|
|
|
|
|
|
|
|
/* SoundCloud http://help.soundcloud.com/customer/portal/articles/247785-what-widgets-can-i-use-from-soundcloud- */
|
2014-03-05 16:53:13 +01:00
|
|
|
if (url.match(/^https?:\/\/soundcloud.com\//)) {
|
2013-10-18 02:14:39 +02:00
|
|
|
return '<iframe width="100%" height="120" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=' + url + '&color=ff6600&auto_play=false&show_artwork=true"></iframe>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MixCloud */
|
2014-03-05 16:53:13 +01:00
|
|
|
if (url.match(/^https?:\/\/([a-z]+\.)?mixcloud.com\//)) {
|
2013-10-18 02:14:39 +02:00
|
|
|
return '<iframe width="480" height="60" src="//www.mixcloud.com/widget/iframe/?feed=' + url + '&mini=1&stylecolor=&hide_artwork=&embed_type=widget_standard&hide_tracklist=1&hide_cover=" frameborder="0"></iframe>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
cloudmusicPlugin.name = 'cloud music';
|
|
|
|
|
2013-10-18 23:32:39 +02:00
|
|
|
/*
|
|
|
|
* Google Maps
|
|
|
|
*/
|
|
|
|
var googlemapPlugin = new Plugin(function(message) {
|
|
|
|
|
|
|
|
var match = message.match(urlRegexp);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
var url = match[0];
|
|
|
|
|
2014-03-05 16:53:13 +01:00
|
|
|
if (url.match(/^https?:\/\/maps\.google\./i) || url.match(/^https?:\/\/([^\w]+\.)?google\.[^\w]+\/maps/i)) {
|
2013-10-18 23:32:39 +02:00
|
|
|
return '<iframe width="450" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + url + '&output=embed"></iframe>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
googlemapPlugin.name = 'Google Map';
|
|
|
|
|
2014-02-23 04:16:32 +01:00
|
|
|
/*
|
|
|
|
* Asciinema plugin
|
|
|
|
*/
|
|
|
|
var asciinemaPlugin = new Plugin(function(message) {
|
|
|
|
|
2014-03-05 16:53:13 +01:00
|
|
|
var regexp = /^https?:\/\/(www\.)?asciinema.org\/a\/(\d+)/;
|
2014-02-24 11:29:50 +01:00
|
|
|
var match = message.match(regexp);
|
|
|
|
if (match) {
|
2014-02-23 04:16:32 +01:00
|
|
|
var id = match[3];
|
|
|
|
return "<script type='text/javascript' src='https://asciinema.org/a/" + id + ".js' id='asciicast-" + id + "' async></script>";
|
2014-02-24 11:29:50 +01:00
|
|
|
}
|
2014-02-23 04:16:32 +01:00
|
|
|
});
|
|
|
|
asciinemaPlugin.name = "ascii cast";
|
2014-02-24 11:28:43 +01:00
|
|
|
|
2013-10-08 23:06:47 +02:00
|
|
|
return {
|
2014-02-23 04:16:32 +01:00
|
|
|
plugins: [youtubePlugin, dailymotionPlugin, allocinePlugin, imagePlugin, spotifyPlugin, cloudmusicPlugin, googlemapPlugin, asciinemaPlugin]
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2014-02-23 04:16:32 +01:00
|
|
|
|
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
});
|