最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why can't my chrome extension use HTML5 postMessage to communicate with a frame I inject? - Stack Overflow

programmeradmin1浏览0评论

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='' 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)

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.' 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)

Share Improve this question asked Nov 1, 2010 at 21:08 NewtangNewtang 6,54411 gold badges53 silver badges72 bronze badges 2
  • Are you just trying to make a cross domain request? Or you need those iframes? – serg Commented Nov 1, 2010 at 21:26
  • I need the iframe to show content on another server. – Newtang Commented Nov 1, 2010 at 21:30
Add a ment  | 

3 Answers 3

Reset to default 10

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./p/chromium/issues/detail?id=20773

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

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.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论