Tuesday, July 21, 2009

jQuery & Greasemonkey

This is a simple snippet that helps us load the jQuery power into our userscripts with Greasemonkey:
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// Check if jQuery's loaded
function GM_wait() {
if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();

// All your GM code must be inside this function
function letsJQuery() {
alert($); // check if the dollar (jquery) function works
}

Bookmark and Share

0 comments:

Post a Comment