Javascript chrome.runtime.onInstalled未定义

Javascript chrome.runtime.onInstalled未定义,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我想在安装扩展后启动选项。 这里有很多关于这个问题的答案,我试着用在这个案例中 我的问题是,chrome.runtime.onInstalled未定义。 这是我的源代码: background.js if(typeof chrome.runtime.onInstalled !== 'undefined') { chrome.runtime.onInstalled.addListener(function (details) { //if(details.reas

我想在安装扩展后启动选项。 这里有很多关于这个问题的答案,我试着用在这个案例中

我的问题是,
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"
  ]
}
我是否在清单中遗漏了什么,或者为什么没有定义函数? 为了让我的附加组件能够正常工作,我不得不把支票包起来。
否则,我总是会遇到一个错误,停止脚本的执行。

出于某种原因,您试图将同一脚本用作背景脚本和内容脚本。千万不要那样做,因为这会让人困惑,原因如下。我打赌你在内容脚本中看到了这个错误


内容脚本;特别是,他们无法使用此事件。它也没有意义,因为内容脚本。

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

。谢谢你的解释。我会尽快尝试它,并在它起作用时将其标记为答案ツ工作完美。ThanksI将投票支持这个答案,如果它以代码形式给出了一个实际可行的解决方案,而事实并非如此。这些链接很有帮助,但不能回答问题。