Firefox 浏览器扩展内容脚本中的 Firebase Firestore 访问失败

Firebase Firestore access within Firefox browser extension content script fails

我有一个浏览器扩展,它试图从内容脚本中与 Firestore 数据库交互。在 Chrome 中,此功能完美无误,但在 Firefox 中,它失败并出现神秘错误:

@firebase/firestore: Firestore (8.8.1): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unavailable]: The operation could not be completed
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

查看 firefox/chrome devtools 中的网络选项卡,很明显没有向 Firestore 后端发出请求,这解释了为什么会抛出此错误,但我无法弄清楚为什么会这样。

我无法确定 chrome 和 firefox 内容脚本之间网络请求处理之间的任何明显差异。

我还应该注意,当我尝试从 Firebase 后台页面访问 firestore 时,它​​按预期工作,只是在内容脚本中有问题。

可以找到一个最小的复制品here & the code that is being run in the content script can be found here

它在 Chrome 中工作但在 Firefox 上失败的原因是因为它们有 different policies for how they treat network requests inside content scripts.

在 Chrome 中,内容脚本请求发生​​在页面上下文中。

在 Firefox 中,请求发生在扩展的上下文中。

这意味着您需要在 manifest.json 内的 permissions 键中添加您要向其发出请求的站点,才能使请求成功。