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)
| Target | Type |
|---|---|
| sendToTab | calls |
Impact (Incoming)
| Source | Type |
|---|---|
| sendToActiveTab | calls |
| sendToSpecificTab | calls |