Firebase在webextension

时间:2017-08-25 11:08:54

标签: javascript firebase firefox firefox-webextensions

我们的浏览器扩展程序的用户通过firebase登录。令牌Firebase问题持续1小时,但Firebase SDK中的User对象透明地处理令牌刷新。这就是为什么要在我的插件中加入Firebase SDK。

在webextension的上下文中加载Firebase js SDK在Firefox中不起作用。它适用于Chrome。我已经创建了一个测试存储库,它将测试用户记录到测试应用程序here中。

点击应用程序按钮后,应用程序将尝试登录用户。

使用Firefox 55.0.2(Linux 64位)时,在单击按钮之前会出现以下错误(仅通过加载插件):

[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 52: SyntaxError: test for equality (==) mistyped as assignment (=)?
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 57: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 57: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 82: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 101: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 106: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 107: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 125: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 177: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 272: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 274: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 396: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 412: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 471: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 475: SyntaxError: test for equality (==) mistyped as assignment (=)?
JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 477: SyntaxError: test for equality (==) mistyped as assignment (=)?
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 28: ReferenceError: reference to undefined property "name_"
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 285: ReferenceError: reference to undefined property "a"
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 28: ReferenceError: reference to undefined property "Auth"

然后点击按钮后:

 [firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 279: ReferenceError: reference to undefined property "currentUser"
 [firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 59: ReferenceError: reference to undefined property "closure_lm_434254"
 [firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 54: ReferenceError: reference to undefined property "storage"
 [firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 176: ReferenceError: reference to undefined property "Va"
 [firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 176: ReferenceError: reference to undefined property "Oe"
 [firefox/index.js][debug] Firefox stderr: JavaScript strict warning: moz-extension://ec7f4b77-3e2d-48c6-8e27-bb75d93980d1/firebase.js, line 91: ReferenceError: reference to undefined property "readystatechange"

此外,控制台向www.googleapis.com显示阻止的CORS请求 (注意此域名在manifest.json中的CPS指令中列入白名单)和a Firebase错误auth/network-request-failed

详细说明见自述文件。

我的问题是:Firebase不能与Firefox一起使用,或者我可以做些什么来使其工作?特别是,我错过了manifest.json或我需要先通过Babel管道Firebase SDK,因为SpiderMonkey不了解V8的一些JS吗?

对于记录,使用Firebase在网页中加载Firebase可以毫不费力地工作。

编辑:MWE如下:

manifest.json

{
  "description": "Firebase & Firefox test case",
  "homepage_url": "https://github.com/adimit/test-firebase",
  "manifest_version": 2,
  "name": "firebase-test-case",
  "version": "1.0.0",

  "icons": {
    "64": "icons/logo-64.png"
  },

  "browser_action": {
    "browser_style": true,

    "default_icon": {
      "64": "icons/logo-64.png"
    }
  },

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

  "content_security_policy": "script-src 'self' 'unsafe-eval' https://www.gstatic.com/ https://*.firebaseio.com https://www.googleapis.com; object-src 'self'",

  "permissions": [
    "tabs",
    "storage"
  ]
}

background.js

const clickButton = () => {
  console.log("firebase object", firebase);

  var config = {
    apiKey: "AIzaSyABe5Ge-PSeZzfZU2EhllUI-w3fMb1nci8",
    authDomain: "webext-firefox.firebaseapp.com",
    databaseURL: "https://webext-firefox.firebaseio.com",
    projectId: "webext-firefox",
    storageBucket: "",
    messagingSenderId: "909758240321"
  };
  firebase.initializeApp(config);

  const user = {
    id: "no.email@example.com",
    password: "test-password"
  };

  firebase.auth().signInWithEmailAndPassword(
    user.id,
    user.password
  ).then(
    user => {
      console.log("user", user);
    },
    error => {
      console.log("error", error);
    }
  );
};

chrome.browserAction.onClicked.addListener(clickButton);

您必须从CDN复制firebase。像curl -LO "https://www.gstatic.com/firebasejs/4.0.0/firebase.js"这样的东西应该这样做。

虽然我建议你想尝试一下,但是你使用我上面链接的回复。

1 个答案:

答案 0 :(得分:1)

Chrome似乎允许隐式地通过插件拨打www.googleapis.com,但是,Firefox并没有。

Firefox报告的JavaScript严格错误似乎不会对运行时行为产生负面影响。

https://www.googleapis.com/*添加到manifest.json:permissions可解决Firefox无法进行身份验证的问题。