diff --git a/api/messaging/src/index.ts b/api/messaging/src/index.ts index 257075c4d..c17b4e848 100644 --- a/api/messaging/src/index.ts +++ b/api/messaging/src/index.ts @@ -26,7 +26,11 @@ export const sendToBackground: PlasmoMessaging.SendFx = async ( */ export const sendToContentScript: PlasmoMessaging.SendFx = async (req) => { const tabId = - typeof req.tabId === "number" ? req.tabId : (await getActiveTab()).id + typeof req.tabId === "number" ? req.tabId : (await getActiveTab())?.id + + if (!tabId) { + throw new Error("No active tab found to send message to.") + } return getExtTabs().sendMessage(tabId, req) } diff --git a/api/messaging/src/utils.ts b/api/messaging/src/utils.ts index 16bfaa5a2..0ffc3ec6c 100644 --- a/api/messaging/src/utils.ts +++ b/api/messaging/src/utils.ts @@ -27,7 +27,7 @@ export const getActiveTab = async () => { active: true, currentWindow: true }) - return tab + return tab as chrome.tabs.Tab | undefined } export const isSameOrigin = (