Chrome.runtime.on Installed by undefined

I want to run parameters after installing my extension. There are many answers to this question that I tried to use in this case.

My problem chrome.runtime.onInstalled

is undefined. This is my original 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 a check around it to get my add-on to work at all. Otherwise, I always get an error that stops my script from executing.

+3


source to share


2 answers


For some reason, you are trying to use the same script as the background script and content script. Never do this, as it is just confusing, and for the reason below. I bet you see this error in the content of the script.



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

+4


source


I got the same problem and the problem was that it chrome.runtime.onInstalled.addListener(...)

couldn't be the first use chrome.runtime

.



0


source







All Articles