sendToTab function test exported
Last updated: 2026-03-01T23:25:47.056Z
Metrics
LOC: 16
Complexity: 2
Params: 5
Signature
sendToTab(
background: Worker,
pageUrl: string,
message: Record<string, unknown>,
): : Promise<unknown>
Summary
Sends a message to the content script of the given page via the background service worker (chrome.tabs.sendMessage).
Tags
#@param background - The extension background service worker#@param pageUrl - URL of the page (used to find the tab ID)#@param message - Message object to send (`{ type, payload? }`)#@returns The response from the content script
Source Code
export async function sendToTab(
background: Worker,
pageUrl: string,
message: Record<string, unknown>,
): Promise<unknown> {
const origin = new URL(pageUrl).origin;
return background.evaluate(
async ({ tabOrigin, msg }) => {
const tabs = await chrome.tabs.query({ url: `${tabOrigin}/*` });
const tab = tabs[0];
if (!tab?.id) throw new Error(`No tab found for origin: ${tabOrigin}`);
return chrome.tabs.sendMessage(tab.id, msg);
},
{ tabOrigin: origin, msg: message },
);
}
No outgoing dependencies.
Impact (Incoming)
| Source | Type |
|---|---|
| sendToTabWithInjection | calls |
| sendToActiveTab | calls |
| sendToSpecificTab | calls |