injectContentScript function application exported
Last updated: 2026-03-05T10:53:28.864Z
Metrics
LOC: 17
Complexity: 4
Params: 1
Signature
injectContentScript(tabId: number): : Promise<boolean>
Summary
Inject a content script into a tab. Used to re-inject after cross-origin navigations that destroy the previous content-script context. Chrome re-injects the manifest-declared content script automatically when the page loads. This function polls until the content script is responsive (via PING) or the timeout is reached.
Source Code
export async function injectContentScript(tabId: number): Promise<boolean> {
const maxAttempts = 6;
const retryDelayMs = 250;
for (let i = 0; i < maxAttempts; i++) {
await sleep(retryDelayMs);
try {
const response = await chrome.tabs.sendMessage(tabId, { type: "PING" });
if (response?.pong) return true;
} catch {
// content script not yet ready — retry
}
}
log.warn(`Content script did not respond in tab ${tabId} after polling`);
return false;
}
Dependencies (Outgoing)
| Target | Type |
|---|---|
| sleep | calls |
Impact (Incoming)
| Source | Type |
|---|---|
| OrchestratorCallbacks | uses |
| sendStepToContentScript | calls |
| executeNavigateStep | calls |
| runLoop | calls |