I want to implement the Google Custom Search API using Greasemonkey, and so far my trials have come up mostly with bugs. The purpose of the code is to introduce a custom search box into an existing site (I'm trying to do this for MATLAB documentation pages, but the injection code should really work with any site). I tried using many of the methods offered by web searches (mainly related to the implementation of jQuery or the Google API api in Greasemonkey) and no one worked for a custom api search ...
I think there might be some kind of problem with the variable scope, but please let me know if anyone has any suggestions for working with it.
var script = document.createElement('script');
script.src = 'http://www.google.com/jsapi?callback=gLoaded';
script.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(script);
var elmBody = document.getElementsByTagName('Body')[0];
var gSearch = document.createElement('div');
gSearch.id = 'g_search';
elmBody.appendChild(gSearch);
var w = unsafeWindow;
w.gLoaded= function()
{ w.google.load('search','1', {"callback" : searchLoaded}); } ;
searchLoaded = function(){
google = w.google;
alert(google);
alert(google.search);
alert(google.search.CurrentLocale);
var mySearch= new google.search.CustomSearchControl('012907575288767046387:tuvzi3yqdkq');
alert(mySearch)
mySearch.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
mySearch.draw('g_search');
}
- debug_1: returns a valid object
- debug_2:
- debug_3: ('en')
- debug_3: undefined
- , searchLoaded → w.searchLoaded (google = w.google), undefined.
, Javascript gLoaded() searchLoaded() -Greasemonkey (Window ) , . , .
- , , ...
google.search.CurrentLocale , google.search.CustomSearchControl()?
searchLoaded unsafeWindow.searchLoaded(. ), google , . , javascript, ! Greasemonkey - , , ?
(hack, @required, google.setOnLoadCallback...), .
, ... , ...
!