From ccec6e320a76969db0cae45ff8753866f94387a5 Mon Sep 17 00:00:00 2001 From: mathieudutour Date: Thu, 23 Nov 2023 10:03:42 +0100 Subject: [PATCH 1/2] Messaging: Fix async listeners --- api/messaging/src/message.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/api/messaging/src/message.ts b/api/messaging/src/message.ts index 1bc0dfdda..c4d282eb5 100644 --- a/api/messaging/src/message.ts +++ b/api/messaging/src/message.ts @@ -4,7 +4,11 @@ import { getExtRuntime } from "./utils" export const listen = ( handler: PlasmoMessaging.Handler ) => { - const metaListener = async (req, sender, sendResponse) => { + const metaListener = async ( + req: any, + sender: chrome.runtime.MessageSender, + sendResponse: (response?: ResponseBody) => void + ) => { await handler?.( { ...req, @@ -16,9 +20,13 @@ export const listen = ( ) } - const listener = (req, sender, sendResponse) => { + const listener = ( + req: any, + sender: chrome.runtime.MessageSender, + sendResponse: (response?: ResponseBody) => void + ) => { metaListener(req, sender, sendResponse) - return // Syncronous return to indicate this is an async listener + return true // Synchronous return to indicate this is an async listener } getExtRuntime().onMessage.addListener(listener) From 6a0465cbe14cfd92db5749f2a3324e774cd7367b Mon Sep 17 00:00:00 2001 From: mathieudutour Date: Thu, 23 Nov 2023 10:03:56 +0100 Subject: [PATCH 2/2] Messaging: Improve TS Docs --- api/messaging/src/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/api/messaging/src/index.ts b/api/messaging/src/index.ts index 8bbefadfa..44ee33776 100644 --- a/api/messaging/src/index.ts +++ b/api/messaging/src/index.ts @@ -12,18 +12,19 @@ export type { } from "./types" /** - * Should only be called from CS or Ext Pages - * Extension Id is required to send a message from a CS in the main world - * TODO: Add a framework runtime check, using a global variable + * Send to Background Service Workers from Content Scripts or Extension pages. + * `extensionId` is required to send a message from a Content Script in the main world */ +// TODO: Add a framework runtime check, using a global variable export const sendToBackground: PlasmoMessaging.SendFx = async ( req ) => { - return getExtRuntime().sendMessage(req.extensionId ?? null, req) + return getExtRuntime().sendMessage(req.extensionId ?? null, req) } /** - * Send to CS from Ext pages or BGSW, default to active tab if no tabId is provided in the request + * Send to Content Scripts from Extension pages or Background Service Workers. + * Default to active tab if no tabId is provided in the request */ export const sendToContentScript: PlasmoMessaging.SendFx = async (req) => { const tabId =