WXT 1 - 跨浏览器的插件侧板控制
webextension-polyfill
查阅 wxt 相关 README
1
pnpm i @wxt-dev/webextension-polyfill webextension-polyfill
以及在
wxt.config.ts中加入:1
2
3
4
5export default defineConfig({
//...
modules: ["@wxt-dev/webextension-polyfill"]
//...
});此外,还需要
@types/webextension-polyfill以及@types/firefox-webext-browser来扩展browser的 Firefox 成员。1
pnpm i -D @types/firefox-webext-browser
括号作为行开头


- 这就是 JavaScript ASI 吗,给我整笑了。
默认打开 Side Panel
Firefox
经测试,
manifest.json中的sidebar_action或者side_panel事实上没有任何作用。也可能是 WXT 抹平了这其中的一些坑,没有细究。
对于 Firefox,点击扩展图标触发 listener 函数的机制和
default_popup是 冲突 的,这意味着如果浏览器发现 manifest 中的action一项中存在default_popup,那么点击扩展图标只会打开 popup 窗口而不会触发background.ts中挂载的 listener 函数。因此,想要默认打开侧板,需要删除
entrypoints中的popup来防止 wxt 自动在manifest.json中生成default_popup。也可以通过一种奇怪的方式来让 WXT 忽略
popup:
但是不能完全删去
action,因为这样一来background.ts就无法访问browser.browserAction或者browser.action。https://github.com/wxt-dev/wxt/issues/669
https://wxt.dev/guide/essentials/config/manifest.html#action-without-popup可以在
wxt.config.ts中的manifest部分添加一个空的action,如下所示:1
2
3
4
5
6
7
8
9export default defineConfig({
// ...
manifest: {
// ...
action: {}
// ...
}
// ...
});
然后在
background.ts中注册监听函数:1
2
3
4
5if (import.meta.env.MANIFEST_VERSION === 3) {
browser.action.onClicked.addListener(sidePanelManager.open);
} else {
browser.browserAction.onClicked.addListener(sidePanelManager.open);
}
Chrome
同样的,需要
manifest.json中包含action,且需要删除popup:https://developer.chrome.com/docs/extensions/reference/api/sidePanel#open-action-icon
经测试,当
PanelBehavior中的openPanelOnActionClick被设为true的时候,点击扩展图标同样不会触发background.ts中挂载的 listener 函数。openPanelOnActionClick默认设置为false,保险起见再手动设置一遍:1
2
3
4
5
6
7// Chrome specific: Disable the default declarative behavior to ensure
// the onClicked event is dispatched to our listener.
if (import.meta.env.CHROME && typeof chrome.sidePanel?.setPanelBehavior === "function") {
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: false }).catch((error) => {
console.error("[Chrome] Error resetting panel behavior: ", error);
});
}
完整代码
@/utils/sidepanel.ts
1 | import { Tabs } from "webextension-polyfill"; |
- Title: WXT 1 - 跨浏览器的插件侧板控制
- Author: Last
- Created at : 2026-01-30 17:08:26
- Link: https://blog.imlast.top/2026/01/30/wxt-1/
- License: This work is licensed under CC BY-NC-SA 4.0.