Firebase 未加载到 Chrome 扩展内容脚本中 [英] Firebase not getting loaded in chrome extension content script

查看:37
本文介绍了Firebase 未加载到 Chrome 扩展内容脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 chrome 扩展程序的内容脚本中加载 Firebase:

I'm trying to load Firebase inside my chrome extension's content script:

var script   = document.createElement("script")
script.type  = "text/javascript"
script.src   = "https://cdn.firebase.com/js/client/1.1.2/firebase.js"
document.body.appendChild(script)

firebaseUrl = 'https://foobar.firebaseio.com'
root = new Firebase(firebaseUrl)

但我收到错误消息:

Uncaught ReferenceError: Firebase is not defined 

我做错了什么?

推荐答案

您正在做的是使用 document.body.append(...) 注入 firebase 库.然后,您尝试从扩展程序内部访问 firebase 库,但 chrome 扩展程序被沙盒化,远离网页.您将 firebase 库注入到网页中,因此您的扩展程序无法直接访问它.您可以通过以下两种方式之一访问它:

What you are doing is injecting the firebase library by using document.body.append(...). Then, you are trying to access the firebase library from inside your extension, but chrome extensions are sandboxed away from the web page. You injected the firebase library into the web page, so it is not directly accessible to your extension. You can access it one of two ways:

  1. 将您想要与 firebase 交互的代码注入到网页中.它将能够与注入的 Firebase 代码进行交互.

  1. Inject the code you want to interact with firebase into the web page. It will be able to interact with the injected firebase code.

下载 firebase 库 javascript,并将 firebase 代码添加到您的 chrome 扩展清单.您必须下载它并将其与扩展程序的其余代码库一起打包.然后可以直接在您的扩展代码中使用它.

Download the firebase library javascript, and add the firebase code to your chrome extension manifest. You will have to download it and package it with the rest of your extension's codebase. It then can be used directly in your extension code.

我个人推荐 2.将所有代码保存在 chrome 沙箱中并且不向用户公开任何 firebase 代码(因为他们可以检查网页并查看您注入的代码)或网站更安全.如果您需要使用任何秘密或私钥连接到 Firebase,则尤其如此.

I personally recommend 2. It is more secure to keep all of the code inside the chrome sandbox and not expose any firebase code to the user (as they could inspect the web page and view your injected code) or website. This is especially true if you need to use any secret or private keys to connect to firebase.

这篇关于Firebase 未加载到 Chrome 扩展内容脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆