Restructure plugin constructors to make them more convenient

This commit is contained in:
Lorenz Hübschle-Schneider 2015-02-08 11:08:12 +01:00
parent 4b65847b46
commit c57911db64

View file

@ -10,15 +10,42 @@ var plugins = angular.module('plugins', []);
/* /*
* Definition of a user provided plugin with sensible default values * Definition of a user provided plugin with sensible default values
* *
* User plugins are created by providing a contentForMessage function * User plugins are created by providing a name and a contentForMessage
* that parses a string and return any additional content. * function that parses a string and returns any additional content.
*/ */
var Plugin = function(contentForMessage) { var Plugin = function(name, contentForMessage) {
return { return {
contentForMessage: contentForMessage, contentForMessage: contentForMessage,
exclusive: false, exclusive: false,
name: "additional content" name: name
};
};
// Regular expression that detects URLs for UrlPlugin
var urlRegexp = /(?:ftp|https?):\/\/\S*[^\s.;,(){}<>]/g;
/*
* Definition of a user provided plugin that consumes URLs
*
* URL plugins are created by providing a name and a function that
* that parses a URL and returns any additional content.
*/
var UrlPlugin = function(name, urlCallback) {
return {
contentForMessage: function(message) {
var urls = message.match(urlRegexp);
var content = [];
for (var i = 0; urls && i < urls.length; i++) {
var result = urlCallback(urls[i]);
if (result) {
content.push(result);
}
}
return content;
},
exclusive: false,
name: name
}; };
}; };
@ -32,8 +59,6 @@ var Plugin = function(contentForMessage) {
*/ */
plugins.service('plugins', ['userPlugins', '$sce', function(userPlugins, $sce) { plugins.service('plugins', ['userPlugins', '$sce', function(userPlugins, $sce) {
var nsfwRegexp = new RegExp('nsfw', 'i');
/* /*
* Defines the plugin manager object * Defines the plugin manager object
*/ */
@ -52,6 +77,8 @@ plugins.service('plugins', ['userPlugins', '$sce', function(userPlugins, $sce) {
} }
}; };
var nsfwRegexp = new RegExp('nsfw', 'i');
/* /*
* Iterates through all the registered plugins * Iterates through all the registered plugins
* and run their contentForMessage function. * and run their contentForMessage function.
@ -146,23 +173,6 @@ plugins.factory('userPlugins', function() {
document.body.appendChild(script); document.body.appendChild(script);
}; };
var urlRegexp = new RegExp(/(?:ftp|https?):\/\/\S*[^\s.;,(){}<>]/g);
var urlPlugin = function(callback) {
return function(message) {
var urls = message.match(urlRegexp);
var content = [];
for (var i = 0; urls && i < urls.length; i++) {
var result = callback(urls[i]);
if (result) {
content.push(result);
}
}
return content;
};
};
/* /*
* Spotify Embedded Player * Spotify Embedded Player
* *
@ -170,7 +180,7 @@ plugins.factory('userPlugins', function() {
* *
*/ */
var spotifyPlugin = new Plugin(function(message) { var spotifyPlugin = new Plugin('Spotify track', function(message) {
var content = []; var content = [];
var addMatch = function(match) { var addMatch = function(match) {
for (var i = 0; match && i < match.length; i++) { for (var i = 0; match && i < match.length; i++) {
@ -182,32 +192,29 @@ plugins.factory('userPlugins', function() {
addMatch(message.match(/open.spotify.com\/track\/([a-zA-Z-0-9]{22})/g)); addMatch(message.match(/open.spotify.com\/track\/([a-zA-Z-0-9]{22})/g));
return content; return content;
}); });
spotifyPlugin.name = 'Spotify track';
/* /*
* YouTube Embedded Player * YouTube Embedded Player
* *
* See: https://developers.google.com/youtube/player_parameters * See: https://developers.google.com/youtube/player_parameters
*/ */
var youtubePlugin = new Plugin(urlPlugin(function(url) { var youtubePlugin = new UrlPlugin('YouTube video', function(url) {
var regex = /(?:youtube.com|youtu.be)\/(?:v\/|embed\/|watch(?:\?v=|\/))?([a-zA-Z0-9-]+)/i, var regex = /(?:youtube.com|youtu.be)\/(?:v\/|embed\/|watch(?:\?v=|\/))?([a-zA-Z0-9-]+)/i,
match = url.match(regex); match = url.match(regex);
if (match){ if (match){
var token = match[1]; var token = match[1],
var embedurl = "https://www.youtube.com/embed/" + token + "?html5=1&iv_load_policy=3&modestbranding=1&rel=0&showinfo=0"; 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>'; return '<iframe width="560" height="315" src="'+ embedurl + '" frameborder="0" allowfullscreen frameborder="0"></iframe>';
} }
})); });
youtubePlugin.name = 'YouTube video';
/* /*
* Dailymotion Embedded Player * Dailymotion Embedded Player
* *
* See: http://www.dailymotion.com/doc/api/player.html * See: http://www.dailymotion.com/doc/api/player.html
*/ */
var dailymotionPlugin = new Plugin(function(message) { var dailymotionPlugin = new Plugin('Dailymotion video', function(message) {
var rPath = /dailymotion.com\/.*video\/([^_?# ]+)/; var rPath = /dailymotion.com\/.*video\/([^_?# ]+)/;
var rAnchor = /dailymotion.com\/.*#video=([^_& ]+)/; var rAnchor = /dailymotion.com\/.*#video=([^_& ]+)/;
var rShorten = /dai.ly\/([^_?# ]+)/; var rShorten = /dai.ly\/([^_?# ]+)/;
@ -221,13 +228,11 @@ plugins.factory('userPlugins', function() {
return null; return null;
}); });
dailymotionPlugin.name = 'Dailymotion video';
/* /*
* AlloCine Embedded Player * AlloCine Embedded Player
*/ */
var allocinePlugin = new Plugin(function(message) { var allocinePlugin = new Plugin('AlloCine video', function(message) {
var rVideokast = /allocine.fr\/videokast\/video-(\d+)/; var rVideokast = /allocine.fr\/videokast\/video-(\d+)/;
var rCmedia = /allocine.fr\/.*cmedia=(\d+)/; var rCmedia = /allocine.fr\/.*cmedia=(\d+)/;
@ -240,74 +245,65 @@ plugins.factory('userPlugins', function() {
return null; return null;
}); });
allocinePlugin.name = 'AlloCine video';
/* /*
* Image Preview * Image Preview
*/ */
var imagePlugin = new Plugin( var imagePlugin = new UrlPlugin('image', function(url) {
urlPlugin(function(url) { var embed = false;
var embed = false; // Check the get parameters as well, they might contain an image to load
// Check the get parameters as well, they might contain an image to load var segments = url.split(/[?&]/).forEach(function(param) {
var segments = url.split(/[?&]/).forEach(function(param) { if (param.match(/\.(png|gif|jpg|jpeg)(:(small|medium|large))?$/i)) {
if (param.match(/\.(png|gif|jpg|jpeg)(:(small|medium|large))?$/i)) { embed = true;
embed = true;
}
});
if (embed) {
/* A fukung.net URL may end by an image extension but is not a direct link. */
if (url.indexOf("^https?://fukung.net/v/") != -1) {
url = url.replace(/.*\//, "http://media.fukung.net/imgs/");
} 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:/, "");
} else if (url.match(/^https:\/\/www\.dropbox\.com\/s\/[a-z0-9]+\/[^?]+$/i)) {
// Dropbox requires a get parameter, dl=1
url = url + "?dl=1";
}
return '<a target="_blank" href="'+url+'"><img class="embed" src="' + url + '"></a>';
} }
}) });
); if (embed) {
imagePlugin.name = 'image'; /* A fukung.net URL may end by an image extension but is not a direct link. */
if (url.indexOf("^https?://fukung.net/v/") != -1) {
url = url.replace(/.*\//, "http://media.fukung.net/imgs/");
} 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:/, "");
} else if (url.match(/^https:\/\/www\.dropbox\.com\/s\/[a-z0-9]+\/[^?]+$/i)) {
// Dropbox requires a get parameter, dl=1
// TODO strip an existing dl=0 parameter
url = url + "?dl=1";
}
return '<a target="_blank" href="'+url+'"><img class="embed" src="' + url + '"></a>';
}
});
/* /*
* Cloud Music Embedded Players * Cloud Music Embedded Players
*/ */
var cloudmusicPlugin = new Plugin( var cloudmusicPlugin = new UrlPlugin('cloud music', function(url) {
urlPlugin(function(url) { /* SoundCloud http://help.soundcloud.com/customer/portal/articles/247785-what-widgets-can-i-use-from-soundcloud- */
/* SoundCloud http://help.soundcloud.com/customer/portal/articles/247785-what-widgets-can-i-use-from-soundcloud- */ if (url.match(/^https?:\/\/soundcloud.com\//)) {
if (url.match(/^https?:\/\/soundcloud.com\//)) { return '<iframe width="100%" height="120" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=' + url + '&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>';
return '<iframe width="100%" height="120" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=' + url + '&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>'; }
}
/* MixCloud */ /* MixCloud */
if (url.match(/^https?:\/\/([a-z]+\.)?mixcloud.com\//)) { if (url.match(/^https?:\/\/([a-z]+\.)?mixcloud.com\//)) {
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 '<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>';
} }
}) });
);
cloudmusicPlugin.name = 'cloud music';
/* /*
* Google Maps * Google Maps
*/ */
var googlemapPlugin = new Plugin( var googlemapPlugin = new UrlPlugin('Google Map', function(url) {
urlPlugin(function(url) { if (url.match(/^https?:\/\/maps\.google\./i) || url.match(/^https?:\/\/(?:[\w]+\.)?google\.[\w]+\/maps/i)) {
if (url.match(/^https?:\/\/maps\.google\./i) || url.match(/^https?:\/\/(?:[\w]+\.)?google\.[\w]+\/maps/i)) { return '<iframe width="450" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + url + '&output=embed"></iframe>';
return '<iframe width="450" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + url + '&output=embed"></iframe>'; }
} });
})
);
googlemapPlugin.name = 'Google Map';
/* /*
* Asciinema plugin * Asciinema plugin
*/ */
var asciinemaPlugin = new Plugin(urlPlugin(function(url) { var asciinemaPlugin = new UrlPlugin('ascii cast', function(url) {
var regexp = /^https?:\/\/(?:www\.)?asciinema.org\/a\/(\d+)/i; var regexp = /^https?:\/\/(?:www\.)?asciinema.org\/a\/(\d+)/i,
var match = url.match(regexp); match = url.match(regexp);
if (match) { if (match) {
var id = match[1]; var id = match[1];
return function() { return function() {
@ -319,91 +315,78 @@ plugins.factory('userPlugins', function() {
element.appendChild(scriptElem); element.appendChild(scriptElem);
}; };
} }
})); });
asciinemaPlugin.name = "ascii cast";
var yrPlugin = new Plugin( var yrPlugin = new UrlPlugin('meteogram', function(url) {
urlPlugin(function(url) { var regexp = /^https?:\/\/(?:www\.)?yr\.no\/(place|stad|sted|sadji|paikka)\/(([^\s.;,(){}<>\/]+\/){3,})/;
var regexp = /^https?:\/\/(?:www\.)?yr\.no\/(place|stad|sted|sadji|paikka)\/(([^\s.;,(){}<>\/]+\/){3,})/; var match = url.match(regexp);
var match = url.match(regexp); if (match) {
if (match) { var language = match[1];
var language = match[1]; var location = match[2];
var location = match[2]; var city = match[match.length - 1].slice(0, -1);
var city = match[match.length - 1].slice(0, -1); url = "http://www.yr.no/" + language + "/" + location + "avansert_meteogram.png";
url = "http://www.yr.no/" + language + "/" + location + "avansert_meteogram.png"; return "<img src='" + url + "' alt='Meteogram for " + city + "' />";
return "<img src='" + url + "' alt='Meteogram for " + city + "' />"; }
} });
})
);
yrPlugin.name = "meteogram";
// Embed GitHub gists // Embed GitHub gists
var gistPlugin = new Plugin( var gistPlugin = new UrlPlugin('Gist', function(url) {
urlPlugin(function(url) { var regexp = /^https:\/\/gist\.github.com\/[^.?]+/i;
var regexp = /^https:\/\/gist\.github.com\/[^.?]+/i; var match = url.match(regexp);
var match = url.match(regexp); if (match) {
if (match) { // get the URL from the match to trim away pseudo file endings and request parameters
// get the URL from the match to trim away pseudo file endings and request parameters url = match[0] + '.json';
url = match[0] + '.json'; // load gist asynchronously -- return a function here
// load gist asynchronously -- return a function here return function() {
return function() { var element = this.getElement();
var element = this.getElement(); jsonp(url, function(data) {
jsonp(url, function(data) { // Add the gist stylesheet only once
// Add the gist stylesheet only once if (document.querySelectorAll('link[rel=stylesheet][href="' + data.stylesheet + '"]').length < 1) {
if (document.querySelectorAll('link[rel=stylesheet][href="' + data.stylesheet + '"]').length < 1) { var stylesheet = '<link rel="stylesheet" href="' + data.stylesheet + '"></link>';
var stylesheet = '<link rel="stylesheet" href="' + data.stylesheet + '"></link>'; document.getElementsByTagName('head')[0].innerHTML += stylesheet;
document.getElementsByTagName('head')[0].innerHTML += stylesheet; }
} element.innerHTML = '<div style="clear:both">' + data.div + '</div>';
element.innerHTML = '<div style="clear:both">' + data.div + '</div>'; });
}); };
}; }
} });
})
);
gistPlugin.name = 'Gist';
var tweetPlugin = new Plugin( var tweetPlugin = new UrlPlugin('Tweet', function(url) {
urlPlugin(function(url) { var regexp = /^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(?:es)?\/(\d+)/i;
var regexp = /^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(?:es)?\/(\d+)/i; var match = url.match(regexp);
var match = url.match(regexp); if (match) {
if (match) { url = 'https://api.twitter.com/1/statuses/oembed.json?id=' + match[2];
url = 'https://api.twitter.com/1/statuses/oembed.json?id=' + match[2]; return function() {
return function() { var element = this.getElement();
var element = this.getElement(); jsonp(url, function(data) {
jsonp(url, function(data) { // separate the HTML into content and script tag
// sepearate the HTML into content and script tag var scriptIndex = data.html.indexOf("<script ");
var scriptIndex = data.html.indexOf("<script "); var content = data.html.substr(0, scriptIndex);
var content = data.html.substr(0, scriptIndex); // Set DNT (Do Not Track)
// Set DNT (Do Not Track) content = content.replace("<blockquote class=\"twitter-tweet\">", "<blockquote class=\"twitter-tweet\" data-dnt=\"true\">");
content = content.replace("<blockquote class=\"twitter-tweet\">", "<blockquote class=\"twitter-tweet\" data-dnt=\"true\">"); element.innerHTML = content;
element.innerHTML = content;
// The script tag needs to be generated manually or the browser won't load it // The script tag needs to be generated manually or the browser won't load it
var scriptElem = document.createElement('script'); var scriptElem = document.createElement('script');
// Hardcoding the URL here, I don't suppose it's going to change anytime soon // Hardcoding the URL here, I don't suppose it's going to change anytime soon
scriptElem.src = "//platform.twitter.com/widgets.js"; scriptElem.src = "//platform.twitter.com/widgets.js";
element.appendChild(scriptElem); element.appendChild(scriptElem);
}); });
}; };
} }
}) });
);
tweetPlugin.name = 'Tweet';
/* /*
* Vine plugin * Vine plugin
*/ */
var vinePlugin = new Plugin( var vinePlugin = new UrlPlugin('Vine', function (url) {
urlPlugin(function (url) { var regexp = /^https?:\/\/(www\.)?vine.co\/v\/([a-zA-Z0-9]+)(\/.*)?/i,
var regexp = /^https?:\/\/(www\.)?vine.co\/v\/([a-zA-Z0-9]+)(\/.*)?/i, match = url.match(regexp);
match = url.match(regexp); if (match) {
if (match) { var id = match[2], embedurl = "https://vine.co/v/" + id + "/embed/simple?audio=1";
var id = match[2], embedurl = "https://vine.co/v/" + id + "/embed/simple?audio=1"; return '<iframe class="vine-embed" src="' + embedurl + '" width="600" height="600" frameborder="0"></iframe><script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>';
return '<iframe class="vine-embed" src="' + embedurl + '" width="600" height="600" frameborder="0"></iframe><script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>'; }
} });
})
);
vinePlugin.name = 'Vine';
return { return {
plugins: [youtubePlugin, dailymotionPlugin, allocinePlugin, imagePlugin, spotifyPlugin, cloudmusicPlugin, googlemapPlugin, asciinemaPlugin, yrPlugin, gistPlugin, tweetPlugin, vinePlugin] plugins: [youtubePlugin, dailymotionPlugin, allocinePlugin, imagePlugin, spotifyPlugin, cloudmusicPlugin, googlemapPlugin, asciinemaPlugin, yrPlugin, gistPlugin, tweetPlugin, vinePlugin]