window.postMessage to sandboxed iframe

1,127 views
Skip to first unread message

Henning Skoglund

unread,
Oct 16, 2013, 6:48:49 AM10/16/13
to chromi...@chromium.org
I'm writing a temperature logger via chrome.usb ANT+ wireless communication. I'm struggeling with sending messages from a window to the sandboxed iframe (with a local script) thats embedded in the window. The reason for sandboxing the in the iframe is that's impossible to run knockoutjs due to CSP.

I've set up a handler in the iframe like suggested in http://developer.chrome.com/apps/app_external.html "Listen for messages"

var messageHandler = function(e) {
   
  console.log('Knockout App got message', e.data);
};

window.addEventListener('message', messageHandler);


When I type "window.postMessage('test','*')" in the <top frame> in developer tools nothing happens. Changing the frame in the drop-down in devtools and typing the same command calls the messageHandler.

I tried to access the .contentWindow property of the iframe, but it gives "DOMException". And accessing .contentDocument gives SecurityError: Blocked a frame with origin "chrome-extension://kbjdachfmhmlnekmflcomjkhijhjkacl" from accessing a cross-origin frame'

Any thoughts?






PhistucK

unread,
Oct 16, 2013, 7:12:44 AM10/16/13
to Henning Skoglund, Chromium Apps
Is there a specific reason you are not using <webview>?
I never tried it, but I heard it has a pretty extensive API.

Regarding posting a message to yourself (same window to the same window), it works for me in a regular web page -
window.addEventListener("message", function (e) { console.log(e.data) }, false)
window.postMessage("hey", "*")
// Outputs hey


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium Apps" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-app...@chromium.org.
To post to this group, send email to chromi...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-apps/.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.

Henning Skoglund

unread,
Oct 16, 2013, 10:29:31 AM10/16/13
to chromi...@chromium.org, Henning Skoglund
Seems like webview is more for hosting external web pages, but I just want to host knockoutjs/UI which is loaded locally. I now know why window.postMessage('test','*') doesnt do anything because its referencing the top window which doesnt have a event handler for message. I only need a reference to the window of the iframe (tried window.frames[0] gives "DOMException") without being with limited by security restrictions.

PhistucK

unread,
Oct 16, 2013, 10:42:14 AM10/16/13
to Henning Skoglund, Chromium Apps
I think it (window.frames[0]) should work, this sounds weird. But anyway, top.frames might get you better results, I am not sure.
Note that accessing window.frames[0] might give you an exception, but perhaps window.frames[0].postMessage can succeed regardless of the exception.


PhistucK

Henning Skoglund

unread,
Oct 16, 2013, 12:20:10 PM10/16/13
to chromi...@chromium.org, Henning Skoglund
Yes, it works by posting to window.frames[0].postMessage('test','*'). I was confused by the "DOMException" (strange error msg, I would prefer a DOMException object with more info whats going on.). Thank you for the advice.
Reply all
Reply to author
Forward
0 new messages