diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-06-10 22:46:11 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-07-05 21:32:43 +0900 |
commit | caced372415a944c4297157397d0027ba629fff0 (patch) | |
tree | d2b86fbea66063abba5a39ff6f4cc1487d8474c9 /src | |
parent | 0d3cb2b613f0d03972cbb4217c4d705946959975 (diff) |
Return a value to background script
This change makes the content script able to return a value to the
background script by the following steps: 1) receive messages in a
single listener, 2) return value with Promise in `onMessage`.
Diffstat (limited to 'src')
-rw-r--r-- | src/content/Application.ts | 19 | ||||
-rw-r--r-- | src/content/MessageListener.ts | 2 |
2 files changed, 10 insertions, 11 deletions
diff --git a/src/content/Application.ts b/src/content/Application.ts index 4c89fa0..9ea047c 100644 --- a/src/content/Application.ts +++ b/src/content/Application.ts @@ -56,16 +56,6 @@ export default class Application { } return undefined; }); - - this.messageListener.onBackgroundMessage((msg: Message) => { - switch (msg.type) { - case messages.ADDON_ENABLED_QUERY: - return this.addonEnabledController.getAddonEnabled(msg); - case messages.TAB_SCROLL_TO: - return this.markController.scrollTo(msg); - } - return undefined; - }); } private routeCommonComponents(): Promise<void> { @@ -102,6 +92,15 @@ export default class Application { case messages.CONSOLE_RESIZE: return this.consoleFrameController.resize(msg); } + + if (window.self === window.top) { + switch (msg.type) { + case messages.ADDON_ENABLED_QUERY: + return this.addonEnabledController.getAddonEnabled(msg); + case messages.TAB_SCROLL_TO: + return this.markController.scrollTo(msg); + } + } }); const inputDriver = new InputDriver(window.document.body); diff --git a/src/content/MessageListener.ts b/src/content/MessageListener.ts index 3fe1dcd..ec6940b 100644 --- a/src/content/MessageListener.ts +++ b/src/content/MessageListener.ts @@ -27,7 +27,7 @@ export default class MessageListener { ) { browser.runtime.onMessage.addListener( (msg: any, sender: WebExtMessageSender) => { - return listener(valueOf(msg), sender); + return Promise.resolve(listener(valueOf(msg), sender)); } ); } |