そのため、DomainA にページがあり、Chrome 拡張機能を使用して、DomainB を指す iframe を挿入する JavaScript を挿入しています。
$("body").append("<iframe id='someFrame' src='http://www.domainB.com' width='300' height='800'></iframe>");
また、iframe の contentWindow を取得しようとする JavaScript を DomainA に挿入します。その上で 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 プロパティは未定義です。それはなぜですか、どうすれば回避できますか? この拡張コードを Web ページに配置すると、それだけで正常に動作します。
ありがとう!
(安っぽいjquery/javascriptを許してください)