CustomEvent()到XUL选项卡 - Firefox插件 [英] CustomEvent() to an XUL tab - Firefox addon
问题描述
由于很多原因,我必须在标签页中打开XUL,而不是使用标准窗口。我希望将自定义事件发送到此选项卡,此处我的代码如何:
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:
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代码:
options.xul code:
window.addEventListener("load", init, false);
window.addEventListener("prefwindow-event", myevent, false, true);
..
myevent: function(e) {
dump("My event : " + e.details );
},
..
但是,我没有收到事件。我已经尝试了所有可能的选择。启用/禁用 useCapture
和 wantUntrusted
addEventListener()。在意识到窗口之间发送自定义事件时有限制,我也尝试使用tab元素调度事件,如下所示:
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);
这也不行。 1)事件调度将工作完全正常,如果我使用对话窗口的标签( openDialog
而不是 loadOneTab
)。 2)标签元素只继承 dispatchEvent
而不是 CustomEvent
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?
推荐答案
var event = new pTab.linkedBrowser._contentWindow.CustomEvent("prefwindow-event");
编辑:
概念证明,打开暂存器,切换环境到浏览器,运行以下代码片段
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);
这篇关于CustomEvent()到XUL选项卡 - Firefox插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!