ChemPal Documentation - v1.6.0
    Preparing search index...

    Function openExtensionTab

    • Opens the extension in a new, focused browser tab and closes the popup. The opened URL carries the ?view=tab param so isTabView resolves to true there. Falls back to window.open outside the extension runtime. If a tab is already open at that URL it is focused instead of duplicated.

      Returns Promise<void>

      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();
      }