Why can't my chrome extension use HTML5 postMessage to communicate with the frame that I am inserting?

So, I have a page in DomainA, and using the Chrome extension, I insert some javascript that inserts an iframe that points to DomainB.

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

I also add some javascript to DomainA which is trying to get iframe 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 this, and how can I get around this? If I add this extension code to a web page, it will work on its own.

Thanks!

(sorry cheesy jquery / javascript)

+7
javascript google-chrome google-chrome-extension postmessage
source share
3 answers

I know this is somehow inconvenient to answer my own question, but I did something else and found an error that was imposed on Chromium for the problem: http://code.google.com/p/chromium/issues / detail? id = 20773

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

+10
source share

I think for the same reasons why content scripts cannot access the Windows object on their parent page. See this question and it has a link to a workaround.

+1
source share

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

-one
source share

All Articles