因此,我在 DomainA 上有一个页面,并且使用 Chrome 扩展程序,我正在注入一些 javascript 来插入指向 DomainB 的 iframe。
$("body").append("<iframe id='someFrame' src='http://www.domainB.com' width='300' height='800'></iframe>");
我还将一些 javascript 注入到 DomainA 中,试图获取 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)