Question

For many reasons, I have to open up XUL in a tab, instead of using a standard window. I wish to send custom events to this tab, and here's how my code looks like :

myextension.js :

  .. 
  var pTab = window.gBrowser.loadOneTab("chrome://myextension/path/options.xul", 
                  {inBackground: false});
  var pWin = window; 
  var event = new pWin.CustomEvent("prefwindow-event");
  pWin.dispatchEvent(event);
  ..

options.xul code:

 window.addEventListener("load", init, false); 
 window.addEventListener("prefwindow-event", myevent, false, true);
 .. 
 myevent: function(e) {
      dump("My event : " + e.details ); 
 },
 ..

However, I don't get the event. I have tried all possible options. Enabled/Disabled useCapture and wantsUntrusted of addEventListener(). After realizing that there are restrictions in sending custom events between windows, I also tried dispatching event with the tab element, like this :

pTab.dispatchEvent(event);

That wouldn't work either. 1) The event dispatch would work perfectly fine if I use a dialog window instead of a tab (openDialog instead of loadOneTab). 2) The tab element only inherits dispatchEvent and not a CustomEvent

So the question is, is there a way to send custom events and have it received in a tab?

Was it helpful?

Solution

var event = new pTab.linkedBrowser._contentWindow.CustomEvent("prefwindow-event");

edit:

Proof of concept, open scratchpad, switch environment to browser, run the following snippet

var url = 'data:text/html;charset=utf-8,';
var content = '<html><script>addEventListener("Hello",function(){alert("Hello")},false)</script></html>'
var tab  = gBrowser.loadOneTab(url+encodeURIComponent(content), {inBackground: false});

tab.addEventListener("load", function(){
  var evt = new tab.linkedBrowser.contentWindow.CustomEvent("Hello");
  tab.linkedBrowser.contentWindow.dispatchEvent(evt);
}, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top