Addon problem #5: Cannot close install or update window

The documentation for browser.windows.remove() (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows/remove) explains how to remove a window created from a background script in response to the user installing or updating an extension.

However, it does not show an example and my attempts to close an update window in response to its Cancel button fail. I can close the window from the background script, but that does not work for me because I cannot figure out how to send a command message from the update window to the background script.

I use the following code (determined by experiment) to obtain the window id integer when creating it:

browser.windows.create({url:url, type: “detached_panel”}).then(E2);
function E2(info)
{
g.uwid=info.id;
} // E2

I use a listener to detect when the Cancel button in the update window is clicked.

Can anyone provide a tested answer? I’ll update the documentation.

Stuff like this baffles me.

I added a second example that should do exactly what you want:

Close the current, e.g. popup, window when the user clicks a button on the page:

// in a script loaded by the page in the window
document.querySelector('#close').addEventListener(async ({ button, }) => { try {
  if (button) return; // not a left click
  const windowId = (await browser.windows.getCurrent()).id;
  await browser.windows.remove(windowId);
  // this point will never be reached, since the window is gone
} catch (error) { console.error('Closing failed:', error); } });

See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows/remove#Examples

Thank you for the useful help. My code works now and looks like this:

GetEl('cancel').addEventListener('click',function(e)
	{
	browser.windows.getCurrent().then(F2);
	});
function F2(info)
	{
	return browser.windows.remove(info.id);
	} // F2