2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* This file contains the plugin definitions
|
|
|
|
*/
|
|
|
|
|
2014-08-24 18:53:55 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var 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
|
|
|
|
*
|
2015-02-08 11:08:12 +01:00
|
|
|
* User plugins are created by providing a name and a contentForMessage
|
|
|
|
* function that parses a string and returns any additional content.
|
2013-10-09 01:07:55 +02:00
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var Plugin = function(name, contentForMessage) {
|
2013-10-09 00:57:43 +02:00
|
|
|
return {
|
|
|
|
contentForMessage: contentForMessage,
|
|
|
|
exclusive: false,
|
2015-02-08 11:08:12 +01:00
|
|
|
name: name
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Regular expression that detects URLs for UrlPlugin
|
2016-01-20 16:50:09 +01:00
|
|
|
var urlRegexp = /(?:(?:https?|ftp):\/\/|www\.|ftp\.)\S*[^\s.;,(){}<>]/g;
|
2015-02-08 11:08:12 +01:00
|
|
|
/*
|
|
|
|
* 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
|
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.
|
|
|
|
*
|
|
|
|
*/
|
2015-10-18 01:23:48 +02:00
|
|
|
plugins.service('plugins', ['userPlugins', '$sce', function(userPlugins, $sce) {
|
2013-10-08 23:11:05 +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
|
|
|
|
2015-02-08 11:08:12 +01:00
|
|
|
var nsfwRegexp = new RegExp('nsfw', 'i');
|
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* Iterates through all the registered plugins
|
|
|
|
* and run their contentForMessage function.
|
|
|
|
*/
|
2014-07-25 23:32:55 +02:00
|
|
|
var contentForMessage = function(message) {
|
2013-10-15 18:58:00 +02:00
|
|
|
message.metadata = [];
|
2014-07-27 13:46:30 +02:00
|
|
|
var addPluginContent = function(content, pluginName, num) {
|
|
|
|
if (num) {
|
|
|
|
pluginName += " " + num;
|
|
|
|
}
|
2014-08-08 15:39:50 +02:00
|
|
|
|
|
|
|
// If content isn't a callback, it's HTML
|
|
|
|
if (!(content instanceof Function)) {
|
|
|
|
content = $sce.trustAsHtml(content);
|
|
|
|
}
|
|
|
|
|
2014-07-27 13:46:30 +02:00
|
|
|
message.metadata.push({
|
2014-08-08 15:39:50 +02:00
|
|
|
'content': content,
|
2014-07-27 13:46:30 +02:00
|
|
|
'nsfw': nsfw,
|
|
|
|
'name': pluginName
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-13 20:44:37 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 21:13:53 +02:00
|
|
|
var pluginContent = plugins[i].contentForMessage(
|
|
|
|
message.text
|
|
|
|
);
|
2014-07-27 13:46:30 +02:00
|
|
|
if (pluginContent && pluginContent !== []) {
|
|
|
|
|
|
|
|
if (pluginContent instanceof Array) {
|
|
|
|
for (var j = pluginContent.length - 1; j >= 0; j--) {
|
|
|
|
// only give a number if there are multiple embeds
|
|
|
|
var num = (pluginContent.length == 1) ? undefined : (j + 1);
|
|
|
|
addPluginContent(pluginContent[j], plugins[i].name, num);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addPluginContent(pluginContent, plugins[i].name);
|
|
|
|
}
|
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:
|
|
|
|
*
|
2014-10-15 07:21:22 +02:00
|
|
|
* 1. Define its contentForMessage function. The contentForMessage
|
2013-10-09 01:07:55 +02:00
|
|
|
* function takes a string as a parameter and returns a HTML string.
|
|
|
|
*
|
2014-10-15 07:21:22 +02:00
|
|
|
* 2. Instantiate a Plugin object with contentForMessage function as its
|
2013-10-09 01:07:55 +02:00
|
|
|
* argument.
|
|
|
|
*
|
|
|
|
* 3. Add it to the plugins array.
|
|
|
|
*
|
|
|
|
*/
|
2013-10-09 00:57:02 +02:00
|
|
|
plugins.factory('userPlugins', function() {
|
2014-08-09 15:17:36 +02:00
|
|
|
// standard JSONp origin policy trick
|
|
|
|
var jsonp = function (url, callback) {
|
|
|
|
var callbackName = 'jsonp_callback_' + Math.round(100000 * Math.random());
|
|
|
|
window[callbackName] = function(data) {
|
|
|
|
delete window[callbackName];
|
|
|
|
document.body.removeChild(script);
|
|
|
|
callback(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
var script = document.createElement('script');
|
|
|
|
script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + callbackName;
|
|
|
|
document.body.appendChild(script);
|
|
|
|
};
|
2013-10-09 00:57:02 +02:00
|
|
|
|
2013-10-18 16:47:53 +02:00
|
|
|
/*
|
|
|
|
* Spotify Embedded Player
|
|
|
|
*
|
|
|
|
* See: https://developer.spotify.com/technologies/widgets/spotify-play-button/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-02-08 11:08:12 +01:00
|
|
|
var spotifyPlugin = new Plugin('Spotify track', function(message) {
|
2014-08-24 18:53:55 +02:00
|
|
|
var content = [];
|
2013-10-18 16:47:53 +02:00
|
|
|
var addMatch = function(match) {
|
2014-07-27 13:47:53 +02:00
|
|
|
for (var i = 0; match && i < match.length; i++) {
|
|
|
|
var id = match[i].substr(match[i].length - 22, match[i].length);
|
2015-10-18 01:15:42 +02:00
|
|
|
var element = angular.element('<iframe></iframe>')
|
|
|
|
.attr('src', '//embed.spotify.com/?uri=spotify:track:' + id)
|
|
|
|
.attr('width', '300')
|
|
|
|
.attr('height', '80')
|
|
|
|
.attr('frameborder', '0')
|
|
|
|
.attr('allowtransparency', 'true');
|
|
|
|
content.push(element.prop('outerHTML'));
|
2013-10-18 16:47:53 +02:00
|
|
|
}
|
|
|
|
};
|
2014-07-27 13:47:53 +02:00
|
|
|
addMatch(message.match(/spotify:track:([a-zA-Z-0-9]{22})/g));
|
|
|
|
addMatch(message.match(/open.spotify.com\/track\/([a-zA-Z-0-9]{22})/g));
|
|
|
|
return content;
|
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
|
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var youtubePlugin = new UrlPlugin('YouTube video', function(url) {
|
2015-02-08 11:23:32 +01:00
|
|
|
var regex = /(?:youtube.com|youtu.be)\/(?:v\/|embed\/|watch(?:\?v=|\/))?([a-zA-Z0-9-]+)/i,
|
|
|
|
match = url.match(regex);
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2015-02-08 11:23:32 +01:00
|
|
|
if (match){
|
2015-02-08 11:08:12 +01:00
|
|
|
var token = match[1],
|
2015-10-18 01:15:42 +02:00
|
|
|
embedurl = "https://www.youtube.com/embed/" + token + "?html5=1&iv_load_policy=3&modestbranding=1&rel=0&showinfo=0",
|
|
|
|
element = angular.element('<iframe></iframe>')
|
|
|
|
.attr('src', embedurl)
|
|
|
|
.attr('width', '560')
|
|
|
|
.attr('height', '315')
|
|
|
|
.attr('frameborder', '0')
|
|
|
|
.attr('allowfullscreen', 'true');
|
|
|
|
return element.prop('outerHTML');
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
2015-02-08 11:08:12 +01:00
|
|
|
});
|
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
|
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var dailymotionPlugin = new Plugin('Dailymotion video', function(message) {
|
2013-10-21 22:10:10 +02:00
|
|
|
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';
|
2015-10-18 01:15:42 +02:00
|
|
|
var element = angular.element('<iframe></iframe>')
|
|
|
|
.attr('src', embedurl)
|
|
|
|
.attr('width', '480')
|
|
|
|
.attr('height', '270')
|
|
|
|
.attr('frameborder', '0');
|
|
|
|
return element.prop('outerHTML');
|
2013-10-21 22:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2013-10-22 22:28:04 +02:00
|
|
|
/*
|
|
|
|
* AlloCine Embedded Player
|
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var allocinePlugin = new Plugin('AlloCine video', function(message) {
|
2013-10-22 22:28:04 +02:00
|
|
|
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;
|
2015-10-18 01:15:42 +02:00
|
|
|
var element = angular.element('<iframe></iframe>')
|
|
|
|
.attr('src', embedurl)
|
|
|
|
.attr('width', '480')
|
|
|
|
.attr('height', '270')
|
|
|
|
.attr('frameborder', '0');
|
|
|
|
return element.prop('outerHTML');
|
2013-10-22 22:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2013-10-17 22:11:31 +02:00
|
|
|
/*
|
|
|
|
* Image Preview
|
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var imagePlugin = new UrlPlugin('image', function(url) {
|
2015-02-08 22:09:01 +01:00
|
|
|
if (url.match(/\.(png|gif|jpg|jpeg)(:(small|medium|large))?\b/i)) {
|
2015-02-08 11:08:12 +01:00
|
|
|
/* 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:/, "");
|
2016-01-02 03:02:53 +01:00
|
|
|
} else if (url.match(/^https:\/\/www\.dropbox\.com\/s\/[a-z0-9]+\//i)) {
|
2015-02-08 11:08:12 +01:00
|
|
|
// Dropbox requires a get parameter, dl=1
|
2016-01-02 03:02:53 +01:00
|
|
|
var dbox_url = document.createElement("a");
|
|
|
|
dbox_url.href = url;
|
|
|
|
var base_url = dbox_url.protocol + '//' + dbox_url.host + dbox_url.pathname + '?';
|
|
|
|
var dbox_params = dbox_url.search.substring(1).split('&');
|
|
|
|
var dl_added = false;
|
|
|
|
for (var i = 0; i < dbox_params.length; i++) {
|
|
|
|
if (dbox_params[i].split('=')[0] === "dl") {
|
|
|
|
dbox_params[i] = "dl=1";
|
|
|
|
dl_added = true;
|
|
|
|
// we continue looking at the other parameters in case
|
|
|
|
// it's specified twice or something
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!dl_added) {
|
|
|
|
dbox_params.push("dl=1");
|
|
|
|
}
|
|
|
|
url = base_url + dbox_params.join('&');
|
2013-10-18 00:20:04 +02:00
|
|
|
}
|
2015-10-19 14:51:14 +02:00
|
|
|
return function() {
|
|
|
|
var element = this.getElement();
|
|
|
|
var imgElem = angular.element('<a></a>')
|
|
|
|
.attr('target', '_blank')
|
|
|
|
.attr('href', url)
|
|
|
|
.append(angular.element('<img>')
|
|
|
|
.addClass('embed')
|
|
|
|
.attr('src', url));
|
|
|
|
element.innerHTML = imgElem.prop('outerHTML');
|
|
|
|
};
|
2015-02-08 11:08:12 +01:00
|
|
|
}
|
|
|
|
});
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2016-02-19 17:31:10 +01:00
|
|
|
/*
|
|
|
|
* audio Preview
|
|
|
|
*/
|
|
|
|
var audioPlugin = new UrlPlugin('audio', function(url) {
|
|
|
|
if (url.match(/\.(mp3|ogg|wav)\b/i)) {
|
|
|
|
return function() {
|
|
|
|
var element = this.getElement();
|
|
|
|
var aelement = angular.element('<audio controls></audio>')
|
|
|
|
.addClass('embed')
|
|
|
|
.attr('width', '560')
|
|
|
|
.append(angular.element('<source></source>')
|
|
|
|
.attr('src', url));
|
|
|
|
element.innerHTML = aelement.prop('outerHTML');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-01-03 05:59:51 +01:00
|
|
|
/*
|
|
|
|
* mp4 video Preview
|
|
|
|
*/
|
|
|
|
var videoPlugin = new UrlPlugin('video', function(url) {
|
2015-11-13 22:38:32 +01:00
|
|
|
if (url.match(/\.(mp4|webm|ogv|gifv)\b/i)) {
|
|
|
|
if (url.match(/^http:\/\/(i\.)?imgur\.com\//i)) {
|
|
|
|
// remove protocol specification to load over https if used by g-b
|
|
|
|
url = url.replace(/\.(gifv)\b/i, ".webm");
|
|
|
|
}
|
2015-10-19 14:51:14 +02:00
|
|
|
return function() {
|
|
|
|
var element = this.getElement();
|
2015-11-13 22:38:32 +01:00
|
|
|
var velement = angular.element('<video autoplay loop muted></video>')
|
2015-10-19 14:51:14 +02:00
|
|
|
.addClass('embed')
|
|
|
|
.attr('width', '560')
|
|
|
|
.append(angular.element('<source></source>')
|
|
|
|
.attr('src', url));
|
|
|
|
element.innerHTML = velement.prop('outerHTML');
|
|
|
|
};
|
2015-01-03 05:59:51 +01:00
|
|
|
}
|
|
|
|
});
|
2015-11-13 22:38:32 +01:00
|
|
|
|
2015-01-03 05:59:51 +01:00
|
|
|
|
2013-10-18 02:14:39 +02:00
|
|
|
/*
|
|
|
|
* Cloud Music Embedded Players
|
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var cloudmusicPlugin = new UrlPlugin('cloud music', function(url) {
|
|
|
|
/* SoundCloud http://help.soundcloud.com/customer/portal/articles/247785-what-widgets-can-i-use-from-soundcloud- */
|
2015-10-18 01:15:42 +02:00
|
|
|
var element;
|
2015-02-08 11:08:12 +01:00
|
|
|
if (url.match(/^https?:\/\/soundcloud.com\//)) {
|
2015-10-18 01:15:42 +02:00
|
|
|
element = angular.element('<iframe></iframe>')
|
|
|
|
.attr('width', '100%')
|
|
|
|
.attr('height', '120')
|
|
|
|
.attr('scrolling', 'no')
|
|
|
|
.attr('frameborder', 'no')
|
|
|
|
.attr('src', 'https://w.soundcloud.com/player/?url=' + url + '&color=ff6600&auto_play=false&show_artwork=true');
|
|
|
|
return element.prop('outerHTML');
|
2015-02-08 11:08:12 +01:00
|
|
|
}
|
2013-10-18 02:14:39 +02:00
|
|
|
|
2015-02-08 11:08:12 +01:00
|
|
|
/* MixCloud */
|
|
|
|
if (url.match(/^https?:\/\/([a-z]+\.)?mixcloud.com\//)) {
|
2015-10-18 01:15:42 +02:00
|
|
|
element = angular.element('<iframe></iframe>')
|
|
|
|
.attr('width', '480')
|
|
|
|
.attr('height', '60')
|
|
|
|
.attr('frameborder', '0')
|
|
|
|
.attr('src', '//www.mixcloud.com/widget/iframe/?feed=' + url + '&mini=1&stylecolor=&hide_artwork=&embed_type=widget_standard&hide_tracklist=1&hide_cover=');
|
|
|
|
return element.prop('outerHTML');
|
2015-02-08 11:08:12 +01:00
|
|
|
}
|
|
|
|
});
|
2013-10-18 02:14:39 +02:00
|
|
|
|
2013-10-18 23:32:39 +02:00
|
|
|
/*
|
|
|
|
* Google Maps
|
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var googlemapPlugin = new UrlPlugin('Google Map', function(url) {
|
|
|
|
if (url.match(/^https?:\/\/maps\.google\./i) || url.match(/^https?:\/\/(?:[\w]+\.)?google\.[\w]+\/maps/i)) {
|
2015-10-18 01:15:42 +02:00
|
|
|
var element = angular.element('<iframe></iframe>')
|
|
|
|
.attr('width', '450')
|
|
|
|
.attr('height', '350')
|
|
|
|
.attr('frameborder', '0')
|
|
|
|
.attr('scrolling', 'no')
|
|
|
|
.attr('marginheight', '0')
|
|
|
|
.attr('src', url + '&output=embed');
|
|
|
|
return element.prop('outerHTML');
|
2015-02-08 11:08:12 +01:00
|
|
|
}
|
|
|
|
});
|
2013-10-18 23:32:39 +02:00
|
|
|
|
2014-02-23 04:16:32 +01:00
|
|
|
/*
|
|
|
|
* Asciinema plugin
|
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var asciinemaPlugin = new UrlPlugin('ascii cast', function(url) {
|
|
|
|
var regexp = /^https?:\/\/(?:www\.)?asciinema.org\/a\/(\d+)/i,
|
|
|
|
match = url.match(regexp);
|
2014-02-24 11:29:50 +01:00
|
|
|
if (match) {
|
2015-02-08 11:16:55 +01:00
|
|
|
var id = match[1];
|
|
|
|
return function() {
|
|
|
|
var element = this.getElement();
|
|
|
|
var scriptElem = document.createElement('script');
|
|
|
|
scriptElem.src = 'https://asciinema.org/a/' + id + '.js';
|
|
|
|
scriptElem.id = 'asciicast-' + id;
|
|
|
|
scriptElem.async = true;
|
|
|
|
element.appendChild(scriptElem);
|
|
|
|
};
|
2014-02-24 11:29:50 +01:00
|
|
|
}
|
2015-02-08 11:08:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var yrPlugin = new UrlPlugin('meteogram', function(url) {
|
|
|
|
var regexp = /^https?:\/\/(?:www\.)?yr\.no\/(place|stad|sted|sadji|paikka)\/(([^\s.;,(){}<>\/]+\/){3,})/;
|
|
|
|
var match = url.match(regexp);
|
|
|
|
if (match) {
|
2015-10-19 14:51:14 +02:00
|
|
|
return function() {
|
|
|
|
var element = this.getElement();
|
|
|
|
var language = match[1];
|
|
|
|
var location = match[2];
|
|
|
|
var city = match[match.length - 1].slice(0, -1);
|
|
|
|
url = "http://www.yr.no/" + language + "/" + location + "avansert_meteogram.png";
|
|
|
|
var ielement = angular.element('<img>')
|
|
|
|
.attr('src', url)
|
|
|
|
.attr('alt', 'Meteogram for ' + city);
|
|
|
|
element.innerHTML = ielement.prop('outerHTML');
|
|
|
|
};
|
2015-02-08 11:08:12 +01:00
|
|
|
}
|
|
|
|
});
|
2014-07-14 23:50:08 +02:00
|
|
|
|
2014-08-09 15:17:36 +02:00
|
|
|
// Embed GitHub gists
|
2015-02-08 11:08:12 +01:00
|
|
|
var gistPlugin = new UrlPlugin('Gist', function(url) {
|
2016-06-25 14:40:24 +02:00
|
|
|
// ignore trailing slashes and anchors
|
|
|
|
var regexp = /^(https:\/\/gist\.github.com\/(?:.*?))[\/]?(?:\#.*)?$/i;
|
2015-02-08 11:08:12 +01:00
|
|
|
var match = url.match(regexp);
|
|
|
|
if (match) {
|
|
|
|
// get the URL from the match to trim away pseudo file endings and request parameters
|
2016-06-25 14:40:24 +02:00
|
|
|
url = match[1] + '.json';
|
2015-02-08 11:08:12 +01:00
|
|
|
// load gist asynchronously -- return a function here
|
|
|
|
return function() {
|
|
|
|
var element = this.getElement();
|
|
|
|
jsonp(url, function(data) {
|
|
|
|
// Add the gist stylesheet only once
|
|
|
|
if (document.querySelectorAll('link[rel=stylesheet][href="' + data.stylesheet + '"]').length < 1) {
|
|
|
|
var stylesheet = '<link rel="stylesheet" href="' + data.stylesheet + '"></link>';
|
|
|
|
document.getElementsByTagName('head')[0].innerHTML += stylesheet;
|
|
|
|
}
|
|
|
|
element.innerHTML = '<div style="clear:both">' + data.div + '</div>';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-22 01:37:51 +02:00
|
|
|
var pastebinPlugin = new UrlPlugin('Pastebin', function(url) {
|
|
|
|
var regexp = /^http:\/\/pastebin.com\/([^.?]+)/i;
|
|
|
|
var match = url.match(regexp);
|
|
|
|
if (match) {
|
|
|
|
var id = match[1],
|
|
|
|
embedurl = "http://pastebin.com/embed_iframe/" + id,
|
|
|
|
element = angular.element('<iframe></iframe>')
|
|
|
|
.attr('src', embedurl)
|
|
|
|
.attr('width', '100%')
|
2016-04-22 02:13:27 +02:00
|
|
|
.attr('height', '480');
|
2016-04-22 01:37:51 +02:00
|
|
|
return element.prop('outerHTML');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-26 07:06:11 +02:00
|
|
|
/* match giphy links and display the assocaited gif images
|
|
|
|
* sample input: http://giphy.com/gifs/eyes-shocked-bird-feqkVgjJpYtjy
|
|
|
|
* sample output: https://media.giphy.com/media/feqkVgjJpYtjy/giphy.gif
|
|
|
|
*/
|
|
|
|
var giphyPlugin = new UrlPlugin('Giphy', function(url) {
|
2015-09-27 00:40:30 +02:00
|
|
|
var regex = /^https?:\/\/giphy.com\/gifs\/.*-(.*)\/?/i;
|
|
|
|
// on match, id will contain the entire url in [0] and the giphy id in [1]
|
|
|
|
var id = url.match(regex);
|
|
|
|
if (id) {
|
|
|
|
var src = "https://media.giphy.com/media/" + id[1] + "/giphy.gif";
|
2015-10-19 14:51:14 +02:00
|
|
|
return function() {
|
|
|
|
var element = this.getElement();
|
|
|
|
var gelement = angular.element('<a></a>')
|
|
|
|
.attr('target', '_blank')
|
|
|
|
.attr('href', url)
|
|
|
|
.append(angular.element('<img>')
|
|
|
|
.addClass('embed')
|
|
|
|
.attr('src', src));
|
|
|
|
element.innerHTML = gelement.prop('outerHTML');
|
|
|
|
};
|
2015-09-26 07:06:11 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-02-08 11:08:12 +01:00
|
|
|
var tweetPlugin = new UrlPlugin('Tweet', function(url) {
|
|
|
|
var regexp = /^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(?:es)?\/(\d+)/i;
|
|
|
|
var match = url.match(regexp);
|
|
|
|
if (match) {
|
|
|
|
url = 'https://api.twitter.com/1/statuses/oembed.json?id=' + match[2];
|
|
|
|
return function() {
|
|
|
|
var element = this.getElement();
|
|
|
|
jsonp(url, function(data) {
|
|
|
|
// separate the HTML into content and script tag
|
|
|
|
var scriptIndex = data.html.indexOf("<script ");
|
|
|
|
var content = data.html.substr(0, scriptIndex);
|
|
|
|
// Set DNT (Do Not Track)
|
|
|
|
content = content.replace("<blockquote class=\"twitter-tweet\">", "<blockquote class=\"twitter-tweet\" data-dnt=\"true\">");
|
|
|
|
element.innerHTML = content;
|
|
|
|
|
|
|
|
// The script tag needs to be generated manually or the browser won't load it
|
|
|
|
var scriptElem = document.createElement('script');
|
|
|
|
// Hardcoding the URL here, I don't suppose it's going to change anytime soon
|
|
|
|
scriptElem.src = "//platform.twitter.com/widgets.js";
|
|
|
|
element.appendChild(scriptElem);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
2014-08-08 18:09:23 +02:00
|
|
|
|
2015-01-31 01:13:17 +01:00
|
|
|
/*
|
|
|
|
* Vine plugin
|
|
|
|
*/
|
2015-02-08 11:08:12 +01:00
|
|
|
var vinePlugin = new UrlPlugin('Vine', function (url) {
|
|
|
|
var regexp = /^https?:\/\/(www\.)?vine.co\/v\/([a-zA-Z0-9]+)(\/.*)?/i,
|
|
|
|
match = url.match(regexp);
|
|
|
|
if (match) {
|
|
|
|
var id = match[2], embedurl = "https://vine.co/v/" + id + "/embed/simple?audio=1";
|
2015-10-18 01:15:42 +02:00
|
|
|
var element = angular.element('<iframe></iframe>')
|
|
|
|
.addClass('vine-embed')
|
|
|
|
.attr('src', embedurl)
|
|
|
|
.attr('width', '600')
|
|
|
|
.attr('height', '600')
|
|
|
|
.attr('frameborder', '0');
|
|
|
|
return element.prop('outerHTML') + '<script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>';
|
2015-02-08 11:08:12 +01:00
|
|
|
}
|
|
|
|
});
|
2015-01-31 01:13:17 +01:00
|
|
|
|
2013-10-08 23:06:47 +02:00
|
|
|
return {
|
2016-04-22 01:37:51 +02:00
|
|
|
plugins: [youtubePlugin, dailymotionPlugin, allocinePlugin, imagePlugin, videoPlugin, audioPlugin, spotifyPlugin, cloudmusicPlugin, googlemapPlugin, asciinemaPlugin, yrPlugin, gistPlugin, pastebinPlugin, giphyPlugin, tweetPlugin, vinePlugin]
|
2013-12-17 21:35:36 +01:00
|
|
|
};
|
2014-02-23 04:16:32 +01:00
|
|
|
|
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
});
|
2014-08-24 18:53:55 +02:00
|
|
|
})();
|