sendToTabWithInjection function exported ✓ 100.0%

Last updated: 2026-02-24T21:07:57.586Z

Metrics

LOC: 27 Complexity: 7 Params: 4 Coverage: 100.0% (11/11 lines, 4x executed)

Signature

sendToTabWithInjection( tabId: number, tabUrl: string | undefined, message: ExtensionMessage, ): : Promise<unknown>

Summary

Sends a message to a tab, auto-injecting the content script if the first attempt fails (e.g. on a freshly opened page).

Source Code

export async function sendToTabWithInjection(
  tabId: number,
  tabUrl: string | undefined,
  message: ExtensionMessage,
): Promise<unknown> {
  const firstTry = await sendToTab(tabId, tabUrl, message);
  if (!(firstTry && typeof firstTry === "object" && "error" in firstTry)) {
    return firstTry;
  }

  try {
    const manifest = chrome.runtime.getManifest();
    const files = (manifest.content_scripts?.[0]?.js ?? []) as string[];
    if (files.length === 0)
      throw new Error("No content script files in manifest");
    await chrome.scripting.executeScript({
      target: { tabId },
      files,
    });
    return await chrome.tabs.sendMessage(tabId, message);
  } catch (injectErr) {
    return {
      error: "Content script not responding",
      details: String(injectErr),
    };
  }
}

Dependencies (Outgoing)

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

Impact (Incoming)

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