chrome.runtime.onInstalled 未定义

chrome.runtime.onInstalled is undefined

我想在安装扩展程序后启动这些选项。 这里有很多我尝试在这种情况下使用的这个问题的答案。

我的问题是 chrome.runtime.onInstalled 未定义。 这是我的源代码:

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'));
    });
}

我的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"
  ]
}

我的清单中是不是遗漏了什么或者为什么函数没有定义? 我必须检查它才能让我的附加组件正常工作。 否则我总是会收到停止执行脚本的错误。

出于某种原因,您正在尝试使用相同的脚本作为背景脚本和内容脚本。永远不要那样做,因为它只是令人困惑,原因如下。我打赌你会在内容脚本中看到这个错误。

内容脚本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

我有同样的问题,问题是chrome.runtime.onInstalled.addListener(...)不能是chrome.runtime的第一个使用。