在沙箱中识别消息中的发件人 iframe

发布于 2025-01-15 10:32:37 字数 708 浏览 2 评论 0原文

为了简化而虚构的情况:

  • 我有一个 grandparent 页面,其中包含一个显示“父页面”的 parent iframe。
  • 此“父页面”包含两个子 iframe childAchildB
  • 出于安全原因,所有 iframe 均为 sandbox="allow-scripts"

parent祖父母childAchildB 发送和接收消息。

parent 收到消息时,我需要能够识别发送者,但我找不到正确的方法,因为 event<的 origin /code> is null

function onReceive(message) {
    // origin is null when the message comes from a sandboxed frame.
    console.log(message.origin);
}

您知道一种干净的方法来知道消息来自谁吗?

Fictional situation for simplification:

  • I have a grandparent page containing one parent iframe displaying the "parent page".
  • This "parent page" contains two children iframes childA and childB.
  • All the iframes are sandbox="allow-scripts" for security reasons.

parent sends and receive messages from/to grandparent, childA and childB.

When parent receives a message, I need to be able to identify the sender, but I can't find the right approach, as the origin of the event is null:

function onReceive(message) {
    // origin is null when the message comes from a sandboxed frame.
    console.log(message.origin);
}

Do you know a clean way to know who does the message come from?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2025-01-22 10:32:37

好的,比我想象的要简单。
我实际上可以将 message.source 属性与子 iframescontentWindowwindow.parent 进行比较祖父母。

OK, it was simpler than I expected.
I actually can compare the message.source property with the contentWindow of the child iframes, or window.parent for the grand parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文