Create a gist now

Instantly share code, notes, and snippets.

CmdUtils.CreateCommand({
name: "isgd",
author: {name: "Thomas Brownback", homepage: "http://thomasbrownback.com", blog: "http://theorybloc.com"},
thanks: {first: "Aza Raskin", foremost: "Aza Raskin", honorableMention: "geero.net", lastAmpBangLeast: "users like you"},
license: "PUBLIC DOMAIN: Any and all rights herein are hereby donated to the commons.",
description: "Shortens a URL using is.gd.",
help: "Replaces a selection with (or simply inserts) a shortened URL from is.gd.",
takes: {"<i>url to shorten</i>": noun_arb_text},
/*
TODO
1) I'd really like to just call one function in both the preview and the execution, but haven't figured out how yet while still maintaining dynamic AJAXy previews. Something like this, except with setSelection instead of displayMessage:
_shortenUrl: function( urlToShorten ) {
var baseUrl = "http://is.gd/api.php";
var params = {longurl: urlToShorten.text};
var shortUrl = jQuery.get( baseUrl, params, function ( isgdResponse ) {
displayMessage(isgdResponse);
})
},
2) Give an option to copy to clipboard instead of replacement/insertion.
I think geero.net had some ideas on that.
*/
preview: function(pBlock, longUrl) {
if (!longUrl.text) {
pBlock.innerHTML = '<b>Shortens a URL using is.gd.</b><br><i>Upon execution, isgd replaces the selection with a shortened URL, or if nothing is selected, simply inserts the shortened URL at the cursor.</i>';
} else {
var previewTemplate = "Hang on while I ask is.gd about <i>${query}</i>.";
var previewData = {query: longUrl.text};
pBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
};
var params = { longurl: longUrl.text};
var isgdURL = 'http://is.gd/api.php?' + jQuery.param( params );
jQuery.ajax({
type: "GET",
url: isgdURL,
success: function(searchResponse) {
pBlock.innerHTML = "<i>" + longUrl.text + "</i> shortens to:<br>" + searchResponse;
}
});
},
execute: function( urlToShorten ) {
var baseUrl = "http://is.gd/api.php";
var params = {longurl: urlToShorten.text};
var shortUrl = jQuery.get( baseUrl, params, function ( isgdResponse ) {
//displayMessage("replacing with " + isgdResponse); //this is probably too noisy
CmdUtils.setSelection( isgdResponse);
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment