diff options
Diffstat (limited to 'src/shared/messages.js')
-rw-r--r-- | src/shared/messages.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/shared/messages.js b/src/shared/messages.js new file mode 100644 index 0000000..517fc4c --- /dev/null +++ b/src/shared/messages.js @@ -0,0 +1,19 @@ +const receive = (win, callback) => { + win.addEventListener('message', (e) => { + let message; + try { + message = JSON.parse(e.data); + } catch (e) { + // ignore message posted by author of web page + return; + } + + callback(message); + }) +} + +const send = (win, message) => { + win.postMessage(JSON.stringify(message), '*'); +} + +export { receive, send }; |