Dispatch CustomEvent() to prefwindow - Firefox addon

帅比萌擦擦* 提交于 2019-12-25 06:31:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!