Wednesday, February 28, 2007

Dive into greasemonkey

I recently had to write a browser client tool that checks for advertisements on Yahoo! pages and displays detail information about the it. I first thought about writing a browser plugin to do the task. Then I came across Greasemonkey for Firefox and decided to give it a try.

They have done a great job is writing the tutorial documentation. I followed the instructions here and I got it up and running in a matter of minutes. But there are certain things I would like to point out about this tutorial.

1. The 'Better way to delay calling a function' example in section 2.3 actually does not work at all, even though it says that it should. The problem has to do with passing the callback function, helloworld, as a string. In this example, the helloworld function has no reference to it after the page is rendered. Therefore, it will be garbage collected. The correct way should be this:
        window.helloworld = function() {
alert('Hello world!');
}

window.setTimeout(helloworld, 60);
Passing in as object makes a reference to the function and keeps the function around by the time it is being called.

2. Section 2.4 on editing your user script is unclear. At first, I made trouble getting it to work and the part on associating you favorite text editor for .js files confused me. I associated js files to textpad on Windows and that did not appear to work. I then found out that I need to associate it in Firefox using the 'about:config' command.

I finally have everything ready to start my little project!!

0 comments: