向父框架发出事件

发布于 2025-02-11 15:58:10 字数 1502 浏览 2 评论 0原文

我正在尝试将聊天机器人(Angular 14应用程序)嵌入iframe中,这有效。下一个阶段是检测内部框架是否应最小化。这是为拟合父容器的样式所需的样式。我希望将自定义事件发送到接收框架。

let iframeCanvas = document.querySelector("iframe").contentWindow.document.querySelector("canvas"); // canvas is where the image is placed for click events

Angular嵌入式页面组件:

<app-avatar #avatar (click)="toggle_chat()" [embedded]=true></app-avatar>

我已经创建了@output() event:@output()isopen = new EventEmitter&lt; boolean&gt;();

在单击avatar时代码> toggle_chat()函数将发射事件:this.isopen.emit(false); //测试是否可以从Vanillajs页面访问该值

问题是我无法访问从Angular发出的事件。如何在父页面上收到这些事件?这些事件在&lt; iframe src =“ ... Angular App ..” .. /&gt; < /code>的内部被判入狱,以便接收消息并将消息发送到iframe事件发射器。

该问题的一个更清晰的图片是这种结构:

<!doctype html>
<body>
   ...
   <script>
      // Receive events from the iframe below
      let iframeCanvas = document.querySelector("iframe").contentWindow.document.querySelector("canvas");
      // Somehow access the iframeCanvas and listen for events
      if(iframeCanvas.isOpen) document.getElementById("chat-app-frame").classList.toggle('full-size-chat-window'); // Show the full size chat window
   </script>
   <iframe id="chat-app-frame" src="...angular/chatapp/index.html" frameborder="0" scrolling="no" style="position:fixed;right:0;bottom:0; height:50%;width:400px;"></iframe>
</body>
</html>

I'm trying to embed a chatbot (Angular 14 app) in an iframe and this works. The next stage is to detect if the frame inside should be minimized. This is needed to style it for fitting the parent container. I wish to send custom events to the receiving frame.

let iframeCanvas = document.querySelector("iframe").contentWindow.document.querySelector("canvas"); // canvas is where the image is placed for click events

Angular embedded page component:

<app-avatar #avatar (click)="toggle_chat()" [embedded]=true></app-avatar>

I have created an @Output() event: @Output() isOpen = new EventEmitter<boolean>();

When clicking on the avatar the toggle_chat() function will emit the event: this.isOpen.emit(false); // testing if the value can be accessed from a vanillaJS page

The issue is I can't access events emitted from Angular. How can I receive these events on the parent page? The events are jailed inside of the <iframe src="...angular app.." .. /> and so to receive messages and send messages to the iframe event emitters are needed.

A more clear picture of the issue is this structure:

<!doctype html>
<body>
   ...
   <script>
      // Receive events from the iframe below
      let iframeCanvas = document.querySelector("iframe").contentWindow.document.querySelector("canvas");
      // Somehow access the iframeCanvas and listen for events
      if(iframeCanvas.isOpen) document.getElementById("chat-app-frame").classList.toggle('full-size-chat-window'); // Show the full size chat window
   </script>
   <iframe id="chat-app-frame" src="...angular/chatapp/index.html" frameborder="0" scrolling="no" style="position:fixed;right:0;bottom:0; height:50%;width:400px;"></iframe>
</body>
</html>

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

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

发布评论

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

评论(1

意犹 2025-02-18 15:58:10

我找到了解决方案。当尝试与不属于的iframe互动时,原点将不起作用。解决方法是使用window.postmessage api。

您可以在此处了解有关此API的更多信息。与iFrame进行交互需要在Windows和Messmessage API之间发送消息是双向的。

I found the solution. When trying to interact with iframe's that do not belong the the origin will not work. The workaround is to use the window.postMessage api.

You can learn more about this API here. Interacting with iFrames requires sending messages between windows and the postMessage API is bi-directional.

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