Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Customized Google Search Commands for Ubiquity
// A collection of customized Google search commands derived from default Ubiquity one
// Currently trere are:
// - Search for a quoted string
// - Google site search
// (C) 2011 g/christensen (gchristnsn@gmail.com)
var googleSearchTemplate =
'{if results.length}\n'
+ '{for result in results}\n'
+ '<div class="gresult">\n'
+ ' <div>\n'
+ ' ${result.key}: <a href="${result.unescapedUrl}" accesskey="${result.key}">${result.title}</a>\n'
+ ' </div>\n'
+ ' <div class="gresult-content">${result.content}</div>\n'
+ ' <div class="gresult-url">${result.visibleUrl}</div>\n'
+ '</div>\n'
+ '{/for}\n'
+ '<small>${tipsMessage}</small>\n'
+ '{else}\n'
+ '${noResultsMessage}\n'
+ '{/if}';
CmdUtils.makeSearchCommand({
names: ["google:quote"],
url: "http://www.google.com/search?q=\"{QUERY}\"",
icon: "chrome://ubiquity/skin/icons/google.ico",
description: "Searches Google for your words quoted.",
help: "Search for a quoted string. You can use the keyboard shortcut ctrl + alt + number to open one " +
"of the Google results shown in the preview.",
preview: function google_preview(pblock, {object}) {
var searchTerm = object.text;
// Don't even display any text before fetching search results,
// since the results come back nearly instantaneously. In the
// future, we can display a throbber.
if (!searchTerm) return void this.previewDefault(pblock);
var url = "http://ajax.googleapis.com/ajax/services/search/web";
var params = {v: "1.0", q: "\"" + searchTerm + "\"", rsz: "small"};
CmdUtils.previewGet(pblock, url, params, function google_get(data) {
var {results} = data.responseData;
results.forEach(function (r, i) { r.key = i + 1 });
var noResultsMessage = _(
"Your search - ${searchTerm} - did not match any documents.",
{searchTerm: object.html.bold()});
var tipsMessage = _(
"Tip: You can go to any result in this preview by pressing " +
"control, alt, and the result number at the same time.");
pblock.innerHTML = CmdUtils.renderTemplate(
googleSearchTemplate,
{ results: results,
noResultsMessage: noResultsMessage,
tipsMessage: tipsMessage });
}, "json");
params.q = searchTerm;
}
});
CmdUtils.makeSearchCommand({
names: ["google:site"],
url: "http://www.google.com/search?q={QUERY}",
icon: "chrome://ubiquity/skin/icons/google.ico",
description: "Searches Google for your words quoted.",
help: "Google site search. You can use the keyboard shortcut ctrl + alt + number to open one " +
"of the Google results shown in the preview.",
preview: function google_preview(pblock, {object}) {
var searchTerm = object.text;
var doc = CmdUtils.getDocument();
var location = doc.location;
searchTerm += " site:" + location.hostname;
this._searchTerm = searchTerm
// Don't even display any text before fetching search results,
// since the results come back nearly instantaneously. In the
// future, we can display a throbber.
if (!searchTerm) return void this.previewDefault(pblock);
var url = "http://ajax.googleapis.com/ajax/services/search/web";
var params = {v: "1.0", q: searchTerm, rsz: "small"};
CmdUtils.previewGet(pblock, url, params, function google_get(data) {
var {results} = data.responseData;
results.forEach(function (r, i) { r.key = i + 1 });
var noResultsMessage = _(
"Your search - ${searchTerm} - did not match any documents.",
{searchTerm: object.html.bold()});
var tipsMessage = _(
"Tip: You can go to any result in this preview by pressing " +
"control, alt, and the result number at the same time.");
pblock.innerHTML = CmdUtils.renderTemplate(
googleSearchTemplate,
{ results: results,
noResultsMessage: noResultsMessage,
tipsMessage: tipsMessage });
}, "json");
},
execute: function () {
if (!this._searchTerm && "defaultUrl" in this)
Utils.openUrlInBrowser(this.defaultUrl);
else
Utils.openUrlInBrowser(
CmdUtils.makeSearchCommand.query(this.url, this._searchTerm, this.charset),
CmdUtils.makeSearchCommand.query(this.postData, this._searchTerm, this.charset));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.