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
  • 46,583
  • 10
  • 100
  • 152
Michael Walter
  • 1,427
  • 12
  • 28

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
  • 74,770
  • 16
  • 179
  • 206
  • Makes sense for me. Thanks for the explanation. I will try it as soon as possible and mark it as the answer, when it is working ツ – Michael Walter May 21 '15 at 11:04
  • I would upvote this answer if it gave an actual working solution in code - which it doesn't. The links are helpful but don't answer the question. – user2677034 Feb 17 '21 at 17:56
-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,398
  • 1
  • 25
  • 25