Skip to content

Instantly share code, notes, and snippets.

@isacssouza
Created June 3, 2009 16:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save isacssouza/123073 to your computer and use it in GitHub Desktop.
A ubiquity command that searchs google with the prefix '"define:"
CmdUtils.makeSearchCommand({
names: ["gdefine"],
url: "http://www.google.com/search?defl=en&q=define:{QUERY}",
icon: "http://www.google.com/favicon.ico",
description: "Searches Google for your words using the 'define' command.",
author: {name: "Isac Sacchi e Souza", email: "isacssouza@gmail.com"},
license: "Public domain",
arguments: [{role: "object", nountype: noun_arb_text, label: "The word to define."}],
preview: function(pblock, args) {
var searchTerm = args.object.text; // The word to define
if(searchTerm.length < 1) {
// Set the default text to show when there is nothing to define
pblock.innerHTML = "Searches Google for your words using the 'define:' operator.";
return;
}
var defineSearch = "define"+encodeURIComponent(':')+searchTerm;
/*
* I set the language to English so there are more and better results.
* Maybe I should use the browser language or something else to set the
* user language.
*/
var url = "http://www.google.com/search?defl=en&q="+defineSearch;
// Get the results, parse and show.
CmdUtils.previewGet( pblock, url, null, function(data) {
var resultDivPos = data.indexOf("ssb", 0); // ssb is the class of the results list.
var beginPos = data.indexOf("<ul", resultDivPos); // get the begin index of the list.
var endPos = data.indexOf("</ul>", beginPos); // get the end index of the list.
// Display the results as an HTML list.
pblock.innerHTML = "<p>Definitions of <b>"+searchTerm+"</b> on the Web:</p>"+data.substring(beginPos, endPos);
});
}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment