CodingIsFun33
CodingIsFun33

Reputation: 453

Force close chrome extension popup from background script

So I have a background script and I am trying to force close the extension popup programmatically from the background script.

I tried doing

window.close();

Which won't work since that would have to be ran from an extension page not a background script.

Then I tried doing

chrome.tabs.update({ active: true });

which is supposed to switch the focus to the current tab which should then close the extension window, but that does not seem to work either.

Anyone have any ideas?

Upvotes: 1

Views: 1641

Answers (1)

woxxom
woxxom

Reputation: 73856

You can use chrome.extension.getViews to close all opened popups:

chrome.extension.getViews({type: 'popup'}).forEach(v => v.close());

...or just the one in the focused window:

chrome.windows.getLastFocused(w => {
  chrome.extension.getViews({type: 'popup', windowId: w.id}).forEach(v => v.close());
});

Alternatively you can use messaging via chrome.runtime.sendMessage to send a message like 'closePopup' so the popup receives it and closes itself using window.close() in its onMessage handler.

Upvotes: 2

Related Questions