Skip to content

Instantly share code, notes, and snippets.

@organisciak
Created February 26, 2009 20:46
Show Gist options
  • Save organisciak/71096 to your computer and use it in GitHub Desktop.
Save organisciak/71096 to your computer and use it in GitHub Desktop.
// Note, parts of this code are based on the default ubiquity commands
function getJSONArray(url, callback) {
jQuery.getJSON(url,
function (data) {
callback(data);
});
}
function getCSVArray(url, callback) {
Utils.ajaxGet(url, function(data) {
data = data.replace(/\n/gi, ",");
data = data.split(",");
callback(data);
}, function() {
callback({});
});
}
//function for defining a noun from an array retrieved by a GET function. Current supported type are 'csv' and 'json'
function getNoun(arrayURL, type) {
var noun_type_GET = {
_name: "GET results",
options: null,
callback:function(returned) {
noun_type_GET.options = returned;
},
suggest: function(text, html) {
if (noun_type_GET.options == null) {
if (type=='csv') {
getCSVArray(arrayURL, noun_type_GET.callback);
} else if (type=='json') {
getJSONArray(arrayURL, noun_type_GET.callback);
}
return [];
}
var suggestions = [];
for ( var i in noun_type_GET.options ) {
if (noun_type_GET.options[i].match(text, "i")) {
suggestions.push(CmdUtils.makeSugg(noun_type_GET.options[i]));
}
}
return suggestions;
}
};
return noun_type_GET;
}
var commandText = {
help: "Start typing your search and the result will be suggested. Pressing enter will paste it into your page.",
preview: {
empty:"Start typing your search and the result will be suggested.",
suggested:["Press ENTER to paste <strong>'","'</strong> into your page."]
}
};
CmdUtils.makeListableCommand = function makeListable(options) {
options.icon = "http://www.listable.org/favicon.ico";
options.help = commandText.help;
options.takes = {"input": getNoun("http://www.listable.org/json/" + options.name, "json")};
options.preview = function( pblock, input ) {
pblock.innerHTML = commandText.preview.empty;
if (input.text.length > 0)
pblock.innerHTML = commandText.preview.suggested[0]+input.text+commandText.preview.suggested[1];
};
options.execute = function(input) {
CmdUtils.setSelection( input.text );
};
options.name = options.name.toLowerCase();
CmdUtils.CreateCommand(options);
};
CmdUtils.makeCSVCommand = function makeListable(options) {
options.help = commandText.help;
options.takes = {"input": getNoun(options.csvUrl, "csv")};
options.preview = function( pblock, input ) {
pblock.innerHTML = commandText.preview.empty;
if (input.text.length > 0)
pblock.innerHTML = commandText.preview.suggested[0]+input.text+commandText.preview.suggested[1];
};
options.execute = function(input) {
CmdUtils.setSelection( input.text );
};
options.name = options.name.toLowerCase();
CmdUtils.CreateCommand(options);
};
CmdUtils.makeCSVCommand({
name: "fast-food-restaurants",
icon : "http://www.google.com/favicon.ico",
csvUrl : "http://spreadsheets.google.com/pub?key=pFSeqrAljsWYeC31S9nilIg&output=csv&gid=0",
homepage : "http://www.porganized.com/category/ubiquity",
author : { name: "Peter Organisciak", email: "organisciak@gmail.com"},
license : "MPL"
});
CmdUtils.makeListableCommand({
name: "countries", //this should be the end of the page url. i.e. if the list is at "http://www.listable.org/show/area-codecountrystatecity", you should name the command "area-codecountrystatecity"
homepage : "http://www.porganized.com/category/ubiquity",
author : { name: "Peter Organisciak", email: "organisciak@gmail.com"},
license : "MPL"
});
CmdUtils.makeListableCommand({
name: "canadian-prime-ministers",
homepage : "http://www.porganized.com/category/ubiquity",
description : "Search a list of Canadian Prime Ministers from Listable.org",
author : { name: "Peter Organisciak", email: "organisciak@gmail.com"},
license : "MPL"
});
CmdUtils.makeListableCommand({
name: "most-commonly-used-passwords",
homepage : "http://www.porganized.com/category/ubiquity",
author : { name: "Peter Organisciak", email: "organisciak@gmail.com"},
license : "MPL"
});
CmdUtils.makeListableCommand({
name: "us-state-names",
homepage : "http://www.porganized.com/category/ubiquity",
author : { name: "Peter Organisciak", email: "organisciak@gmail.com"},
license : "MPL"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment