关于windows.create弹窗定位、非模态设置及alwaysOnTop异常的技术咨询
Hey there, let's unpack your two questions with what I know about browser extension window APIs, especially since you're migrating an older Chrome project:
1. Why windows.create() top/left properties fail to position the window (it stays centered)
This boils down to modern Chromium browser (Chrome/Edge/etc.) behavior changes and built-in restrictions, especially when creating windows with type: "popup":
- Popup type constraints: Chromium prioritizes user experience for popup windows by automatically centering small-sized popups, ignoring explicit
top/leftvalues. This is a deliberate change from older Chrome versions where popups allowed free positioning. - Window state defaults: If you don't explicitly set
state: "normal", the browser might fall back to default popup positioning rules that override your coordinates. - Screen visibility checks: The browser will adjust your
top/leftvalues if the window would be partially or fully off-screen, which can sometimes result in centering as a fallback.
To make top/left work as expected, try switching the window type to "normal" (instead of "popup") and explicitly set state: "normal", along with a reasonable window size (avoid tiny dimensions that trigger auto-centering). Example:
browser.windows.create({ url: "your-popup.html", type: "normal", state: "normal", top: 150, left: 200, width: 450, height: 350 });
2. Can the popup be non-modal, and why does alwaysOnTop return false but the window stays on top?
Non-modal setup
Yes, you can make it non-modal, but you need to adjust the window type:
- Windows created with
type: "popup"have built-in modal-like behavior in modern Chromium—they're forced to stay above their parent context (like the extension's background page or the tab that triggered them). To get a non-modal window that can be covered by other apps/windows, usetype: "normal"instead. This matches the non-modal behavior you had in older Chrome versions.
The alwaysOnTop discrepancy
The reason alwaysOnTop returns false but the window stays on top is because:
- For
type: "popup"windows, Chromium enforces a "stays above parent" rule independently of thealwaysOnTopproperty. The API returnsfalsebecause your explicit setting is ignored by the browser's internal popup handling logic. When you switch totype: "normal", thealwaysOnTopproperty will work as expected—set it tofalseand the window will no longer stay pinned on top.
Putting this together, here's a code snippet for a non-modal, positionable window:
const nonModalWindow = await browser.windows.create({ url: "your-content.html", type: "normal", state: "normal", top: 100, left: 100, width: 400, height: 300, alwaysOnTop: false }); // Now nonModalWindow.alwaysOnTop will correctly reflect your setting, and the window won't stay pinned
内容的提问来源于stack exchange,提问作者KnightWing

