gratipay / grtp.co

Gratipay Widgets + API
https://grtp.co/
MIT License
26 stars 19 forks source link

Cross-domain communication with Gittip.com #50

Closed duckinator closed 6 years ago

duckinator commented 10 years ago

For implementing the modal (mentioned in https://github.com/gittip/www.gittip.com/issues/1167) on pages (which we do not control) which run our widgets, we need a way to communicate information -- such as the size of the iframe's content -- from the iframe to the parent, and vice-versa.

The way I've seen this done previously is using the Window.postMessage API. Since the data is represented as a single string, the way I'd do it is by passing a serialized JSON object.

Something like the following, plus the corresponding code for receiving it and dispatching things to the correct functions:

function send (command, object) {
    object['command'] = command;
    parent.postMessage(JSON.stringify(object), '*');
}

var iframeWidth  = magicToGetWidth();
var iframeHeight = magicToGetHeight();
send("resize", {width: iframeWidth, height: iframeHeight});