Created
June 20, 2010 03:03
-
-
Save tkosaka/445519 to your computer and use it in GitHub Desktop.
Google via the prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================ // | |
// Inside of the PRESERVE Area | |
// ============================================================ // | |
// | |
var googlePrompt = ( | |
function () { | |
var googleAPIUrl = "http://ajax.googleapis.com/ajax/services/search/"; | |
var page_index = null; | |
function decodeJSON(json) { | |
return util.safeEval("(" + json + ")"); | |
} | |
function search (query, page_index) { | |
if (!page_index) { | |
page_index= 0; | |
} | |
var url = googleAPIUrl + "web?v=1.0&rsz=large&start=" + page_index + "&q=" + encodeURIComponent(query); | |
var xhr = XMLHttpRequest(); | |
xhr.open("GET", url, false); | |
xhr.setRequestHeader("Content-type", "application/json"); | |
xhr.send(); | |
var obj = decodeJSON(xhr.responseText); | |
var results = obj["responseData"]["results"]; | |
var collection = []; | |
for (var i = 0; i < results.length; i++) { | |
var result = results[i]; | |
collection.push([result["titleNoFormatting"], result["unescapedUrl"]]); | |
} | |
prompt.selector( | |
{ | |
message: "Google Search Result " + "(p. " + page_index + ")", | |
collection: collection, | |
header: ["title", "url"], | |
width: [60, 40], | |
actions: [ | |
[function (aIndex) { | |
gBrowser.loadOneTab(collection[aIndex][1], null, null, null, false); | |
}, | |
"Open this url", | |
"open-this-url,c"], | |
[function (aIndex) { | |
command.setClipboardText(collection[aIndex][1]); | |
}, | |
"Copy this url", | |
"copy-this-url,c"], | |
[function (aIndex) { | |
prompt.finish(); | |
search(query, page_index + 8); | |
}, | |
"Next", | |
"search-next,c"] | |
], | |
keymap: { | |
"j": "search-next", | |
"y": "copy-this-url" | |
} | |
} | |
); | |
} | |
var self = { | |
do: function (ev, arg) { | |
prompt.read("Search:", | |
function (aReadStr, aUserArg) { | |
search(aReadStr); | |
} | |
, null, null, null, 0, null); | |
} | |
}; | |
return self; | |
} | |
)(); | |
ext.add("google-prompt", googlePrompt.do, | |
M({ja: 'Google Prompt.', | |
en: 'Google Prompt.'})); | |
// google-prompt ends here. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment