Created
February 18, 2015 16:31
-
-
Save vivien/50f329e36d404dae00ab to your computer and use it in GitHub Desktop.
Draft to hash URLs in Glowing Bear plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/js/plugins.js b/js/plugins.js | |
index b282d13..839df4c 100644 | |
--- a/js/plugins.js | |
+++ b/js/plugins.js | |
@@ -21,6 +21,50 @@ var Plugin = function(name, contentForMessage) { | |
}; | |
}; | |
+var parseURL = function(url) { | |
+ var regexp = /(https?):\/\/(?:www\.)?([^\/\?#]+)(\/[^\?#]+)?(#|\?)?(.*)/; | |
+ var match = url.match(regexp); | |
+ var obj = { | |
+ url: url, | |
+ protocol: match[1], | |
+ domain: match[2].split('.').reverse(), | |
+ path: [], | |
+ params: {}, | |
+ anchor: null | |
+ }; | |
+ | |
+ if (match[3] && match[3] != '/') { | |
+ obj.path = match[3].split('/'); | |
+ } | |
+ | |
+ if (match[5] && match[5].length > 0) { | |
+ if (match[4] == '#') { | |
+ obj.anchor = match[5]; | |
+ } else if (match[4] == '?') { | |
+ obj.params = match[5].split('&').reduce(function(h, p) { | |
+ var eq = p.indexOf('='); | |
+ var k = p.slice(0, eq); | |
+ var v = p.slice(eq + 1); | |
+ | |
+ h[k] = v; | |
+ | |
+ return h; | |
+ }, {}); | |
+ } | |
+ } | |
+ | |
+ return obj; | |
+}; | |
+ | |
+var underDomain = function(complete, portion) { | |
+ for (var i = 0; i < portion.length; i++) { | |
+ if (complete[i] != portion[i]) { | |
+ return false; | |
+ } | |
+ } | |
+ | |
+ return true; | |
+}; | |
// Regular expression that detects URLs for UrlPlugin | |
var urlRegexp = /(?:ftp|https?):\/\/\S*[^\s.;,(){}<>]/g; | |
@@ -33,11 +77,12 @@ var urlRegexp = /(?:ftp|https?):\/\/\S*[^\s.;,(){}<>]/g; | |
var UrlPlugin = function(name, urlCallback) { | |
return { | |
contentForMessage: function(message) { | |
- var urls = message.match(urlRegexp); | |
+ var urls = message.match(urlRegexp) || []; | |
var content = []; | |
- for (var i = 0; urls && i < urls.length; i++) { | |
- var result = urlCallback(urls[i]); | |
+ for (var i = 0; i < urls.length; i++) { | |
+ var url = parseURL(urls[i]); | |
+ var result = urlCallback(url); | |
if (result) { | |
content.push(result); | |
} | |
@@ -199,14 +244,24 @@ plugins.factory('userPlugins', function() { | |
* See: https://developers.google.com/youtube/player_parameters | |
*/ | |
var youtubePlugin = new UrlPlugin('YouTube video', function(url) { | |
- var regex = /(?:youtube.com|youtu.be)\/(?:v\/|embed\/|watch(?:\?v=|\/))?([a-zA-Z0-9-]+)/i, | |
- match = url.match(regex); | |
+ var token = null; | |
+ | |
+ if (underDomain(url.domain, ['com', 'youtube'])) { | |
+ if (url.path[0] == 'watch') { | |
+ token = url.params.v; | |
+ } else if (url.path[0] == 'embed') { | |
+ token = url.path[1]; | |
+ } | |
+ } else if (underDomain(url.domain, ['be', 'youtu'])) { | |
+ token = url.path[0]; | |
+ } | |
- if (match){ | |
- var token = match[1], | |
- embedurl = "https://www.youtube.com/embed/" + token + "?html5=1&iv_load_policy=3&modestbranding=1&rel=0&showinfo=0"; | |
+ if (token) { | |
+ 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>'; | |
} | |
+ | |
+ return null; | |
}); | |
/* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment