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)

graph LR sendToTab["sendToTab"] sendToTabWithInjection["sendToTabWithInjection"] sendToActiveTab["sendToActiveTab"] sendToSpecificTab["sendToSpecificTab"] sendToTabWithInjection -->|calls| sendToTab sendToActiveTab -->|calls| sendToTab sendToSpecificTab -->|calls| sendToTab style sendToTab fill:#dbeafe,stroke:#2563eb,stroke-width:2px click sendToTab "4b0c8d5eae39e1ff.html" click sendToTabWithInjection "c1233e42257a3186.html" click sendToActiveTab "a91f63e9f38af9db.html" click sendToSpecificTab "3da88adecaec2d24.html"