From caced372415a944c4297157397d0027ba629fff0 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 10 Jun 2021 22:46:11 +0900 Subject: 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`. --- src/content/Application.ts | 19 +++++++++---------- src/content/MessageListener.ts | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'src/content') 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 { @@ -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)); } ); } -- cgit v1.2.3