This is what i do in my extension and it works perfect with me
this utility open a window and await until it's closed, you can also export the popup variable if you need it in your code, also you can change the height and width logic to be passed as params.
import { Tabs, Windows } from "webextension-polyfill";
import WindowType = Tabs.WindowType;
let popup: Windows.Window | undefined;
let popupRequestId: string | undefined;
const closePopup = async () => {
// try to close the popup if possible
try {
if (popup?.id) {
await windows.remove(popup.id);
}
} catch {
// ignore
}
};
export const openPopup = async (type: WindowType = "popup", requestId: string = "") => {
const lastFocused = await windows.getLastFocused();
const { top = 0, left = 0, width = 0 } = lastFocused;
if (popupRequestId === requestId) {
if (popup?.id) {
await windows.update(popup.id, {
focused: true,
drawAttention: true,
});
}
} else {
popupRequestId = requestId;
if (popup) {
await closePopup();
}
popup = await windows.create({
type,
url: `./index.html?requestId=${requestId}`,
width: 360,
height: 628, // +28 (the toolbar height)
focused: true,
left: Math.max(left + width - 360 - 20, 0),
top: top + 20,
});
return new Promise(resolve => {
const onRemoved = (windowId: number) => {
if (popup && windowId === popup.id) {
popup = undefined;
popupRequestId = undefined;
windows.onRemoved.removeListener(onRemoved);
}
resolve(true);
};
windows.onRemoved.addListener(onRemoved);
});
}
};
here is how to use it:
const test = async () => {
await openPopup("panel", "unique id");
// popup is closed :)
};