问题
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 ?
回答1:
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);
回答2:
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);
来源:https://stackoverflow.com/questions/22502868/dispatch-customevent-to-prefwindow-firefox-addon