7

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)

Newtang
  • 6,414
  • 10
  • 49
  • 70

3 Answers3

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

Newtang
  • 6,414
  • 10
  • 49
  • 70
1

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.

Community
  • 1
  • 1
serg
  • 109,619
  • 77
  • 317
  • 330
-1

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

bbosak
  • 5,353
  • 7
  • 42
  • 60
  • This is not what I'm looking for. I don't need to utilize a hack for a chrome extension. I'm trying to figure out why the contentWindow property is undefined so I can use postMessage. – Newtang Nov 02 '10 at 17:30