chrome.runtime.onInstalled未定义。

4
我希望在我的扩展程序安装后启动选项。以下是我尝试解决该问题的多种答案。但我遇到的问题是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"
  ]
}

我在清单文件中漏掉了什么,还是为什么函数没有被定义? 我不得不将检查过程包装起来才能让我的插件正常工作。 否则,我总是会遇到错误,导致脚本停止执行。

2个回答

9
你因某些原因试图将相同的脚本用作背景脚本和内容脚本。永远不要这样做,因为这会让事情变得混乱,并且出现以下原因。我敢打赌你在内容脚本中看到了这个错误。
内容脚本对Chrome API的访问非常有限;特别是,它们无法使用此事件。这也没有意义,因为当扩展初始化时,内容脚本尚不存在

听起来很有道理。谢谢您的解释。我会尽快尝试并在它正常工作时将其标记为答案,哈哈。 - Michael Walter
完美运行。谢谢。 - Michael Walter
如果这个答案提供了一个实际可行的代码解决方案,我会点赞它 - 但它并没有。这些链接是有帮助的,但并没有回答问题。 - user2677034

-1

我有同样的问题,麻烦是chrome.runtime.onInstalled.addListener(...)不能作为chrome.runtime的第一次使用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接