使用 firefox 扩展连接 firebase 实时数据库时出现未捕获的引用错误

问题描述 投票:0回答:1

我想创建一个连接到 firebase 的 firefox 扩展,但每次我将导入添加到 backgroud.js 脚本时,我都会收到此错误:

未捕获的引用错误:firebase 未定义或未捕获的语法错误:导入声明只能出现在模块的顶层

manifest.json

{
    "manifest_version": 2,
    "name": "Firebase Connection Test",
    "version": "1.0",
    "permissions": [
      "storage"
    ],
    "background": {
      "scripts": ["background.js"],
      "persistent": false
    }
  }
  

背景.js:

// Import the functions you need from the Firebase SDKs
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.24.0/firebase-app.js";
import { getDatabase, ref, get } from "https://www.gstatic.com/firebasejs/9.24.0/firebase-database.js";

// Firebase configuration
const firebaseConfig = {
    apiKey: "YOUR_API_KEY", // Replace with your API key
    authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
    databaseURL: "https://YOUR_PROJECT_ID.firebaseio.com", // Replace with your Database URL
    projectId: "YOUR_PROJECT_ID",
    storageBucket: "YOUR_PROJECT_ID.appspot.com",
    messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
    appId: "YOUR_APP_ID"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Reference to your database
const database = firebase.database();

// Check the connection
database.ref('.info/connected').once('value').then((snapshot) => {
    if (snapshot.val() === true) {
        console.log('Connected to Firebase Realtime Database');
    } else {
        console.log('Not connected to Firebase');
    }
});

有什么方法可以在 firefox 扩展中连接到 firebase 吗?

firebase firefox firefox-addon manifest.json
1个回答
0
投票

后台脚本的默认设置是常规 JavaScript 文件。要使 Firefox 将它们视为 ESM 模块,请将

"type": "module"
添加到
background
节点:

"background": {
  "scripts": ["background.js"],
  "type": "module",
  "persistent": false
}

来源:https://discourse.mozilla.org/t/webextension-import-a-module-in-a-script/17381/11

© www.soinside.com 2019 - 2024. All rights reserved.