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

Function reuseOrCreateTab

background/tab-runtime.js:405–526  ·  view source on GitHub ↗
(source, url, options = {})

Source from the content-addressed store, hash-verified

403 }
404
405 async function reuseOrCreateTab(source, url, options = {}) {
406 const alive = await isTabAlive(source);
407 if (alive) {
408 const tabId = await getTabId(source);
409 await closeConflictingTabsForSource(source, url, { excludeTabIds: [tabId] });
410 const currentTab = await chrome.tabs.get(tabId);
411 const sameUrl = currentTab.url === url;
412 const shouldReloadOnReuse = sameUrl && options.reloadIfSameUrl;
413
414 const registry = await getTabRegistry();
415 if (sameUrl) {
416 await chrome.tabs.update(tabId, { active: true });
417 if (shouldReloadOnReuse) {
418 if (registry[source]) registry[source].ready = false;
419 await setState({ tabRegistry: registry });
420 await chrome.tabs.reload(tabId);
421 await new Promise((resolve) => {
422 const timer = setTimeout(() => { chrome.tabs.onUpdated.removeListener(listener); resolve(); }, 30000);
423 const listener = (tid, info) => {
424 if (tid === tabId && info.status === 'complete') {
425 chrome.tabs.onUpdated.removeListener(listener);
426 clearTimeout(timer);
427 resolve();
428 }
429 };
430 chrome.tabs.onUpdated.addListener(listener);
431 });
432 }
433
434 if (options.inject) {
435 if (registry[source]) registry[source].ready = false;
436 await setState({ tabRegistry: registry });
437 if (options.injectSource) {
438 await chrome.scripting.executeScript({
439 target: { tabId },
440 func: (injectedSource) => {
441 window.__MULTIPAGE_SOURCE = injectedSource;
442 },
443 args: [options.injectSource],
444 });
445 }
446 await chrome.scripting.executeScript({
447 target: { tabId },
448 files: options.inject,
449 });
450 await new Promise((resolve) => setTimeout(resolve, 500));
451 }
452
453 await rememberSourceLastUrl(source, url);
454 return tabId;
455 }
456
457 if (registry[source]) registry[source].ready = false;
458 await setState({ tabRegistry: registry });
459 await chrome.tabs.update(tabId, { url, active: true });
460
461 await new Promise((resolve) => {
462 const timer = setTimeout(() => { chrome.tabs.onUpdated.removeListener(listener); resolve(); }, 30000);

Callers 9

fetchDuckEmailFunction · 0.70
openSignupEntryTabFunction · 0.70
executeStep6Function · 0.50
executeStep8Function · 0.50
executeStep4Function · 0.50
runStep7AttemptFunction · 0.50
executeCpaStep9Function · 0.50
executeSub2ApiStep9Function · 0.50

Calls 6

setStateFunction · 0.85
isTabAliveFunction · 0.70
getTabIdFunction · 0.70
getTabRegistryFunction · 0.70
rememberSourceLastUrlFunction · 0.70

Tested by

no test coverage detected