views:

737

answers:

2

I was fixing up an extension that was written for Firefox 2 to work with Firefox 3. One of the changes was to use:

window.getBrowser().addEventListener("load", checkURL, true);

instead of going through just window.

The only problem is that the "load" event seems to occur more than once per page load. I have also tried listening for "DOMContentLoaded" but with similar results.

Is there a way to make sure to execute the script only once per page load? If it matters, the pages I am loading are forum pages from forums.somethingawful.com .

A: 

Try:

window.getBrowser().addProgressListener(
  checkURL,
  Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT
);

and checkLink needs to be an object with onStateChange function with this signature:

onStateChange: function(aProgress, request, stateFlags, status);

Check out the docs on nsiWebProgress.

apphacker
+1  A: 

Well, it turns out that for one, load events can be triggered by loading images like the favicon. So one must make sure that the target is indeed the document:

aEvent.originalTarget.nodeName == "#document"

More info can be obtained on the snippets page: https://developer.mozilla.org/En/Code_snippets/On_page_load

You may also want to check the condition `aEvent.originalTarget.documentURI == "about:blank"` and do nothing if it is true (user opened a new tab and it's empty)
MatrixFrog