Chrome 扩展:通信 iframe <->内容脚本

发布于 2024-12-01 05:51:33 字数 1242 浏览 3 评论 0 原文

嗯,经常讨论的事情。但我无法让它发挥作用。必须做什么:

  • 内容或后台脚本必须与 iframe 通信,反之亦然。
  • iframe在我手下,一切皆有可能。

我尝试了很多。它根本不起作用。例如:如果我通过(清单)在每个页面上部署内容脚本,并使用 allFrames=true 。好吧,有道理。 iframe 稍后创建,因此不会调用触发器。因此,让我们这样做:创建 iframe,然后发送一个executeScript 请求:

 chrome.tabs.executeScript(tabinst.tab_id, { allFrames: true, file:'frame.js'}, function() {
      console.log("done");
 });

但这也不起作用。有没有人有与 XDM iframe 和 chrome 扩展程序通信的解决方案?

PS:如果 chrome 扩展允许在 iframe 上使用 postMessage,那该多好

EDIT1:

代码不会注入 iframe 中。场景:

文件“file.js”中有一个简单的 foo 函数。现在,我在 iframe 创建并显示后 2 秒将其与上述语句一起应用。此函数 foo 在 iframe 中不可用...但在内容脚本中可用。尝试在 iframe 中执行 foo(通过单击)会引发 ReferenceError。

所以,这不是一个时间问题。如果我通过清单应用脚本并且 all_frames true 并不重要。如果可行,则 content_script 将可用。但事实并非如此。

编辑2: @serg

是的,谢谢,这有效!我刚刚经历过。我的问题是,我假设当调用 chrome.tabs.executeScript 的回调时,请求的脚本结束并且包含 DOM 操作完成。但事实并非如此。 iframe 中的脚本和包含的侦听器准备就绪需要一些时间。

因此,我必须从 iframe 中的脚本发送 chrome.extension.sendRequest,然后从后台侦听器启动一些代码来操作 iframe。感谢您的帮助。

PS:也可以在没有“all_frames”的情况下做到这一点:true。动态 iframe 准备就绪只需要一些时间。有超时就可以了。在大多数情况下,这没有用,但也许有人首先进行了另一个用户交互。

PPS:我仍然不明白为什么可以这样,并且不可能发送 postMessage 事件。但也许有一天这会起作用。

Well, a often discussed thing. But I can't get it work. What has to be done:

  • The Content or Background Script has to communicate with the iframe et vice versa.
  • The iframe is under my hand, so there is everything possible.

I tried a lot. It doesn't work at all. For instance: If I deploy the content script on every page with allFrames=true via (manifest). Ok, makes sense. The iframe is created later so the trigger won't be called. So let's do this: create the iframe and afterwards sending an executeScript request:

 chrome.tabs.executeScript(tabinst.tab_id, { allFrames: true, file:'frame.js'}, function() {
      console.log("done");
 });

But that doesn't work either. Has anyone a solution to communicate with an XDM iframe and a chrome extension?

PS: How nice it would be if the chrome extension would allow postMessage on iframe

EDIT1:

The code doesn't get injected in the iframe. Scenario:

The file "file.js" has a simple foo function in it. I now apply it with the above statement 2 seconds after the iframe was created and showed. This function foo is not available in the iframe...but is in the content script. The ReferenceError is thrown by trying to execute foo within the iframe (by click).

So, it's not a timing thing. And it doesn't matter if I apply the Scripts via manifest and all_frames true. If that would work, the content_script would be available. But is not.

EDIT2:
@serg

Yeah, thanks, that works! I just got through it. My problem was, that I assumed that when the callback of chrome.tabs.executeScript is called, the requested script is ended and the including DOM manipulation finished. But that's actually not the case. It takes some time till the script in the iframe and the containing listener is ready.

So I had to send a chrome.extension.sendRequest from that script in the iframe and then start some code out of the background listener to manipulate the iframe. Thanks for your help.

PS: It's also possible to do it without "all_frames": true. It just takes some time till the dynamic iframe is ready. With a timeout it works. For the most cases, this is not useful, but maybe someone has another user interaction first.

PPS: I still can't see why it's possible like this, and not possible to send postMessage events. But maybe somewhen this will works.

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

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

发布评论

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

评论(1

乖乖哒 2024-12-08 05:51:33

我刚刚测试过,内容脚本被注入到动态创建的 iframe 中(我使用了清单)。我认为问题是您正在尝试访问 iframe 内的内容脚本功能,这是不允许的。

在 iframe 内部,您不能这样做:

<a onclick="contentScriptFunction()"></a>

您需要从内容脚本中添加事件侦听器:

$("a").click(contentScriptFunction);

I just tested and content script is getting injected into dynamically created iframes (I used manifest). I think the problem is you are trying to access content script's function within iframe, which is not allowed.

Inside your iframe you can't just do:

<a onclick="contentScriptFunction()"></a>

You need to be adding event listener from within the content script:

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