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

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

问题描述



pre $ 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)



但是我收到错误信息:

pre $ Uncaught ReferenceError:Firebase is not defined

我做错了什么?


您正在做的是使用 document.body.append(...)。然后,您正在尝试从扩展程序内部访问Firebase库,但Chrome扩展程序已远离网页进行沙盒处理。您将Firebase库注入到网页中,因此不能直接访问您的扩展。您可以通过以下两种方法之一访问它:


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

  2. 下载firebase库javascript,并将firebase代码添加到您的Chrome扩展清单。您将不得不下载并将其与您的其他扩展程序的代码库一起打包。然后它可以直接在你的扩展代码中使用。

我个人推荐2.保持所有的Chrome沙箱内的代码,而不是向用户展示任何firebase代码(因为他们可以检查网页,查看您注入的代码)或网站。如果您需要使用任何密钥或私钥连接到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)

But I get the error message:

Uncaught ReferenceError: Firebase is not defined 

What am I doing wrong?

解决方案

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. Inject the code you want to interact with firebase into the web page. It will be able to interact with the injected firebase code.

  2. 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.

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天全站免登陆