Question

Working with a Firefox addon, I wish to send a CustomEvent() to a preference window.

I open the preference window using an openDialog(), and keep a reference to the opened window. After that, I try to dispatch the event, but the event is never received.

var pWin = window.openDialg("chrome://myextension/path/options.xul", "name", features); 
var event = new pWin.CustomEvent("prefwindow-event"); 
pWin.dispatchEvent(event);

In the prefwindow scope, I have this code in the XUL attached script :

window.addEventListener("prefwindow-event", this.catchEvent, false); 

However, I never receive that event. The documentation for CustomEvent() says

When creating a CustomEvent object, you must create the object from the same window as you're going to fire against.

So does it mean that, I can never dispatch an event from my main extension scope to that of another window ? If this is indeed possible, what am I doing wrong here ? If not, is there an alternative ?

Was it helpful?

Solution

I guess the following will work

var pWin = window.openDialg("chrome://myextension/path/options.xul", "name", features); 
pWin.addEventListener("load", function(){
  var event = new pWin.CustomEvent("prefwindow-event"); 
  pWin.dispatchEvent(event);
}, false);

OTHER TIPS

Paa's code should work.

var pWin = window.openDialg("chrome://myextension/path/options.xul", "name", features); 
pWin.addEventListener("prefwindow-event", function(){pWin.alert('prefwindow-event fired')}, false);
pWin.addEventListener("load", function(){
  var event = new pWin.CustomEvent("prefwindow-event"); 
  pWin.dispatchEvent(event);
}, false);

if not then try this:

var pWin = window.openDialg("chrome://myextension/path/options.xul", "name", features); 
pWin.addEventListener("load", function(){
  pWin.addEventListener("prefwindow-event", function(){pWin.alert('prefwindow-event fired')}, false);
  var event = new pWin.CustomEvent("prefwindow-event"); 
  pWin.dispatchEvent(event);
}, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top