我想创建一个连接到 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 吗?
后台脚本的默认设置是常规 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