Debug extensions
Stay organized with collections
Save and categorize content based on your preferences.
Extensions can access the same Chrome DevTools as web pages. To become an expert in debugging extensions, you will need to know how to locate logs and errors of the different extension components. This tutorial provides fundamental techniques for debugging your extension.
Before you begin
This guide assumes that you have basic web development experience. We recommend reading
Development Basics for an introduction to the extension development workflow.
Design the user interface gives you an introduction to the user
interface elements available in extensions.
Break the extension
This tutorial will break one extension component at a time and then demonstrate how to fix it. Remember to undo the bugs introduced in one section before continuing to the next section. Start by downloading the Broken Color sample on GitHub.
Debug the manifest
First, let's break the manifest file by changing the "version" key to "versions":
manifest.json:
{"name":"Broken Background Color",(削除) "version":"1.0", (削除ここまで)"versions":"1.0","description":"Fix an Extension!",...}
Save the extension and try loading it again. It should load successfully this time. In the extension
Management page you will see three buttons: Details, Remove and Errors. The Errors
button label turns red when there's an error. Click the Errors button to see the
following error:
The service worker sets the default color to storage and logs it to the console. To view this log, open the Chrome DevTools panel by selecting the blue link next to Inspect views.
Let's break the service worker by changing onInstalled to lowercase oninstalled:
service-worker.js:
// There's a typo in the line below;// ❌ oninstalled should be ✅ onInstalled.(削除) chrome.runtime.onInstalled.addListener(()=>{ (削除ここまで)chrome.runtime.oninstalled.addListener(()=>{chrome.storage.sync.set({color:'#3aa757'},()=>{console.log('The background color is green.');});});
Refresh and click Errors to view the error log. The first error lets you know that the service worker failed to register. This means something went wrong during initiation:
Serviceworkerregistrationfailed.Statuscode:15.
Service worker registration failed. Status code: 15 error message
Service worker registration error message.
Now that the extension initializes correctly, let's break the popup by commenting out the highlighted lines below:
popup.js:
...changeColorButton.addEventListener('click',(event)=>{constcolor=event.target.value;// Query the active tab before injecting the content scriptchrome.tabs.query({active:true,currentWindow:true},(tabs)=>{// Use the Scripting API to execute a scriptchrome.scripting.executeScript({target:{tabId:tabs[0].id},args:[color],func:setColor});});});
Navigate back to the Extensions Management page. The Errors button reappears. Click it to
view the new log. It shows the following error message:
The error, tabs is undefined, says the extension doesn't know where to inject the content script.
Correct this by calling tabs.query(), then selecting the active tab.
To update the code, click the Clear all button in the upper right-hand corner, and then reload the
extension.
Debug content scripts
Now let's break the content script by changing the variable "color" to "colors":
content.js:
...functionsetColor(color){// There's a typo in the line below;// ❌ colors should be ✅ color.(削除) document.body.style.backgroundColor=color; (削除ここまで)document.body.style.backgroundColor=colors;}
Refresh the page, open the popup and click the green box. Nothing happens.
If you go to the Extensions Management page the Errors button does not appear. This is because only runtime errors, console.warning and,
console.error are recorded on the Extensions Management page.
Content scripts run inside a website. So to find these errors we must inspect the web page the extension is trying to alter:
The error says colors is not defined. The extension must not be passing the variable correctly.
Correct the injected script to pass the color variable into the code.
Monitor network requests
The popup often makes all of the required network requests before even the speediest of
developers can open DevTools. To view these requests, refresh from inside the network panel. It
reloads the popup without closing the DevTools panel.
Some extension APIs
require permissions. Refer to the permissions article and the Chrome
APIs to ensure an extension is requesting the correct permissions in the manifest.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2012年09月18日 UTC."],[],[]]