Monday, September 10, 2007

Discovering the Coolness of nsIWebProgressListener

I am writing an extension and I have been jumping through some painful hoops with the "load" and "unload" events and "onDocumentLoad" and I have been trying to figure out how this is supposed to behave....

Then I discovered nsIWebProgressListener. What a beautiful interface!

I had thought that, in all my questions and grumblings and gripes, that someone would have pointed me to this. Someone probably did and I probably did not hear. But then, I notice that I will be drowning in a pool and I will say "Help!" and someone will say "you probably need an instance of a nsINylonLinearExtractionDevice...". No, actually, I need to not drown! Or I need a rope. And if I decode the answer, I can see that, yes, I was told I should grab a rope. How could I have missed it? :-)

It also makes it challenging that when you do a search (in, you are as likely to find a document from 1999 as any other. Yes, people have written new documents on the new ways of doing things. How often do the old documents get removed from wiki.m.o, or www.m.o? From the evidence, I would guess it does not happen very often.

There needs to be many more snippets on MDC that make use of this interface. I look forward to posting some. I am recalling many newbie questions on the newsgroups where the appropriate response would have been that one needs to look at all the wonderful things this interface gives you.

Of course, I always have a suggestion for future improvement. It's just something I have to do.

I wanted to keep track of some resources, one for each open window or tab. The interface, through the onStateChange and onLocationChange, gives me almost everything I need. Except that when I close a window, or close a tab, I get no message? I could just keep my stuff around in case a notification ever comes in on that window. But if that window was closed, wouldn't that be a leak in my extension?

I am sure there are other, older events I could look for, but it seems that nsIWebProgressListener was written to provide a better interface. It just needs to do the whole job.

See: -
nsIWebProgressListener can send message when window or tab is closed


jto said...

"How often do the old documents get removed from wiki.m.o, or www.m.o? From the evidence, I would guess it does not happen very often."

You can help.

Mark Finkle said...

nsIWebProgressListener should _not_ notify on window of tab close. That is not the purpose of the interface. Its a slippery slope that leads to adding other notifications as well. nsIWebProgressListener does its job well enough as it is.

Use "close" or "unload" events for window. Use "TabNew", "TabOpen" or
"TabClose" events for tabs.

See here for code examples:

Feel free to add more