Skip to content

Instantly share code, notes, and snippets.

@palfrey
Created June 25, 2009 10:45
Show Gist options
  • Select an option

  • Save palfrey/135784 to your computer and use it in GitHub Desktop.

Select an option

Save palfrey/135784 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
names: ["isgd"],
author: {name: "Thomas Brownback", homepage: "http://thomasbrownback.com", blog: "http://theorybloc.com"},
contributors: ["Tom Parker"],
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.",
arguments: [{role: "urlToShorten",
nountype: noun_arb_text,
label: "<i>url to shorten</i>"}],
/*
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, args) {
var longUrl = args.urlToShorten;
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(arguments ) {
var baseUrl = "http://is.gd/api.php";
var params = {longurl: arguments.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