0

I'm new to Chrome extension development and I'm trying to debug the chrome.notifications API.

Alas, when I try to use the console to test the API, I get the error that:

> chrome.notifications
undefined

I guess I need to test chrome.notifications in the context of an extension. Is there a way to do that?

mlissner
  • 17,359
  • 18
  • 106
  • 169

1 Answers1

1

In order to get access to the Chrome extension APIs you'll need to setup and install a barebones extension with the correct permissions.

You'll need a manifest.json with something like below:

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

As well as an empty index.html. Once you load your unpacked extension in the chrome://extensions page you'll be able to inspect the popup index.html and test out the notifications api in the inspector console.

As @abraham mentioned below another option would be to use a background script. You can do this by adding this object to manifest.json:

"background": {
  "scripts": ["background.js"]
}

In order to open the dev tools for the background page you'll need to click the background page link of your extension found in chrome://extensions.

peterdotjs
  • 1,526
  • 1
  • 13
  • 19
  • 1
    It would probably be easier to use an empty background.js script so you don't have to worry about popups closing. – abraham Mar 05 '15 at 16:24
  • @abraham I think it's a matter of preference and also what the specific use case is. I personally find it easier to use the popup as there is no need to click `refresh` on any change. – peterdotjs Mar 05 '15 at 16:35
  • 1
    If you have the background.js dev tools highlighted you can hit cmd+r/windows equivalent to refresh. – abraham Mar 05 '15 at 16:37
  • Hmm I guess its roughly the same. With the popup as long as the dev tools window is open the popup also stays locked/open. – peterdotjs Mar 05 '15 at 16:44