Friday, February 6, 2009

boring (III) / unread email count

After the fun I had last time with JavaScript, I felt like coding another micro-project along the same idea: this time I wanted to display the unread emails count in the Reader page.

So I needed that special URL GMail uses to retrieve the count. So I sent myself several emails and all this time I used HttpFox to sniff everything that went in and out my browser; in the end I discovered a JavaScript object that held the information I was after.

Next, I needed to make an async request to the URL I found previously. A very forgiving version of XmlHttpRequest that allows one to make cross-site requests is the one GreaseMonkey provides.

Next, good old string parsing did the job of extracting the unread count from the response I got from GMail, so finally I just needed to append the count to the existing GMail link in Reader.

At this step unfortunately, more testing proved that the URL I've found worked for me only and not for other GMail accounts. I relied on the fact that account specific information would be sent using cookies, and I failed.

So I was back at finding the magic URL. My search went on through cookies, text/html and text/javascript content GMail responded with and I was lucky enough to track down the same js object sent back from another URL, this time a simple one with no scrambled GET parameters. A quick test for 3 GMail accounts on 2 different machines proved it working.

You can install the script from here.

No comments: