Accessing console and devtools of extension's `background` script
Asked Answered
Y

10

232

I just started out with Google Chrome extensions and I can't seem to log to console from my background js. When an error occurs (because of a syntax error, for example), I can't find any error messages either.

My manifest file:

{
  "name": "My First Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "pageCapture",
    "tabs"
  ]
}

background.js:

alert("here");
console.log("Hello, world!")

When I load the extension, the alert comes up but I don't see anything being logged to console. What am I doing wrong?

Yellowwood answered 21/4, 2012 at 7:59 Comment(2)
possible duplicate of How to debug Google Chrome background script?Allin
Please select messages or info if highlighted bar is on other tabs like No verbose selected tab mattersMatthia
S
441

You're looking at the wrong place. These console messages do not appear in the web page, but in the invisible background page (ManifestV2) or service worker (ManifestV3).

To view the correct console open devtools for the background script's context:

  1. Visit chrome://extensions/ or right-click the extension icon and select "Manage extensions".
  2. Enable developer mode
  3. Click on the link named background page (ManifestV2) or service worker (ManifestV3).

Screenshot for ManifestV2 extensions:

enter image description here

enter image description here

Screenshot for ManifestV3 extensions:

enter image description here

Stinkwood answered 21/4, 2012 at 10:12 Comment(7)
@RobW, I don't have the triangular button to expand the extension and don't see any active views. Is this answer no longer the solution for the latest Chrome build or am I missing something?Gillian
@ggundersen I've updated the picture. The triangle has been removed, that step now automatically happens when Developer mode is activated.Stinkwood
how do you debug content scripts then?Juarez
@Juarez Via the devtools in the tab where the content script is running.Stinkwood
Nothing appears when I click that.Hornbook
@Hornbook it is really buggy sometimes you have to disable and reenable extensionVintager
If you don't see Inspect view, load the extension againServitor
F
17

I had the same problem, in my case the logging was set to "Hide all" in the console tab in Chrome Developer tools. I had not even realised this was an option, and I can't remember turning it off

screenshot of setting in console tab in chrome dev tools

Fredrickafredrickson answered 10/8, 2017 at 9:54 Comment(0)
B
10

For followers who wish to see the debug console for a "content script" of their chrome extension, it is available by doing a normal "show developer console" then use the dropdown arrow to selects its "javascript environment" then you'll have access to its methods, etc.

enter image description here

Bratcher answered 18/10, 2016 at 22:37 Comment(0)
G
7

additionally

if you want to see content_script js file ( when "background" property is not set ) in manifest.json

"content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["popup.js"],
  }]

"browser_action": {
    "default_icon": "icon_32.png",
    "default_popup": "popup.html"
  }

then right click on extension icon and click on Inspect popup and developer window opens with popup.html opened , there you see console tab.

Gheber answered 14/10, 2014 at 11:3 Comment(2)
1) This does not answer the question, 2) This is simply wrong; content script logs messages into the console of the page it's injected to, i.e. the actual browser tab. I suppose in your code, popup.js was reused in the popup.html, and as such the output of that copy goes to the place you mentioned. But it's totally misleading.Representation
this answer helps me to check log of chrome extension that run as popupTopflight
P
5

Similar to the answer of Michiel i also had a funny console configuration: A filter i dont remember setting:

enter image description here

After clearing the filter i saw the messages.

Palmetto answered 10/6, 2019 at 22:44 Comment(0)
S
1

If we want to read messages printed to console from the popup page, we can click the extension icon to open the popup page, then do right click on the popup page anywhere, a dropdown menu will display, we just click "Inspect" menu to open the developer tool. Notice that the popup page must be keep opening. If it is closed(by window.close()), the developer tool will be closed too.

Shaduf answered 10/1, 2020 at 2:33 Comment(0)
Z
1

The other answer(s) work(s) for background.js but, if you are looking for console.logs from the popup then you can try:

var bkg = chrome.extension.getBackgroundPage();
bkg.console.log('foo');

I was developing using cra, and this worked for me.

Zouave answered 23/9, 2021 at 18:25 Comment(0)
B
0

I had this problem as well. It seems as though my webpage was not updating to the newly saved script. This was solved by pressing Ctrl + refresh (or Ctrl + F5) in the chrome browser.

Belak answered 27/6, 2019 at 12:42 Comment(0)
S
0

For those developing extensions for Firefox:

TL;DR version: you need to use Ctrl+Shift+J to call up the browser console window, and click on the settings icon on the top right-hand corner and make sure that "Show Content Messages" is checked.

Longer explanation from a related stackoverflow question: How do I see the console.log output of a background script in a Firefox WebExtension?

Shonda answered 29/9, 2022 at 4:16 Comment(0)
L
0

In my case I had to click the Inspect Pop-up button in the Exntensions panel which opened up a new window with a dedicated console for the extension.

enter image description here

Lotte answered 19/11, 2023 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.