views:

524

answers:

2

I want to read the contents of Gmail messages and add some fancyness on links. Here's some code:

unsafeWindow.gmonkey.load("1.0", function(gmail){
  gmail.registerViewChangeCallback(function(){
    if (gmail.getActiveViewType && gmail.getActiveViewType() == "cv") {
      var viewElement = gmail.getActiveViewElement()
      // Do things with viewElement
    }
  })
})

The actual detection of links in the dom objects for the mails is the easy part. The problem is that the registerViewChangeCallback only runs when you display a thread. Large threads will have most of it's messages hidden, only to be loaded by a users request. I haven't found a Gmail greasemonkey API method for this particular action (loading a individual message), which is when I need to run my script.

Any suggestions?

A: 

I think you'll find that some messages are loaded when they're listed in the thread; hence your problem.

Why don't you just use a custom style anyway? UserStyles FF plugin.

Noon Silk
I just updated my question for more clarity; that's exactly what I have discovered and what I'm trying to solve.Is "a custom style" referring to another FF extension, as an alternative to greasemonkey? How would this extension solve the problem?
August Lilleaas
http://userstyles.org/I thought you wanted to change the look of gmail in some way; I'd use 'Stylish' for that.
Noon Silk
It's not for looks, really, it's about adding behaviour to some links.
August Lilleaas
Oh; okay. Sorry then. Can't help.
Noon Silk
Seems like you're not the only one ; ) Thanks anyway!
August Lilleaas
+1  A: 

As you say, the registerViewChangeCallback() function only fires when the user changes their view from e.g. threads to archives, etc.

What you really need is to add a function that intercepts gmail's post-backs and then changes the links. I have never tried doing it myself, but this answer has some sample code for you. When gmail has retrieved a new message, it will fire a readystatechange event, which your code can intercept. You can then change the contents of the message in whichever way you wish (although you may have to wait for a moment to allow gmail to insert the message first - not sure about that one).

a_m0d
I tried to trace function calls in firebug so that I could monkey patch into those, but wasn't able to because of the thousands of function calls Gmail performs.Figuring out which AJAX call to took into should be much easier, though, I'll investigate the answer you linked to and see what I can find out.
August Lilleaas