angular-post-message icon indicating copy to clipboard operation
angular-post-message copied to clipboard

Add support to specify recipient window explicitly

Open quaardvark opened this issue 8 years ago • 6 comments

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.

quaardvark avatar Dec 29 '15 15:12 quaardvark

Sounds like you're requesting a couple features here

  1. move the $braodcast from the $rootScope - but to where? Most applications need the message to broadcast down from the $rootScope.
  2. 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.

kylewelsby avatar Jan 03 '16 16:01 kylewelsby

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.

quaardvark avatar Jan 04 '16 14:01 quaardvark

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/

TheH0g4n avatar Oct 13 '17 14:10 TheH0g4n

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.

kylewelsby avatar Oct 16 '17 19:10 kylewelsby

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.

xiaoyao9184 avatar Feb 12 '19 09:02 xiaoyao9184

Thanks, @xiaoyao9184 do you have time to open a Pull Request with this change?

kylewelsby avatar Feb 12 '19 11:02 kylewelsby