window.postMessage and frames

90 views
Skip to first unread message

Gildas

unread,
Jun 8, 2010, 6:18:55 PM6/8/10
to Chromium-extensions
Hi,

I have found that this kind of code which allows to send message
between frames works well a into content script :

window.addEventListener("message", function(event) {
if (event.data == "hi !") {
console.log(event);
}
}, false);

location.href="javascript:parent.postMessage('hi !','*')";

It logs the received event into each Window object with at least one
child frame.

But it works with all domains (i.e. cross-domain messaging) only if I
inject the code into the page this way :
location.href="javascript:parent.postMessage('hi !','*')";

instead of simply writing :
parent.postMessage('hi !','*')

It is a feature or a bug ?

Gildas

unread,
Jun 15, 2010, 6:21:49 AM6/15/10
to Chromium-extensions
For your information, I use this kind of communication in a extension
since it's the only way I found to reconstruct dependably Window
objects DOM (i.e. nested frames) in the background page without
relying on frame src. I cannot rely on frame src because I'm unable to
match a frame with a redirected (HTTP 300x) content
(document.location.href != frame.src).

Actually, reconstructing Window objects DOM is painful.

There should be a way to do this easily. For example, the Port class
could have "parentPort" (storing a reference to parent Window port)
and "childPorts" (storing references to frame Window ports)
properties.
I think also that Window.postMessage should be accessible in a content
script since it's a standard way to send messages between frames. The
"location.href="javascript:parent.postMessage('hi !','*')"; trick is
just ugly.

Regards,

Gildas.
Reply all
Reply to author
Forward
0 new messages