views:

32

answers:

2

I've written a sidebar addon:

  • which fetches the current page's URL
  • makes a call to a personal server with the URL
  • and displays contextual info in the sidebar

Currently, this function is invoked on following events:

  • on enabling the addon
  • on window load
  • on appcontent load
  • on TabSelect

However, there are times when a web page takes too long to fire the "load" event. Hence, what I'd prefer is a way to invoke my addon functionality as soon as the first request for the current page is sent.

I'm told that Progress Listener and Observer might be of help. I tried quick and dirty Progress-Listener code, but it didn't work as intended (perhaps, my fault).

Requesting suggestions/links/code-samples. Thanks in advance.

A: 

First thing that comes to mind is the 'DOMContentLoaded' event. That should be faster than 'load' I would think, but try it and see.

MatrixFrog
I'd used DOMContentLoaded, but that was again too late on slow pages. I'm currently using Observer service (have posted a dummy code below), and it seems to solve my problem.
Jumper
+1  A: 

As expected earlier, Observer was exactly what was needed. All I needed to do was listen to the topic "http-on-modify-request" and invoke the custom method (based on whatever custom check's required).

A sample code:

var MyAwesomeObserver = {
observe: function(subject, topic, data)
{
    if (topic == "http-on-modify-request") {
        MyLogger.info('Yosssssssss! '+topic); //customer Logger that uses nsIConsoleService
        MyAwesomeObject.notSoAwesomeFunction(); //invoke the custom method here
    }
},

get observerService() {
    return Components.classes["@mozilla.org/observer-service;1"]
    .getService(Components.interfaces.nsIObserverService);
},

register: function(){
    this.observerService.addObserver(this, "http-on-modify-request", false);
},

}
   MyAwesomeObserver.register();
Jumper