From adcaf2aa48d0453f231268f5db168376c08e4e6b Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 1 Sep 2017 11:40:24 +0900 Subject: message passing by helper functions --- src/shared/messages.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/shared/messages.js (limited to 'src/shared/messages.js') 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 }; -- cgit v1.2.3