从Chrome应用使用Firebase时出错 [英] Error using Firebase from Chrome App

查看:178
本文介绍了从Chrome应用使用Firebase时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从Chrome应用程序(而不是扩展程序)中使用Firebase,并且无法解决以下错误:


拒绝加载脚本
'https:// {my firebase id} .firebaseio.com / .lp?start = t& ser = 81980096& cb = 15& v = 5'
因为它违反了以下内容安全策略指令:
default-srcself'chrome-extension-resource:。请注意,
'script-src'没有明确设置,所以'default-src'被用作
的后备。


我的manifest.json文件如下所示:

  {
manifest_version:2,
name:Simple Demo,
version:1,
icons:{
128:icon_128.png
},

permissions:[https://*.firebaseio.com/],
app:{
background:{
scripts:[background.js]
}
},
minimum_chrome_version:28
}

我有chrome应用程序的本地firebase.js脚本,问题似乎是它试图加载不允许的其他脚本。 / p>

任何想法都是可以理解的。 解释方案

REST API方法,在这种情况下,我用我的Chrome应用程序,它的工作正常。这不需要添加firebase SDK!



请阅读以下文档:
https://firebase.googleblog.com/2014/03/announcing-streaming-for-firebase-rest.html



https://firebase.google.com/docs/reference/rest/database/



https://firebase.google.com/docs/database/rest/retrieve-data



方法很简单



协议被称为,您在哪里听特定的服务器的URL,当有什么改变,你得到的事件回调

>在这种情况下,firebase关闭icially提供基于SSE的机制

示例代码片段是

$ p $ // firebaseUrl会像https://xxxxx.firebaseio.com/yourNode.json或https://xxxxx.firebaseio.com/yourNode/.json
//注意URL末尾的.json ,无论您想要监听的节点,添加.json作为后缀
var firebaseEventSS = new EventSource(firebaseUrl);
//你将得到数据
的回调firebaseEventSS.addEventListener(patch,function(e){
var data = e.data;
},假);
firebaseEventSS.addEventListener(put,function(e){
var data = e.data;
},false);

只要查看 => EventSource示例,并与Firebase Doc for REST API结合使用,您就全都设置好了!

完成后不要忘记关闭()。

  firebaseEventSS.close(); 


I am attempting to use Firebase from within a Chrome App (not an extension) and have not been able to work around the following error:

Refused to load the script 'https://{my firebase id}.firebaseio.com/.lp?start=t&ser=81980096&cb=15&v=5' because it violates the following Content Security Policy directive: "default-src 'self' chrome-extension-resource:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

my manifest.json file looks like this:

{
  "manifest_version": 2,
  "name": "Simple Demo",
  "version": "1",
  "icons": {
    "128": "icon_128.png"
  },

  "permissions": ["https://*.firebaseio.com/"],
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "minimum_chrome_version": "28"
}

I have the firebase.js script local to the chrome app, the issue seems to be that it tries to load other scripts which isn't permitted.

Any ideas are appreciated.

解决方案

Well, You can use firebase REST API Approach, in this case, I've used for my chrome app and its working fine. This don't require to have firebase SDK to be added!

Read the following docs, https://firebase.googleblog.com/2014/03/announcing-streaming-for-firebase-rest.html

https://firebase.google.com/docs/reference/rest/database/

https://firebase.google.com/docs/database/rest/retrieve-data

Approach is very simple

Protocol is known as SSE (Server Sent Events) where you listen to specific server URL and when something changes, you get the event callback.

In this case, firebase officially provides SSE based mechanism

A sample code snippet is,

 //firebaseUrl will be like https://xxxxx.firebaseio.com/yourNode.json or https://xxxxx.firebaseio.com/yourNode/.json
 //notice ".json" at the end of URL, whatever node you want to listen, add .json as suffix
    var firebaseEventSS = new EventSource(firebaseUrl);
    //and you will have callbacks where you get the data
    firebaseEventSS.addEventListener("patch", function (e) {
                var data = e.data;
            }, false);
    firebaseEventSS.addEventListener("put", function (e) {
                var data = e.data;
            }, false);

Just check few EventSource examples and combine with Firebase Doc for REST API and you are all set!

Don't forget to close() when it's done.

firebaseEventSS.close();

这篇关于从Chrome应用使用Firebase时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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