Tatva-Artha

meaning of "it"

Writing your Greasemonkey userscripts in jQuery

without comments

I’ve played with greasemonkey and like them in concept. They give users control of the webpage independent of the website where they are served from. I’ve used it to mostly replace default maps with google maps and to disable advertisement sections. Since, I am gaining more affinity to jQuery, it would be nice to write all my jQuery userscripts in jQuery. For this, we need to incorporate jQuery into userscript before doing custom stuff.

Here is a jquery userscript that I found simple to integrate:

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();
 
function letsJQuery() {
// All your custom GM code must be inside this function
}

I am not sure if we can put this in a separate JS file and import this in all our customer userscripts. But it is small enough to be copy/pasted (yuck! but necessary) into each file.

As an example, I visit NationalMortgageProfessional.com website often for mortgage related news these days. But I hate the flashing ads on the right bar. All I needed is to hide this bar. Of course, you can do this in 2 lines in javascript, but jQuery is so much better. Here is how I did it:

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();
 
function letsJQuery() {
  // hide the right sidebar with flashing ads
 $('#sidebar-right').hide();
}

And there I went.
Happy Reading!

http://www.tatvartha.com/wp-content/plugins/sociofluid/images/digg_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/reddit_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/stumbleupon_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/delicious_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/google_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/twitter_16.png

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Written by Sharad

February 3rd, 2010 at 5:14 am

Posted in All

Leave a Reply