4

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.

abraham
  • 44,161
  • 9
  • 92
  • 140
Michael Walter
  • 1,379
  • 10
  • 23

2 Answers2

8

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.

Community
  • 1
  • 1
Xan
  • 71,217
  • 14
  • 165
  • 189
-1

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

Rayron Victor
  • 2,328
  • 1
  • 23
  • 24