-
-
Save szupie/42986 to your computer and use it in GitHub Desktop.
Google search with keyword "in" for site search
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
CmdUtils.CreateCommand({ | |
name: "google", | |
icon: "http://www.google.com/favicon.ico", | |
searchURL: "http://www.google.com/search?hl=en&q=", | |
description: "Searches Google for your words.", | |
takes: {"keywords": noun_arb_text}, | |
modifiers: {"in": noun_arb_text }, | |
execute: function(keywords, modifiers) { | |
var query = this.buildQuery(keywords,modifiers); | |
Utils.openUrlInBrowser( this.searchURL + query ); | |
}, | |
preview: function(pblock, keywords, modifiers) { | |
var query = this.buildQuery(keywords,modifiers); | |
if(query.length < 1) { | |
pblock.innerHTML = this.description; | |
return; | |
} | |
var pTemplate = "Searches Google for <b>${query}</b>"; | |
var pData = {query: query }; | |
pblock.innerHTML = CmdUtils.renderTemplate(pTemplate, pData); | |
var url = "http://ajax.googleapis.com/ajax/services/search/web"; | |
var params = { v: "1.0", q: query }; | |
CmdUtils.previewGet( pblock, url, params, function(data) { | |
var numToDisplay = 3; | |
var results = data.responseData.results.splice( 0, numToDisplay ); | |
var format = '<div id="google-search">{for result in results}<div class="gresult"> <div> ${result.key}: <a href="${result.unescapedUrl}" accesskey="${result.key}">${result.title}</a> </div> <xul:description class="gresult-content">${result.content}</xul:description> <div class="gresult-url">${result.visibleUrl}</div></div>{forelse} <b>Your search - ${searchTerm} - did not match any documents. </b>{/for}<small>Tip: You can go to any result in this preview by using its number as an access key. (Linux and Windows: alt-number, Mac: control-number)</small></div>' | |
; | |
pblock.innerHTML = CmdUtils.renderTemplate( format, {results:results, searchTerm:query}); | |
}, "json"); | |
}, | |
buildQuery: function(keywords, modifiers) | |
{ | |
var site = modifiers.in.text; | |
if(site) | |
{ | |
if(site == "here") | |
{ | |
bw = CmdUtils.getWindowInsecure(); | |
site = bw.location.host; | |
} | |
return 'site:' + site + ' ' + keywords.text; | |
} | |
else { return keywords.text; } | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment