【问题标题】:Error using Firebase from Chrome App从 Chrome 应用程序使用 Firebase 时出错
【发布时间】:2015-06-06 14:52:17
【问题描述】:

我正在尝试在 Chrome 应用(不是扩展程序)中使用 Firebase,但无法解决以下错误:

拒绝加载脚本 'https://{我的 firebase id}.firebaseio.com/.lp?start=t&ser=81980096&cb=15&v=5' 因为它违反了以下内容安全政策指令: “default-src 'self' 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 脚本,问题似乎是它试图加载其他不允许的脚本。

感谢任何想法。

【问题讨论】:

  • 您是否在清单中添加了内容安全策略{ "content_security_policy": "script-src 'self' https://cdn.firebase.com https://*.firebaseio.com; object-src 'self'" }
  • 我确实尝试过,只是再次尝试确定,没有帮助,认为该设置仅适用于 chrome 扩展,不适用于 chrome 应用程序。
  • 好的,我明白了。我认为您需要下载脚本并将其放入 Chrome App 文件夹,而不是直接从网络加载脚本。
  • 这是我在本地拥有的 firebase.js 脚本,它正在尝试下载导致问题的其他脚本。
  • 你可能需要去the sandbox route。 (ExtJS 只是一个示例教程)

标签: firebase google-chrome-app


【解决方案1】:

好吧,您可以使用 firebase 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

方法很简单

协议被称为SSE (Server Sent Events),您可以在其中侦听特定的服务器 URL,当发生变化时,您会收到事件回调。

在这种情况下,firebase 官方提供了基于 SSE 的机制

示例代码 sn-p 是,

 //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);

只需检查几个 EventSource examples 并结合 Firebase Doc for REST API 就可以了!

完成后不要忘记 close()。

firebaseEventSS.close();

【讨论】:

    【解决方案2】:

    这是一个老问题,但 Firebase 团队提供的解决方案是为数据库使用不同的 URL 协议。

    所以不要使用https://<app>.firebaseio.com/,而是使用wss://<app>.firebaseio.com/

    我找到了解决方案here。这里is an answer provided by the Firebase team on this subject

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 2018-02-11
      • 2018-07-04
      相关资源
      最近更新 更多