MCPcopy Index your code
hub / github.com/QLHazyCoder/FlowPilot / ensureContentScriptReadyOnTab

Function ensureContentScriptReadyOnTab

background/tab-runtime.js:228–303  ·  view source on GitHub ↗
(source, tabId, options = {})

Source from the content-addressed store, hash-verified

226 }
227
228 async function ensureContentScriptReadyOnTab(source, tabId, options = {}) {
229 const {
230 inject = null,
231 injectSource = null,
232 timeoutMs = 30000,
233 retryDelayMs = 700,
234 logMessage = '',
235 } = options;
236
237 const start = Date.now();
238 let lastError = null;
239 let logged = false;
240 let attempt = 0;
241
242 console.log(
243 LOG_PREFIX,
244 `[ensureContentScriptReadyOnTab] start ${source} tab=${tabId}, timeout=${timeoutMs}ms, inject=${Array.isArray(inject) ? inject.join(',') : 'none'}`
245 );
246
247 while (Date.now() - start < timeoutMs) {
248 attempt += 1;
249 const pong = await pingContentScriptOnTab(tabId);
250 if (pong?.ok && (!pong.source || pong.source === source)) {
251 console.log(LOG_PREFIX, `[ensureContentScriptReadyOnTab] ready ${source} tab=${tabId} on attempt ${attempt} after ${Date.now() - start}ms`);
252 await registerTab(source, tabId);
253 return;
254 }
255
256 if (!inject || !inject.length) {
257 throw new Error(`${getSourceLabel(source)} 内容脚本未就绪,且未提供可用的注入文件。`);
258 }
259
260 const registry = await getTabRegistry();
261 if (registry[source]) {
262 registry[source].ready = false;
263 await setState({ tabRegistry: registry });
264 }
265
266 try {
267 if (injectSource) {
268 await chrome.scripting.executeScript({
269 target: { tabId },
270 func: (injectedSource) => {
271 window.__MULTIPAGE_SOURCE = injectedSource;
272 },
273 args: [injectSource],
274 });
275 }
276
277 await chrome.scripting.executeScript({
278 target: { tabId },
279 files: inject,
280 });
281 } catch (err) {
282 lastError = err;
283 console.warn(LOG_PREFIX, `[ensureContentScriptReadyOnTab] inject attempt ${attempt} failed for ${source} tab=${tabId}: ${err?.message || err}`);
284 }
285

Callers 8

openSignupEntryTabFunction · 0.70
requestCpaOAuthUrlFunction · 0.70
requestSub2ApiOAuthUrlFunction · 0.70
executeStep2Function · 0.50
executeStep3Function · 0.50
executeCpaStep9Function · 0.50
executeSub2ApiStep9Function · 0.50

Calls 6

setStateFunction · 0.85
pingContentScriptOnTabFunction · 0.70
registerTabFunction · 0.70
getSourceLabelFunction · 0.70
getTabRegistryFunction · 0.70
addLogFunction · 0.70

Tested by

no test coverage detected