The techniques described here work in Firefox 50 and later. If you need to use an earlier version of Firefox, please refer to the article on debugging WebExtensions before Firefox 50.
This article explains how you can use the Firefox developer tools to debug WebExtensions.
A WebExtension can consist of various different pieces — background scripts, popups, options pages, content scripts — and you'll need to use a slightly different workflow to debug each piece. So each piece gets a top-level section in this article, and the intention is that these sections can be read in isolation. We'll begin by introducing the Add-on Debugger, which you'll use to debug most of the pieces of your add-on.
The Add-on Debugger
For most of this article we'll use the Add-on Debugger. To open the Add-on Debugger:
- visit "about:debugging"
- check the box labeled "Enable add-on debugging"
- click the "Debug" button next to your add-on
- click "OK" in the warning dialog.
You'll then see a new window open. The main Firefox window will be switched into the foreground, so you'll have to click on the new window to bring in it front.
This new window is sometimes called a "toolbox" and contains the debugging tools we'll use. It has a tabbed interface: the row of tabs along the top lets you switch between the different tools:
In this article we'll use three debugging tools:
- The Inspector: this enables you to examine and modify the HTML and CSS used to build your add-on's pages.
Debugging background scripts
The examples in this section use the "notify-link-clicks-l10n" example add-on. If you'd like to play along, you can find this example in the webextensions-examples repository.
Background scripts stay loaded for the lifetime of the add-on. They're loaded inside an invisible "background page": by default this is an empty HTML document, but you can specify your own HTML content using the "background" key in "manifest.json".
You can debug background scripts using the Add-on Debugger.
In the Add-on Debugger's Console you'll see logged output, including calls to
console.log() from your own background scripts and any errors the browser raises as it executes them. Note that at the moment, the console shows all errors raised by the browser, not just errors related to your add-ons code.
For example, the notify-link-clicks-i18n example add-on logs a message from its background script when it receives a message from one of its content scripts:
Using the Console's command line, you can access and modify the objects created by your background scripts.
For example, here we call the
notify() function defined in the add-on's background script:
If you switch to the Debugger, you'll see all your add-on's background scripts. You can set breakpoints, step through code, and do everything else you'd expect to be able to do in a debugger.
If you press the Escape key while you're in the Debugger, the toolbox will be split, with the bottom half now occupied by the Console. While you're at a breakpoint, you can now modify the program's state using the console. See Split console for more on this.
Debugging options pages
Options pages are HTML pages that the add-on developer can supply, that contain options for the add-on. They are typically displayed in an iframe in the Add-ons Manager (to see the Add-ons Manager, visit the "about:addons" page).
To debug options pages:
- open the Add-on Debugger for your add-on
- open your add-on's option page.
This video uses the favourite-colour example WebExtension.
You'll also see any messages logged by your code in the Add-on Debugger's Console.
You can also use the Add-on Debugger to debug the page's HTML and CSS. First, though, you need to point the tools at the iframe that hosts the options page. To do this: open the options page, click the icon highlighted in the screenshot below, and select the options page from the drop-down list:
Now switch to the Inspector tab, and you'll be able to examine and edit HTML and CSS for the page:
One problem with popups is that if a popup is open and you click outside the popup, the popup is closed and its code is unloaded. This obviously makes them impossible to debug. To suppress this behavior, click the button in the Add-on Debugger that we've highlighted in the screenshot below:
Now, when you open a popup it will stay open until you press Escape.
Note that this change applies to built-in browser popups, like the Hamburger menu (), as well as add-on popups.
Also note that the change is persistent, even across browser restarts. We're working on addressing this in bug 1251658, but until then you may prefer to re-enable autohide by clicking the button again before you close the Browser Toolbox.
Internally, this button just toggles the
ui.popup.disable_autohide preference, which you can toggle manually using about:config.
This video uses the beastify example WebExtension.
You can also use the Add-on Debugger to debug the popup's HTML and CSS. First, though, you need to point the tools at the popup's document. To do this: open the popup, then click the icon highlighted in the screenshot below and select the popup's page from the drop-down list:
Now switch to the Inspector, and you'll be able to examine and edit the popup's HTML and CSS:
Debugging content scripts
You can use the Add-on Debugger to debug background pages, options pages, and popups. However, you can't use it to debug content scripts. This is because, in multiprocess Firefox, content scripts run in a different process from the other parts of your add-on.
To debug content scripts attached to a web page, use the normal web developer tools for that page:
- either select "Toggle Tools" from the Web Developer submenu in the Firefox Menu (or Tools menu if you display the menu bar or are on Mac OS X)
- or press the CtrlShiftI (CommandOptionI on OS X) keyboard shortcut.
By default, the tools are shown attached to the bottom of browser tab, to reflect the fact that they are attached to this tab. You'll see any output from
console.log() statements in your content scripts. You will also see your content scripts listed in the Debugger, where you'll be able to set breakpoints, step through the code, and so on.
This video uses the notify-link-clicks-i18n example WebExtension.
If the developer tools tab was not already open when the content script was injected, sometimes the content script is not listed in the debugger panel. If you experience this, reloading the page with the developer tools tab open should fix the problem.