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