paramName
( optional Type array of paramType )
Undocumented.
Description of this parameter from the json schema.

Google Chrome Extensions (Labs)

Tutorial: Debugging

Tutorial: Debugging
true

This tutorial introduces you to using Google Chrome's built-in Developer Tools to interactively debug an extension.

View extension information

To follow this tutorial, you need the Hello World extension that was featured in Getting Started. In this section, you'll load the extension and take a look at its information in the Extensions page.

  1. Load the Hello World extension if it isn't already running. If the extension is running, you'll see the Hello World icon to the right of your browser's address bar.

    If the Hello World extension isn't already running, find the extension files and load them. If you don't have a handy copy of the files, extract them from this ZIP file. See Getting Started if you need instructions for loading the extension.

  2. Go to the Extensions page (chrome://extensions), and make sure the page is in Developer mode.
  3. Look at the Hello World extension's information on that page. You can see details such as the extension's name, description, and ID.

    [PENDING: screenshot here?]

  4. Keeping an eye on the Extensions page, click the Hello World extension's icon . A popup appears, and a link named popup.html appears in extension's information, under a new "Inspect active views:" heading.

Normally, you can inspect a page by clicking its link in the Extensions page. However, clicking the link for a popup just makes the popup disappear. (This happens because a popup goes away as soon as it loses the keyboard focus.) The next section shows how you can inspect popup pages.

Inspect the popup

Using the chrome-extension protocol, you can view any file in your extension. [PENDING: check] This feature is especially handy for debugging popups.

  1. Get the ID for the Hello World extension from the Extensions page. Example: opnnhifacfpohionnikhlecfgheooenk
  2. Open a new tab or window, and enter the following in the address box:
    chrome-extension://extensionId/popup.html

    You should see a full-page version of the popup's contents, like the following:

  3. Back in the Extensions page, press F5 or click the browser's Reload button to update the Hello World extension's information. [PENDING: this seems like it should be unnecessary -- like the page should update itself]
  4. Click the link to popup.html. A Developer Tools window like the following should appear, displaying the code for popup.html.

  5. If the Scripts button isn't already selected, click it. [PENDING: can we omit this step?]
  6. Click the console button (second from left, at the bottom of the Developer Tools window) so that you can see both the code and the console.

Use the debugger

In this section, you'll follow the execution of the popup page as it adds images to itself.

  1. Set a breakpoint inside the image-adding loop by searching for the string img.src and clicking the number of the line where it occurs (for example, 37):

  2. Make sure you can see the popup.html tab. It should show 20 "hello world" images.
  3. At the console prompt, reload the popup page by entering location.reload():
    > location.reload()
    

    The popup page goes blank as it begins to reload, and its execution stops at line 37.

  4. In the upper right part of the tools window, you should see the local scope variables. This section shows the current values of all variables in the current scope. For example, in the following screenshot the value of i is 0, and photos is a node list that contains at least a few elements. (In fact, it contains 20 elements at indexes 0 through 19, each one representing a photo.)

  5. Click the play/pause button (near the top right of the Developer Tools window) to go through the image-processing loop a single time. Each time you click that button, the value of i increments and another icon appears in the popup page. For example, when i is 10, the popup page looks something like this:
  6. the popup page with 10 images

  7. Use the buttons next to the play/pause button to step over, into, and out of function calls. To let the page finish loading, click line 37 to disable the breakpoint, and then press play/pause to continue execution.

Summary

This tutorial demonstrated some techniques you can use to debug your extensions:

  • Find your extension's ID and links to its pages in the Extensions page (chrome://extensions).
  • View hard-to-reach pages (and any other file in your extension) using chrome-extension://extensionId/filename.
  • Use Developer Tools to inspect and step through a page's JavaScript code.
  • Reload the currently inspected page from the console using location.reload().

Now what?

Now that you've been introduced to debugging, here are suggestions for what to do next:

  • Try installing and inspecting other extensions, such as the samples.
  • Try using widely available debugging APIs such as console.log() and console.error() in your extension's JavaScript code. Example: console.log("Hello, world!")
  • Explore the Developer Tools site, and watch some video tutorials.

[PENDING: do something to help people debug content scripts, which show up in blue]

For more ideas, see the Now what? section of Getting Started.