Created
October 5, 2010 05:21
-
-
Save jjdelc/611040 to your computer and use it in GitHub Desktop.
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
CmdUtils.CreateCommand({ | |
names: ["Goo.gl", "shorten"], | |
description: "Shorten a URL using Google's shortener", | |
help: "Just use the url as the parameter", | |
author: { | |
name: "Jj", | |
email: "jjdelc@gmail.com", | |
homepage: "http://isgeek.net/", | |
}, | |
license: "GPL", | |
homepage: "http://isgeek.net/", | |
icon: "http://www.google.com/favicon.ico", | |
arguments: [{role: "object", nountype: noun_arb_text, label: "text"}], | |
execute: function execute(args) { | |
var self = this; | |
this._shorten(args.object.text, function(url){ | |
Utils.clipboard.text = url; | |
displayMessage("Copied to clipboard:\n " + url, self); | |
return url; | |
}); | |
}, | |
preview: function preview(pblock, args) { | |
pblock.innerHTML = "Generates Goo.gl short url... <i>generating</i>"; | |
this._shorten(args.object.text, function(url){ | |
pblock.innerHTML = "Replaces " + args.object.html.bold() + " with <b>" + url + "</b>"; | |
}); | |
}, | |
_get_endpoint: function(long_url) { | |
return 'http://goo.gl/api/shorten?url=' + encodeURIComponent(long_url); | |
}, | |
_shorten: function(long_url, callback) { | |
// Code saken from Goo.gl lite Firefox extension | |
// https://addons.mozilla.org/en-US/firefox/addon/55308/ | |
var req = new XMLHttpRequest(); | |
req.addEventListener("load", function(){ | |
var response = JSON.parse(req.responseText); | |
if (response.error_message){ | |
return callback("An error occured: " + response.error_message); | |
} | |
return callback(response.short_url); | |
}, false); | |
req.addEventListener('error', function(){ | |
return callback("An error occured:" + res.status); | |
}, false); | |
req.open("POST", this._get_endpoint(long_url)); | |
req.setRequestHeader("X-Auth-Google-Url-Shortener", "true"); | |
req.send(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment