Created
November 10, 2009 12:30
-
-
Save ba3r/230861 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* longurl v0.81 */ | |
CmdUtils.CreateCommand({ | |
names: ["expand-short-url", "longurl.org"], | |
icon: "data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAFvSURBVDhPjZPLSsNAFIb/OZNUrLSKl4VbcaEgqOB14wP4DD6C6CuIax/AV/AxdCFeEBeCbyBeUAppoWlME8+ZZtLUJmnJYpJh/nP+/zsT9dMM4qPLJ3x6AeIICKMYYTdKVn4337Lynllj+L9d1Ct1vF5sQr/MHp69NTpQ/AyKpQgXHBBLEaDTmsLi8jXe3SuQdNaqRCxu0iJAu1nF6sYtDvaf8eX5oGHbmc42irHfF+9uP6LTrsGlCqjQ9ggxKQ1NmgukwMbvbMWOIi4QW+oZYEK+wLYVc28odkFhKIfzaQuwlfU72MxZMbF9bRwMzXkQ2N7OvQGWJ9bKEQZJhJxRSWe/UMz2SWUg5mQWsZPQlszWtpbsRBxBGJQA6x3krAXifoF/l6Tcdq+zEQtE+THs9bSZR9m2YsW/AM1NzmBp7caMSjqPZZs7R3x/FqrzUB+eF58/nOK7FaBCbgrKjI0PmlEx7axtxfvTEzWcbB3jD1bNgOd9VMEgAAAAAElFTkSuQmCC", | |
homepage: "http://longurl.org/tools", | |
author: {name: "Sean Murphy", homepage: "http://IamSeanMurphy.com"}, | |
contributors: {name: "Michael Baer", homepage: "http://twitter.com/synapsos"}, | |
license: "GPL", | |
help: "Select a shortened URL and type longurl to display the destination in the preview. Press Enter to replace your selected URL with the long URL.", | |
arguments: [{role: 'object', nountype: noun_arb_text, label: "short url"}], | |
preview: function(previewBlock, args) { | |
previewBlock.innerHTML = "Expand a URL shortened with a service like Bit.ly"; | |
if (args.object.text.match(/http:\/\/([^\/]+)\/.+/i)) { | |
this._expand(previewBlock, args); | |
} | |
}, | |
execute: function(args) { | |
var short_url = args.object.text.replace(/\s/g, ""); | |
if(short_url.length < 1) { | |
displayMessage("No shortened URL was specified."); | |
return; | |
} | |
if (globals.longurl.long_url) { | |
var replacement = args.object.html.replace(short_url, globals.longurl.long_url, "ig"); | |
CmdUtils.setSelection(replacement); | |
} | |
}, | |
_expand: function(previewBlock, args) { | |
jQuery.ajax({ | |
type: 'GET', | |
url: 'http://api.longurl.org/v1/expand', | |
data: {format: 'json', url: args.object.text}, | |
dataType: 'json', | |
beforeSend: function(xhrObject) { | |
xhrObject.setRequestHeader('User-Agent', 'expand-short-url (Ubiquity)'); | |
}, | |
error: function(xhrObject, responseText) { | |
displayMessage('Error contacting LongURL service. Try again later.'); | |
}, | |
success: function(data, responseText) { | |
if (typeof(data.messages) !== 'undefined') { // There was an error | |
previewBlock.innerHTML = 'LongURL Error: ' + data.messages[0].message; | |
} else { | |
globals.longurl = {'long_url': data.long_url}; | |
globals.title = {'long_title': data.title}; | |
var long_domain = globals.longurl.long_url; | |
long_domain = long_domain.replace(/http:\/\//, ''); | |
long_domain = long_domain.replace(/www\./, ''); | |
long_domain = long_domain.replace(/\/\S*/, ''); | |
long_domain = long_domain.replace(/\s/g, ''); | |
var long_title = globals.title.long_title; | |
var long_icon = 'http://www.google.com/s2/favicons?domain=' + long_domain ; | |
var short_query = args.object.text; | |
short_query = short_query.replace(/\s/g, ''); | |
short_query = short_query.replace(/\:/g, '%3A'); | |
short_query = short_query.replace(/\//g, '%2F'); | |
var long_details = 'http://longurl.org/expand?url=' + short_query; | |
var previewTemplate = "<strong>${short_url}</strong> resolves to <br><br><strong>${long_url}</strong><br><br><img src='${long_icon}'</> <a href='${long_url}'>${long_title}</a><br><br><a href='${long_details}'>Details</a><br>"; | |
var previewData = { | |
short_url: args.object.text, | |
long_url: data.long_url, | |
long_icon: long_icon, | |
long_details: long_details, | |
long_title: long_title | |
}; | |
previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData); | |
} | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment