antfu-collective / vitesse-webext

⚡️ WebExtension Vite Starter Template
MIT License
2.92k stars 225 forks source link

fix: auto import error in several pages #187

Closed imba97 closed 3 months ago

imba97 commented 4 months ago

环境

Windows 10 Node 18.18.2

描述

resolve (#171)

在所有 Content Scripts 中,browser 对象没有被正确导入

以下写法只在 Background Script 中生效

{
  'webextension-polyfill': [['*', 'browser']],
}

在 Options、Popup、Sidepanel 中使用 browser 会报错

function openOptionsPage() {
  console.log(browser.runtime)
  browser.runtime.openOptionsPage()
}

vite.config.content.mts 中需要改成

{
  'webextension-polyfill': [['default', 'browser']],
}


也许还有更好的办法?

tmkx commented 3 months ago

background works due to another reason: image

it also errors if we change the code to this:

- browser.runtime.onInstalled.addListener(() => {})
+ const b = browser
+ b.runtime.onInstalled.addListener(() => {})
imba97 commented 3 months ago

background works due to another reason: image

it also errors if we change the code to this:

- browser.runtime.onInstalled.addListener(() => {})
+ const b = browser
+ b.runtime.onInstalled.addListener(() => {})

有空我再看看,我感觉现在写的也有点违和感

tmkx commented 3 months ago

this is fixed in #188, so i'm going to close this pr, thanks for your work! 🫶