A promise that resolves once the tab has been requested.
// From a popup click handler:
await openExtensionTab();
// => focuses the existing index.html?view=tab tab, or opens one, then window.close()
export async function openExtensionTab(): Promise<void> {
const path = `index.html?${VIEW_PARAM}=${TAB_VIEW}`;
// Outside the extension runtime (e.g. dev preview): fall back to a plain tab.
if (typeof chrome?.tabs === 'undefined' || typeof chrome?.runtime?.getURL !== 'function') {
window.open(`/${path}`, '_blank');
return;
}
const url = chrome.runtime.getURL(path);
try {
const existing = await findExtensionTab(url);
if (existing?.id == null) {
await chrome.tabs.create({ url, active: true });
} else {
await chrome.tabs.update(existing.id, { active: true });
if (typeof chrome.windows !== 'undefined' && existing.windowId != null) {
await chrome.windows.update(existing.windowId, { focused: true });
}
}
} catch (error) {
console.error('Failed to open extension tab:', { error });
}
window.close();
}
Opens the extension in a new, focused browser tab and closes the popup. The opened URL carries the
?view=tabparam so isTabView resolves totruethere. Falls back towindow.openoutside the extension runtime. If a tab is already open at that URL it is focused instead of duplicated.