为什么我的 Chrome 扩展程序无法使用 HTML5 postMessage 与我注入的框架进行通信?

发布于 2024-09-30 12:26:29 字数 874 浏览 2 评论 0原文

因此,我在 DomainA 上有一个页面,并且使用 Chrome 扩展程序注入一些 javascript,插入指向 DomainB 的 iframe。

$("body").append("<iframe id='someFrame' src='http://www.domainB.com' width='300' height='800'></iframe>");

我还在 DomainA 中注入了一些 javascript,尝试获取 iframe 的 contentWindow。我想在上面使用 HTML5 postMessage api。

$("body").append("<a class='myLink'>Post Message</a>");
$(".myLink").click(function(){
    var frameElem = document.getElementById("someFrame"); 
    console.log("frameElem: " + frameElem); //succeeds

var contentWin = frameElem.contentWindow;
console.log("contentWin : " + contentWin); //undefined

//can't do this since contentWin is undefined: 
//contentWin.postMessage("data", "*");
});

但是,contentWindow 属性未定义。为什么会这样,我该如何解决它?如果我将此扩展代码放入网页中,它本身就可以正常工作。

谢谢!

(请原谅蹩脚的jquery/javascript)

So, I have a page on DomainA, and, using a Chrome extension, I'm injecting some javascript that inserts iframe that points to DomainB.

$("body").append("<iframe id='someFrame' src='http://www.domainB.com' width='300' height='800'></iframe>");

I also inject a some javascript into DomainA that attempts to get the iframe's contentWindow. I want to use the HTML5 postMessage api on it.

$("body").append("<a class='myLink'>Post Message</a>");
$(".myLink").click(function(){
    var frameElem = document.getElementById("someFrame"); 
    console.log("frameElem: " + frameElem); //succeeds

var contentWin = frameElem.contentWindow;
console.log("contentWin : " + contentWin); //undefined

//can't do this since contentWin is undefined: 
//contentWin.postMessage("data", "*");
});

However, the contentWindow property is undefined. Why is that, and how can I get around it? If I put this extension code in a webpage it'll work fine by itself.

Thanks!

(pardon the crappy jquery/javascript)

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

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

发布评论

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

评论(3

相权↑美人 2024-10-07 12:26:29

我知道回答我自己的问题有点蹩脚,但我做了更多挖掘,发现了 Chromium 上针对该问题提交的一个错误:http://code.google.com/p/chromium/issues/detail?id=20773

我在 chromium 扩展组中找到了此链接:http://groups.google.com/a /chromium.org/group/chromium-extensions/browse_thread/thread/1d4b68f0971ef190/3446a7e82848351c?lnk=gst&q=contentWindow#3446a7e82848351c

I know it's kind of lame to answer my own question, but I did some more digging, and found a bug filed on Chromium for the issue: http://code.google.com/p/chromium/issues/detail?id=20773

I found this link in the chromium extensions group: http://groups.google.com/a/chromium.org/group/chromium-extensions/browse_thread/thread/1d4b68f0971ef190/3446a7e82848351c?lnk=gst&q=contentWindow#3446a7e82848351c

不乱于心 2024-10-07 12:26:29

我认为这与内容脚本无法访问其父页面的窗口对象的原因相同。请参阅此问题,其中包含指向解决方法的链接。

I think it's for the same reasons why content scripts cannot access window object of their parent page. See this question, and it has a link to a workaround.

够钟 2024-10-07 12:26:29

您需要在根框架中嵌入一个 iframe,该 iframe 将请求发送到客户端框架,然后客户端框架将命令发送回根网站,如以下示例所示。这被称为“单向管道”黑客:
http://msdn.microsoft.com/en-us/library/bb735305。 ASPX

替代文本

You need to embed an iframe in your root frame which sends requests to a client frame, which then sends commands back to the root website as shown in the following example. This is known as the 'one-way pipe' hack.:
http://msdn.microsoft.com/en-us/library/bb735305.aspx

alt text

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