Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox onLocationChange not always called

I am building a firefox extension that creates several hidden browser elements.

I would like to addProgressListener() to handle onLocationChange for the page that I load. However, my handler does not always get called.

More specifically, here's what I'm doing:

  1. Create a browser element, without setting its src property
  2. Attach it to another element
  3. Add a progress listener listening for onLocationChange to the browser element
  4. Call loadURIWithFlags() with the desired url and post data

I expect the handler to be called every time after 4, but sometimes it does not (it seems to get stuck on the same pages though).

Interestingly, if I wrap 3 and 4 inside a setTimeout(..., 5000); it works every time.

I've also tried shuffling some of the steps around, but it did not have any effect.

The bigger picture: I would like to be reliably notified when browser's contentDocument is that of the newly loaded page (after redirects). Is there a better way to do this?

Update: I've since opened a bug on mozilla's bug tracker with a minimal xulrunner app displaying this behavior, in case anybody wants to take a closer look: https://bugzilla.mozilla.org/show_bug.cgi?id=941414

like image 637
Sergiu Toarca Avatar asked Nov 20 '13 21:11

Sergiu Toarca


1 Answers

In my experience developing with Firefox, I've found in some cases the initialization code for various elements acts as if it were asynchronous. In other words, when you're done executing

var newBrowser = window.document.createElement('browser');
newBrowser.setAttribute('flex', '1');
newBrowser.setAttribute('type', 'content');
cacheFrame.insertBefore(newBrowser, null);

, your browser may not actually be ready yet. When you add the delay, things have time to initialize, so they work fine. Additionally, when you do things like dynamically creating browser elements, you're likely doing something that very few have tried before. In other words, this sounds like a bug in Firefox, and probably one that will not get much attention.

You say you're using onLocationChange so that you can know when to add a load listener. I'm going to guess that you're adding the load listener to the contentDocument since you mentioned it. What you can do instead is add the load listener to the browser itself, much like you would with an iframe. If I replace

newBrowser.addProgressListener(listener);

with

newBrowser.addEventListener("load", function(e) {
  console.log('got here! ' + e.target.contentDocument.location.href);
}, false);

then I receive notifications for each browser.

like image 147
Jason Barnabe Avatar answered Sep 22 '22 13:09

Jason Barnabe