angular-post-message
angular-post-message copied to clipboard
Add support to specify recipient window explicitly
Currently, the recipient window that is targeted by $messageOutgoing
event is defined in a confusing way. It is either window.parent
or event source of the message that has been received earlier by emitting window. In my opinion, this is confusing and prone to side effects (event source also pollutes $rootScope
without necessity).
I think that ability to specify recipient window explicitly would add great value to the module and make it really universally usable. In my case, I needed to send message to window.opener
which is not supported out of the box.
Sounds like you're requesting a couple features here
- move the
$braodcast
from the$rootScope
- but to where? Most applications need the message to broadcast down from the$rootScope
. - include a parameter to target a specific listener. - Is this something that is supported in native
postMessage
?
Could you make a little example on JSFiddle/JSBin/Plunker of what you're expecting. Or even better submit a pull-request with your changes.
What I meant about $rootScope
, is that listener currently accessed via $rootScope.sender
which is set in confusing way and there is also no real need to keep it in $rootScope
. I just think it could be more clear and controlled.
I agree. We recently encountered an issue in our application while running in Chrome 61 where triggering the message event from the parent window would not send the event down to the window of an embedded iFrame. The solution was to set $rootScope.sender to the contentWindow of the iFrame element.
Also as per this Mozilla documentation on postMessage it should be triggered from the target window (otherWindow in the example used by Mozilla):
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
The example used here in the HTML5 Web Messaging article on the W3C website uses the contentWindow of the target frame to post the message as well (section 4.1).
https://www.w3.org/TR/webmessaging/
Not touched this project in a while. if you fancy. Do open a pull request, I'll get it into the master branch for you.
I think if pop-up new window, and sub-window post massage back to man-window, need to use window.opener
not $window.parent
https://github.com/kylewelsby/angular-post-message/blob/d3f73038606102d10ecb4861ee2ee16c4bfa96e4/src/angular-post-message.js#L16
I think window.opener
has a higher priority.
set $rootScope.sender
first also can do, but through the parameter will be more clear.
Thanks, @xiaoyao9184 do you have time to open a Pull Request with this change?