0

我正在尝试按照此处的文档将我的后台脚本中的变量传递给我的内容脚本。

http://code.google.com/chrome/extensions/messaging.html

显现:

{
  "name": "name",
  "description": "desc",
  "version": "1.0",
  "manifest_version": 2,
  "content_scripts": [
    {
      "matches": [ "http://*/*", "https://*/*" ],
      "js": ["content.js"]
    }
  ],
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "tabs",
    "http://*/*",
    "https://*/*"
      ],
  "options_page": "options.html",
  "browser_action":
  {
    "default_icon": "icon.png",
    "default_title": "Settings",
    "default_popup": "settingspanel.html"
   }
 }

背景.js 文件:

chrome.extension.onConnect.addListener(function(port) {
  port.onMessage.addListener(function(msg) {
    port.postMessage({counter: msg.counter+1});
  });
});

内容.js

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    sendResponse({counter: request.counter+1});
  });

我在后台脚本上收到一条错误消息,显示“无法读取未定义的属性 'onConnect'”

4

1 回答 1

1

可能您的问题是Issue #131623: Reloading extension while developer tools are open 会破坏 chrome object 和 js console

在修复通过之前,您必须在重新加载扩展之前关闭开发人员工具。

于 2012-06-21T17:27:46.447 回答