在Firefox扩展中调用Javascript API时出错

时间:2018-03-09 15:35:28

标签: javascript firefox web-applications

我目前正在构建一个Web扩展,并且正处于浏览器之间移植的阶段。在从Chrome迁移到Firefox时,我遇到了使用runtime.sendMessage API的问题,该API允许我在内容和后台脚本之间进行通信。

内容脚本中的消息代码:

var img = saved-image;
browser.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
    if(request.greeting === "screenshot-please"){
      sendResponse({
        farewell: img
      });
    }       
  }
);

后台脚本:

{
  "description": "...",
  "manifest_version": 2,
  "name": "...",
  "permissions": [
    "find",
        "tabs",
        "activeTab",
        "<all_urls>"
    ],
    "applications": {
        "gecko": {
                 "id": "..."
        }
  },
  "background": {
    "scripts": ["/JavaScript/moz-initialPopup.js"]
    },
    "content_scripts": [
        {
      "all_frames": true,
            "matches": ["*://127.0.0.1:3000/*"],
            "js": ["Javascript/dataGrab.js"]
    }
  ],
  "icons": {
    "48": "..."
  },
  "browser_action": {
    "browser_style": true,
    "default_title": "...",
    "default_icon": {
        "48": "..."
    }
  },
  "version": "1.0"
}

和清单:

{{1}}

当使用chrome(用* chrome替换*浏览器的所有实例)时,此消息传递系统正常工作。在firefox中,在开发人员工具中我得到'ReferenceError:浏览器未定义'。

有人可以帮我解释为什么我无法在这里访问浏览器吗?

0 个答案:

没有答案
相关问题