在JS中将Firebase添加到Chrome扩展程序时出错

时间:2019-04-07 13:58:20

标签: javascript html firebase firebase-realtime-database google-chrome-extension

我无法使Firebase在JS中工作,特别是在扩展上工作。

这是我的Manifest.json

{
"manifest_version": 2,     //required
"version": "1.0",               //required
"name": "Hello World!",   //required
"description": "Learning how to make a chrome extension!",
"icons": {
    	"16": "images/puppy16.png",
    	"48": "images/puppy48.png",
    	"128": "images/puppy.png"
    },
"browser_action":
	{
	"default_icon": "images/puppy.png",
    "default_popup": "popup.html"
	}, 
"background": {
    "scripts": ["background.js"]
  },
  "content_security_policy": "script-src 'self' https://cdn.firebase.com https://*.firebaseio.com; object-src 'self'"
  "permissions": [
      "identity",
      "tabs",
       "activeTab",
        "http://*/*",
        "https://*/*"
  ]
}

这是我的background.js文件

 // Initialize Firebase
  var config = {
    apiKey: "xxxxxx",
    authDomain: "xxx.firebaseapp.com",
    databaseURL: "xxxx",
    projectId: "xxx",
    storageBucket: "xxx",
    messagingSenderId: "xxx"
  };
  firebase.initializeApp(config);
  
var rootRef = firebase.database().ref();

function writeUserData(userId, name, email, imageUrl) {
  rootRef.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });
}
writeUserData("11", "pete", "p@g.com", "google.com")

我特别收到一条错误,指出:未捕获的ReferenceError:未定义firebase。

问题可能出在外部库中吗?

1 个答案:

答案 0 :(得分:1)

您尚未以任何方式导入firebase SDK。仅将它们放在同一个文件夹中不足以让您的文件了解Firebase。在background.js脚本之前,将firebase SDK js文件添加到清单的脚本部分。假设它名为firebase.js,您的清单中应该包含以下行:

"scripts": ["firebase.js", "background.js"]