[英]BroadcastChannel testing in chrome developer tools console isn't working
I am testing the BroadcastChannel functionality and I'am having trouble.我正在测试BroadcastChannel功能,但遇到了麻烦。 I open two Chrome windows and the dev tools for each.
我打开两个 Chrome windows 和每个的开发工具。 On the console I write:
在控制台上我写:
const z = new BroadcastChannel('blarg')
z.onmessage = function (ev) {console.log(ev)}
I can examine z and it has the function saved to the onmessage prop so it all looks good.我可以检查 z 并且它有 function 保存到 onmessage 道具所以一切看起来都很好。 However, when I test:
但是,当我测试时:
z.postMessage('sweet')
in one of the consoles, nothing shows in the other.在其中一个控制台中,另一个控制台中没有任何显示。 I would expect since both Chrome windows are subscribed to the broadcast channel
blarg
and have a function to console log the message that is posted, I would see the message sweet
to be shown in the other console but nothing happens.我希望 Chrome windows 都订阅了广播频道
blarg
并且有一个 function 来控制台记录发布的消息,我会看到消息sweet
显示在另一个控制台中,但没有任何反应。
So two questions:所以两个问题:
Can't I test BroadcastChannel
interface in the devtools console like this?我不能像这样在 devtools 控制台中测试
BroadcastChannel
接口吗?
If so, what am I missing about how BroadcastChannel works?如果是这样,我对 BroadcastChannel 的工作方式有何遗漏?
It works if:它在以下情况下有效:
blarg
in your case, but it could also be ""
)blarg
,但也可能是""
)When you say当你说
I open two chrome windows and the dev tools for each.
我打开两个 chrome windows 和每个的开发工具。
call them A
and B
.称他们为
A
和B
。 In nomenclature A
and B
are defined as "browsing contexts", and they could also be tabs, frames or iframes.在命名法中,
A
和B
被定义为“浏览上下文”,它们也可以是选项卡、框架或 iframe。
The following code is an example that satisfies conditions 1. and 2.以下代码是满足条件 1. 和 2 的示例。
devtools A
:开发工具
A
:
location.href = 'https://example.com'
z = new BroadcastChannel('blarg')
z.onmessage = (ev) => { console.log(ev) }
devtools B
:开发工具
B
:
location.href = 'https://example.com'
z = new BroadcastChannel('blarg')
z.onmessage = (ev) => { console.log(ev) }
Condition 1 is also satisfied when browser contexts (windows, tabs, frames or iframes) don't "point" to any regular url (for example when you press CTRL+T to open a new tab).当浏览器上下文(窗口、选项卡、框架或 iframe)不“指向”任何常规 url(例如,当您按 CTRL+T 打开新选项卡时)时,也满足条件 1。 In this case the origin has the special value of
chrome://new-tab-page
.在这种情况下,原点具有
chrome://new-tab-page
的特殊值。
Said that, if you post from A
to B
, B
will receive the message and console.log
it.说,如果您从
A
发布到B
, B
将收到消息并console.log
它。 The same applies vice versa.反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.