简体   繁体   English

将 Manifest Version 3 Chrome Extension 连接到 firestore 数据库

[英]Connect a Manifest Version 3 Chrome Extension to a firestore database

The Problem问题

I'm writing a chrome extension that users will use to fill forms using content hosted in a firestore database.我正在编写一个 chrome 扩展,用户将使用它来使用托管在 firestore 数据库中的内容来填写表单。 The core problem I'm facing is that Chrome Extension's Manifest Version 3 does not appear to support any of google's mechanisms for interacting with firestore.我面临的核心问题是 Chrome 扩展的清单版本 3 似乎不支持任何谷歌与 firestore 交互的机制。

Previously, with Manifest Version 2, you'd link to the CDN hosted firebase scripts in your background.html file like so:以前,使用 Manifest 版本 2,您可以在 background.html 文件中链接到 CDN 托管的 firebase 脚本,如下所示:

<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-firestore.js"></script>

With MV3, remotely hosted code is no longer allowed for security reasons, despite the fact that the script I'm trying to link to is google's, and attempting to do so throws errors as soon as you load the unpacked extension.使用 MV3,出于安全原因, 不再允许远程托管代码,尽管我尝试链接到的脚本是 google 的,并且尝试这样做会在您加载解压缩的扩展程序时立即引发错误。

My question:我的问题:

How can I connect a Manifest Version 3 Chrome Extension to a firestore database?如何将 Manifest Version 3 Chrome Extension 连接到 firestore 数据库?

What I've tried我试过的

I spent quite a lot of time experimenting with pulling the cdn scripts locally and then importing them directly into my background.js like so:我花了很多时间尝试在本地提取 cdn 脚本,然后将它们直接导入到我的background.js中,如下所示:

try {
    importScripts('firebase/firebase-firestore.js');
} catch (error) {
    console.log(error);
}

This approach led me deep down a rabbit hole because this also did not work but apparently for different reasons.这种方法使我陷入了深深的困境,因为这也不起作用,但显然是出于不同的原因。 Specifically, with MV3, XMLHttpRequest is not supported in a service worker.具体来说,对于 MV3,服务工作者不支持 XMLHttpRequest。

Possible next steps可能的后续步骤

From the reading I've done, and I really hope somebody here can point me to something I've missed, it appears that it will not be possible to connect firestore to an extension directly.从我所做的阅读来看,我真的希望这里有人能指出我错过的东西,似乎无法将 firestore 直接连接到扩展。 I have read elsewhere that the firebase realtime data base can be accessed via a MV3 chrome extension.我在别处读到可以通过 MV3 chrome 扩展访问 firebase 实时数据库。

Setting up some link between firestore and the realtime database for the relevant content our users need to be able to access with the chrome extension and then connecting the realtime database to the extension seems like it is the next step.为我们的用户需要能够使用 chrome 扩展程序访问的相关内容在 firestore 和实时数据库之间建立一些链接,然后将实时数据库连接到扩展程序似乎是下一步。 Is this viable?这可行吗?

Why not use Manifest Version 2?为什么不使用清单版本 2?

Simply to future proof our app, we don't want to be left scrambling when MV2 is deprecated.只是为了将来证明我们的应用程序,当 MV2 被弃用时,我们不希望陷入混乱。 While no deprecation date for MV2 appears on any official channel, the chromium blog says :虽然任何官方频道上都没有出现 MV2 的弃用日期, 但 Chromium 博客说

"While there is not an exact date for removing support for Manifest V2 extensions, developers can expect the migration period to last at least a year from when Manifest V3 lands in the stable channel." “虽然没有取消对 Manifest V2 扩展支持的确切日期,但开发人员可以预期迁移期至少会持续一年,从 Manifest V3 登陆稳定通道开始。”

Which puts MV2's end of life sometime early in 2022.这使得 MV2 的生命周期在 2022 年初的某个时候结束。

Edit to add:编辑添加:

Theyannounced some hard dates , January 2022 they stop accepting new MV2 extensions but maintenance of those already existing may continue until January 2023 when all extensions must be MV3.他们宣布了一些硬性日期,2022 年 1 月他们停止接受新的 MV2 扩展,但对现有扩展的维护可能会持续到 2023 年 1 月,届时所有扩展都必须是 MV3。

I've just seen this question because I'm just doing an extension with manifest v3 and firebase and I've been with the same problem for about two days but I think I've finally reach a solution.我刚刚看到这个问题,因为我只是在使用 manifest v3 和 firebase 进行扩展,而且我已经遇到同样的问题大约两天了,但我想我终于找到了解决方案。

Import the firebase files导入 firebase 文件

First of all there's the problem of manifest v3 not accepting extern files.首先是 manifest v3 不接受外部文件的问题。 In their doc they say that you can use npm install firebase and follow their doc in order to connect it but this is still broken.在他们的文档中,他们说你可以使用 npm install firebase 并按照他们的文档来连接它,但这仍然是坏的。 What I've done is download the files from the urls of firebase and create a folder with all of them inside.我所做的是从firebase的url下载文件并创建一个包含所有文件的文件夹。 Those URLS have this pattern:这些 URL 具有以下模式:

https://www.gstatic.com/firebasejs/VERSION/firebase-SERVICE.js --> (where VERSION is the SDK version such as 9.6.4 and SERVICE is an SDK name such as firebase-firestore) https://www.gstatic.com/firebasejs/VERSION/firebase-SERVICE.js -->(其中 VERSION 是 SDK 版本,例如 9.6.4,SERVICE 是 SDK 名称,例如 firebase-firestore)

All these files can be in a folder just like this:所有这些文件都可以像这样在一个文件夹中: 在此处输入图像描述

Connect to firebase Then you must create your background file.连接到 firebase然后你必须创建你的背景文件。 In my case I've called it firebase.js .就我而言,我称它为 firebase.js 。 In this file the first lines will be the imports在这个文件中,第一行将是导入

import { initializeApp } from "/firebase/firebase-app.js";
import { HERE YOU WRITE THE SERVICES YOU WANT } from '/firebase/firebase-auth.js';
import {HERE YOU WRITE THE SERVICES YOU WANT} from '/firebase/firebase-firestore.js';

As you can see you must use the relative path to the files.如您所见,您必须使用文件的相对路径。

One important thing you must know is that all service files of firebase depends on firebase-app.js so you must edit its imports to the relative path of the firebase-app.js in your project.您必须知道的一件重要事情是,firebase 的所有服务文件都依赖于 firebase-app.js,因此您必须将其导入编辑为项目中 firebase-app.js 的相对路径。

Then you must declare the firebase config:然后你必须声明firebase配置:

const firebaseConfig = {
    apiKey: "///////",
    authDomain: "///////",
    projectId: "///////",
    storageBucket: "///////",
    messagingSenderId: "///////",
    appId: "///////",
    measurementId: "///////"
};

(You can find this information in the project settings page of firebase) (您可以在firebase的项目设置页面中找到此信息)

Finally you must initalize your firebase app and all the services just like this (example with the auth and firestore services):最后,您必须像这样初始化您的 firebase 应用程序和所有服务(例如 auth 和 firestore 服务):

const firebase = initializeApp(firebaseConfig);
const auth = getAuth(firebase);
const db=getFirestore(firebase);

Adapt the manifest file调整清单文件

Because you are using imports, your background.js (firebase.js in my case) must be module so in your manifest.json file write the following:因为您正在使用导入,所以您的 background.js(在我的情况下为 firebase.js)必须是模块,因此在您的 manifest.json 文件中写入以下内容:

"background": {
    "service_worker": "firebase.js",
    "type": "module"
  }

The next steps depends of the service you want to use.接下来的步骤取决于您要使用的服务。 From here the documentation is good enough.从这里开始,文档就足够好了。

Finally one quick warning.最后一个快速警告。 Because of how manifest v3 works I think some services don't work at all but I'm not quite sure.由于 manifest v3 的工作方式,我认为某些服务根本不起作用,但我不太确定。

Hope this helps you or anyone with this problem :D希望这可以帮助您或遇到此问题的任何人:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 消息传递(chrome.runtime.connect())到扩展失败,chrome清单版本2 - message passing (chrome.runtime.connect()) to extension fails, chrome manifest version 2 Chrome扩展程序与背景页面不使用清单版本2 - Chrome Extension With Background Page Not Working With Manifest Version 2 Paypal Google Chrome 扩展清单版本 3 的按钮 - Paypal Button for Google Chrome Extension Manifest Version 3 如何在Manifest版本2中使用Backbone.js模板进行Chrome扩展 - How to use Templates with Backbone.js for Chrome Extension in Manifest Version 2 chrome.extension.getBackgroundPage() 不适用于 manifest_version: 3 - chrome.extension.getBackgroundPage() doesn't work with manifest_version: 3 chrome 扩展清单版本 3 中的点击事件有问题 - Having issue with click event in chrome extension manifest version 3 地理位置权限不适用于 Manifest 版本 3 Chrome 扩展 - geolocation permission doesn't work with Manifest Version 3 Chrome Extension Chrome Extension Manifest权限 - Chrome Extension Manifest permissions 将Chrome扩展程序更改为manifest_version 2需要做些什么? 我的扩展程序只是在弹出窗口中打开一个URL。 - What needs to be done for changing a Chrome Extension to manifest_version 2? My extension simply opens a url in a popup. 在Chrome扩展程序清单中排除匹配项 - Exclude matches in Chrome extension manifest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM