Cannot read property 'onConnet'

I'm attempting to follow the documentation here to pass a variable from my background script, to my content script.

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

Manifest:

{
  "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"
   }
 }

background.js file:

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

content.js

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

I receive an error on the background script that says "Cannot read property 'onConnect' of undefined"


Probably your problem is Issue #131623: Reloading extension while developer tools are open breaks chrome object and js console.

Until the fix gets through you have to close the Developer Tools before reloading the extension.

链接地址: http://www.djcxy.com/p/63714.html

上一篇: 预加载页面消息的脚本失败

下一篇: 无法读取'onConnet'属性