glowingbear-mainbox/js/plugins.js

201 lines
6.1 KiB
JavaScript
Raw Normal View History

2013-10-09 01:07:55 +02:00
/*
* This file contains the plugin definitions
*/
plugins = angular.module('plugins', []);
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.
*/
var Plugin = function(contentForMessage) {
return {
contentForMessage: contentForMessage,
exclusive: false,
name: "additional content"
}
}
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.
*
*/
plugins.service('plugins', ['userPlugins', '$sce', function(userPlugins, $sce) {
var nsfwRegexp = new RegExp('nsfw', 'i');
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-10-09 01:07:55 +02: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
*/
var registerPlugins = function(userPlugins) {
for (var i = 0; i < userPlugins.length; i++) {
plugins.push(userPlugins[i]);
};
2013-10-08 23:55:30 +02:00
}
2013-10-09 01:07:55 +02:00
/*
* Iterates through all the registered plugins
* and run their contentForMessage function.
*/
2013-10-08 23:55:30 +02:00
var contentForMessage = function(message) {
message.metadata = [];
2013-10-08 23:55:30 +02:00
for (var i = 0; i < plugins.length; i++) {
var nsfw = false;
var visible = true;
if (message.text.match(nsfwRegexp)) {
var nsfw = true;
var visible = false;
}
var pluginContent = plugins[i].contentForMessage(message.text);
2013-10-08 23:55:30 +02:00
if (pluginContent) {
var pluginContent = {'visible': visible,
'content': $sce.trustAsHtml(pluginContent),
'nsfw': nsfw,
'name': plugins[i].name }
2013-10-13 20:33:09 +02:00
message.metadata.push(pluginContent);
2013-10-08 23:55:30 +02:00
if (plugins[i].exclusive) {
break;
}
}
}
/* Replace all URLs with hyperlinks */
var urlRegexp = RegExp(/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/g);
for(k in message.content) {
var text = message.content[k].text;
var url = text.match(urlRegexp);
for(i in url) {
var u = url[i];
text = text.replace(u, '<a target="_blank" href="' + u + '">' + u + '</a>');
}
message.content[k].text = $sce.trustAsHtml(text);
}
return message;
}
2013-10-08 23:55:30 +02:00
return {
registerPlugins: registerPlugins,
2013-10-08 23:55:30 +02:00
contentForMessage: contentForMessage
}
}
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();
this.PluginManager.registerPlugins(userPlugins.plugins);
2013-10-09 01:07:55 +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.
*
*/
plugins.factory('userPlugins', function() {
2013-10-13 21:14:43 +02:00
var urlRegexp = RegExp(/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/);
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 = '';
for(i in match) {
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;
});
spotifyPlugin.name = 'Spotify track'
/*
* YouTube Embedded Player
*
* See: https://developers.google.com/youtube/player_parameters
*/
var youtubePlugin = new Plugin(function(message) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = message.match(regExp);
if (match && match[7].length==11){
var token = match[7];
var embedurl = "http://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>';
}
return null;
});
youtubePlugin.name = 'YouTube video';
/*
* Image Preview
*/
var imagePlugin = new Plugin(function(message) {
2013-10-13 21:14:43 +02:00
var url = message.match(urlRegexp);
var content = null;
2013-10-13 21:14:43 +02:00
if (url) {
var url = url[0]; /* Actually parse one url per message */
2013-10-13 21:14:43 +02:00
if (url.match(/png$|gif$|jpg$|jpeg$/)) {
/* A fukung.net URL may end by an image extension but is not a direct link. */
if (url.indexOf("fukung.net/v/") != -1) {
url = url.replace(/.*\//, "http://media.fukung.net/imgs/")
}
content = '<img src="' + url + '" height="300">';
}
}
return content;
});
2013-10-13 21:00:00 +02:00
imagePlugin.name = 'image';
return {
2013-10-18 16:47:53 +02:00
plugins: [youtubePlugin, imagePlugin, spotifyPlugin]
}
});