简体   繁体   English

BroadcastChannel.onmessage 在 Firefox 中不起作用

[英]BroadcastChannel.onmessage not working in firefox

I need to share some information between 2 tabs of the same browser pointing at the same site.我需要在指向同一站点的同一浏览器的 2 个选项卡之间共享一些信息。 I'm using the BroadcastChannel.onmessage event handler as detailed here: https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/onmessage我正在使用 BroadcastChannel.onmessage 事件处理程序,详情如下: https : //developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/onmessage

In the sender tab javascript code I defined:在我定义的发件人选项卡 javascript 代码中:

var bc = new BroadcastChannel('my_bc_channel');
bc.postMessage(i); //where i is simply the line number i want to share

And in the receiving tab:在接收选项卡中:

var bc = new BroadcastChannel('my_bc_channel');
//then use this to receive the incoming messages:
bc.onmessage = function (ev) {
    last_line = ev.data
}

My code works fine in Chrome, but it does not do anything in Firefox (latest version 70 freshly installed under windows).我的代码在 Chrome 中运行良好,但在 Firefox 中没有任何作用(最新版本 70 在 Windows 下新安装)。 The compatibility chart in the link I gave above says it should be working from Firefox version 38.我上面给出的链接中的兼容性图表说它应该可以在 Firefox 38 版上运行。

The thing is, I'm not sure how to debug this.问题是,我不确定如何调试它。 I don't have any error messages in the console.我在控制台中没有任何错误消息。 I don't know if it's the sender code that does not send anything.我不知道是不是发件人代码不发送任何东西。 But clearly the receiving code is not triggered so I guess the .onmessage event is not detected.但显然没有触发接收代码,所以我猜没有检测到 .onmessage 事件。 Where can I see in the javascript console if the message is sent ?如果消息已发送,我可以在 javascript 控制台中的何处查看?

I opened a bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=1600512我打开了一个错误报告: https : //bugzilla.mozilla.org/show_bug.cgi?id=1600512

Seems BroadcastChannel.onmessage does not work properly when you host your files locally on your hard drive, as I did in my example above.当您将文件本地托管在硬盘驱动器上时,似乎 BroadcastChannel.onmessage 无法正常工作,就像我在上面的示例中所做的那样。 It works otherwise.它以其他方式工作。

It might be that the channel "in the receiving tab" has it's onmessage handler set only after the other tab has broadcast the message, and the message is lost.可能是“在接收选项卡中”的频道只有在另一个选项卡广播消息后才设置了它的 onmessage 处理程序,并且消息丢失了。

You might want the "sending" tab to also set a message handler, and wait for the other tab to broadcast a message that it is ready to receive, and only then start the broadcast.您可能希望“发送”选项卡还设置消息处理程序,并等待另一个选项卡广播准备接收的消息,然后才开始广播。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM