1

When running in test (reloading an unpacked extension), about 1 out of 5 times my event page's chrome.runtime object does not (yet) have the 'onInstalled' property.

// Cannot read property 'addListener' of undefined
chrome.runtime.onInstalled.addListener(...) 

Feels like a race condition on startup within the extension container?

When the error throws, chrome.runtime only has the following:

{OnInstalledReason, OnRestartRequiredReason, PlatformArch,
PlatformNaclArch, PlatformOs, RequestUpdateCheckStatus, connect, sendMessage}
rsb
  • 1,020
  • 1
  • 10
  • 25
  • Could you please provide more details about your code? Like your `manifest.json` and where did you put above code? If you put that listener in `content scripts`, maybe http://stackoverflow.com/questions/30370995/chrome-runtime-oninstalled-is-undefined could help – Haibara Ai Apr 08 '16 at 01:17
  • does it only happen if background page is already open? Chromium bug here https://bugs.chromium.org/p/chromium/issues/detail?id=601559&q=oninstalled&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified – psimyn Apr 28 '16 at 22:52
  • Yes -- only if it's already open. – rsb Apr 29 '16 at 21:53

2 Answers2

0

Try moving the event listener to a background script if they are in a content script. Content scripts have limited access to Chrome API. You can define it in the manifest. Then, if needed, you can send it from the background to a content script.

{
  "manifest_version": 2,

  "name": "someName",
  "version": "0.0",
  "description": ":D",

  "content_scripts": [
    {
      "matches": ["https://*/*", "http://*/*"],
      "js": ["content.js"]
    }
  ],
  "background":{
      "scripts":["background.js"]
  }
}
Tal
  • 9
  • 1
0

According to the linked Issue 601559, this was a bug in Chrome that was fixed in Chrome 51 (May 2016).

Xan
  • 74,770
  • 16
  • 179
  • 206