krakenjs / post-robot

Cross domain post-messaging on the client side using a simple listener/client pattern.
Apache License 2.0
747 stars 91 forks source link

Send a message to All child Iframes #27

Closed sonoman closed 7 years ago

sonoman commented 7 years ago

I need to send to all xcomponents running within a parent window a certain message (like broadcasting)....

Can post-robot handle this requirement in any way I'm not seeing ?

bluepnume commented 7 years ago

There's no built-in way to do this right now, because it's not always possible to enumerate all of the open popups and iframes, but if you just want to message all iframes it's a pretty simple traversal.

function messageAllFrames(win, name) {
    postRobot.send(win, name);
    for (var i = 0; i < win.frames.length; i++) {
        messageAllFrames(win.frames[i], name);
    }
}

messageAllFrames(window.top, 'foo');

Does that work for your use-case?

sonoman commented 7 years ago

It totally makes sense. Thank you!

sonoman commented 7 years ago

I tested your suggestion with a blank page with 2 iframes, and it worked. But when I combine with xComponent, it won't work... is there any incompatibility to use post-robot with xComponent alltogether ?

bluepnume commented 7 years ago

Do you see a specific error when doing this in conjunction with xcomponent?

sonoman commented 7 years ago

Actually, I'm not receiving anything on the event listener application, and the callback that I have on the "send", is returning an error related to cross origin...I have the parent application running on localhost:4200 (where I have the "Send" call), and is rendering an xcomponent with an application on localhost:8080 (where I have the "on" listener). The following is a stringify of the event object returned on the callback

ack: "error" domain:"http://localhost:4200" error:"Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame.↵Error: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame.↵ at JSON.stringify ()↵ at http://localhost:8080/?uid=8c123d7ab7&version=latest&xcomponent=1:79:49↵ at http://localhost:8080/xcomponent.js:1537:36↵ at trycatch (http://localhost:8080/xcomponent.js:1600:10)↵ at new SyncPromise (http://localhost:8080/xcomponent.js:1729:6)↵ at Object.deNodeify (http://localhost:8080/xcomponent.js:1534:17)↵ at Object.onSuccess (http://localhost:8080/xcomponent.js:3761:30)↵ at _loop2 (http://localhost:8080/xcomponent.js:1802:55)↵ at SyncPromise.dispatch (http://localhost:8080/xcomponent.js:1836:22)↵ at SyncPromise.then (http://localhost:8080/xcomponent.js:1862:11)" hash:"SAVE_STATE_REQUESTED_673eed1063" id:"b4a9e3bec8" name:"SAVE_STATE_REQUESTED" sourceDomain:"http://localhost:8080" type:"postrobot_message_response" windowType:"iframe"

bluepnume commented 7 years ago

Looks like http://localhost:8080/?uid=8c123d7ab7&version=latest&xcomponent=1:79:49 is some code running in your iframe, guessing in a script tag? Could you share the code?