I'm trying to put together a userscript (for use in Chrome) and I keep hitting a wall. My first goal is to have Scott Reed's gSearch jQuery plugin working on any page via the script.
Here is the script I have from shoving snippets and examples, I find here and there, together:
The error I'm getting with this is "Uncaught TypeError: Cannot read property 'WebSearch' of undefined
".
// ==UserScript==
// @name Example
// @version 1.2
// @namespace
// @description
// @include *
// ==/UserScript==
function addScripts(callback) {
var script = document.createElement("script");
script.setAttribute("src", "http://google.com/jsapi");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
var script = document.createElement("script");
script.setAttribute("src", "http://gsearch.scottreeddesign.com/js/jquery.gSearch-1.0-min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
function LoadGoogle()
{
if(typeof google != 'undefined' && google && google.load)
{
google.load("search", "1");
}
else
{
// Retry later...
setTimeout(LoadGoogle, 30);
}
}
LoadGoogle();
function main() {
$("#div").css("border", ".5em solid black");
$('head').append('<link rel="stylesheet" href="http://gsearch.scottreeddesign.com/css/gSearch.css" type="text/css" />');
$('body').append('<div id="search-results"></div>');
$('#div').click(
function() {
$("#search-results").gSearch({search_text : 'example search'});
});
}
// load scripts and execute the main function
addScripts(main);