仅测试 Chrome 个 API

Test Chrome-only APIs

我是 Chrome 扩展开发的新手,我正在尝试调试 chrome.notifications API。

唉,当我尝试使用控制台测试 API 时,我得到的错误是:

> chrome.notifications
undefined

我想我需要在扩展的上下文中测试 chrome.notifications。有没有办法做到这一点?

为了访问 Chrome 扩展 API,您需要设置并安装具有正确权限的准系统扩展。

您需要一个 manifest.json,内容如下:

{
  "name": "foo",
  "short_name": "foo",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "foo bar",
  "browser_action": {
    "default_popup": "index.html",
  },
  "permissions": ["notifications"],
 }

还有一个空的index.html。在 chrome://extensions 页面中加载解压缩的扩展后,您将能够检查弹出窗口 index.html 并在检查器控制台中测试通知 api。

正如@abraham 在下面提到的,另一种选择是使用后台脚本。您可以通过将此对象添加到 manifest.json:

来完成此操作
"background": {
  "scripts": ["background.js"]
}

为了打开后台页面的开发工具,您需要单击 chrome://extensions.

中扩展的 background page link