Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 287
Newtang Avatar asked Nov 01 '10 21:11

Newtang


2 Answers

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

like image 131
Newtang Avatar answered Oct 20 '22 08:10

Newtang


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.

like image 27
serg Avatar answered Oct 20 '22 09:10

serg