Michael Walter
Michael Walter

Reputation: 1477

chrome.runtime.onInstalled is undefined

I want to launch the options after my extension got installed. Here are many answer to this issue which I tried to use in this case.

My problem is that chrome.runtime.onInstalled is undefined. This is my source code:

background.js

if(typeof chrome.runtime.onInstalled  !== 'undefined')
{
    chrome.runtime.onInstalled.addListener(function (details)
    {
        //if(details.reason == 'update') window.open(chrome.extension.getURL('options.html'));
        if(details.reason == 'install') window.open(chrome.extension.getURL('options.html'));
    });
}

my manifest.json

{
  "name": "Context Menu 2.0 for Google Translate™",
  "short_name": "Translate",
  "version": "1.0.0.4",
  "manifest_version": 2,
  "description": "Creates a context menu option to translate selected texts to a specified language",
  "icons": {
    "16": "trans_16.png",
    "64": "trans_64.png",
    "128": "trans_128.png"
  },
  "content_scripts": [
    {
      "matches":
      [
          "http://*/*", "https://*/*"
      ],
      "js": ["background.js"]
    }
  ], 
  "options_page": "options.html",
  "background": {
    "scripts": ["background.js"]
  },
  "permissions":[
    "http://*/*", "https://*/*",
    "storage","contextMenus"
  ]
}

Am I missing something in my manifest or why is the function not defined? I had to wrap the check around it to get my add-on working at all. Otherwise I always get an error which stops the execution of my script.

Upvotes: 6

Views: 3948

Answers (3)

Poseidon Octo
Poseidon Octo

Reputation: 1

Incase anyone has the same problem as me, I had the same error and the issue was an import statement in my content script.

The import statement was importing something from my background script, which for some reason meant the content script was trying to understand APIs called in the background script that it didn't have access to. I solved this by simply moving the exports of my background script to a separate file (although i'm sure there is a better way) :).

Upvotes: 0

Rayron Victor
Rayron Victor

Reputation: 2426

I Have the same problem and the trouble was that chrome.runtime.onInstalled.addListener(...) couldn't be the first use of chrome.runtime.

Upvotes: -1

Xan
Xan

Reputation: 77523

You are, for some reason, trying to use the same script as a background script and a content script. Never do that, as it's just confusing, and for the reason below. I bet you see this error in the content script.

Content scripts have very, very limited access to Chrome API; in particular, this event is not available to them. It wouldn't make sense, either, since content scripts do not exist yet when the extension is initialized.

Upvotes: 10

Related Questions