diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-18 22:04:30 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-18 22:04:30 +0900 |
commit | 903c9c78b7965b3a046ad90af36e3cac676da452 (patch) | |
tree | 52cde5919c1940a18ac03b59b90fa42116036308 /src/shared | |
parent | 86431e82f75af48f1a8b98c95257714ff75261fc (diff) |
remove message propagation
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/messages.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/shared/messages.js b/src/shared/messages.js index 134d71c..dc497b6 100644 --- a/src/shared/messages.js +++ b/src/shared/messages.js @@ -1,3 +1,26 @@ +const onWebMessage = (listener) => { + window.addEventListener('message', (event) => { + let sender = event.source; + let message = null; + try { + message = JSON.parse(event.data); + } catch (e) { + // ignore unexpected message + return; + } + listener(message, sender); + }); +}; + +const onBackgroundMessage = (listener) => { + browser.runtime.onMessage.addListener(listener); +}; + +const onMessage = (listener) => { + onWebMessage(listener); + onBackgroundMessage(listener); +}; + export default { BACKGROUND_OPERATION: 'background.operation', @@ -23,4 +46,8 @@ export default { SETTINGS_RELOAD: 'settings.reload', SETTINGS_CHANGED: 'settings.changed', SETTINGS_QUERY: 'settings.query', + + onWebMessage, + onBackgroundMessage, + onMessage, }; |