Home > other >  How to keep the Chrome extension window open after I click a link?
How to keep the Chrome extension window open after I click a link?

Time:10-16

I'm having this issue with a simple extension I'm working on.

The extension consists of a bunch of links. When I click a single link, the extension closes. I would like to keep the extension window open so I can open more links before closing the extension/clicking on the page to continue working.

Is there a way I can keep the extension open once a single link is clicked and only close it when I click on the page/lose focus?

Thanks.

CodePudding user response:

Switching to another tab in the same window closes the popup because the popup is conceptually bound to the tab where it was invoked. When you open a URL it opens in a new active tab by default ("active" means it's currently in foreground), the old tab loses its active status, its popup closes.

The solution is to open the links in an inactive tab i.e. in background.

document.addEventListener('click', evt => {
  const a = evt.target.closest('a[href]');
  if (a) {
    evt.preventDefault();
    chrome.tabs.create({url: a.href, active: false});
  }
});

Alternatively, open the links in a new window using chrome.windows.create.

  • Related