239

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?

2

11 Answers 11

452

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 service worker (ManifestV3) or background page (ManifestV2).

Screenshot for ManifestV3 extensions:

enter image description here

Screenshot for ManifestV2 extensions:

enter image description here

enter image description here

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?
    – jds
    Commented Oct 22, 2014 at 22:45
  • 1
    @ggundersen I've updated the picture. The triangle has been removed, that step now automatically happens when Developer mode is activated.
    – Rob W
    Commented Oct 23, 2014 at 9:28
  • how do you debug content scripts then? Commented Jul 5, 2017 at 16:26
  • 1
    @SuperUberDuper Via the devtools in the tab where the content script is running.
    – Rob W
    Commented Jul 5, 2017 at 22:27
  • 1
    @velkoon it is really buggy sometimes you have to disable and reenable extension
    – strix25
    Commented Aug 25, 2022 at 6:46
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

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

6

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.

2
  • 10
    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.
    – Xan
    Commented Oct 22, 2014 at 23:16
  • 3
    this answer helps me to check log of chrome extension that run as popup
    – RashFlash
    Commented Mar 5, 2015 at 14:29
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.

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.

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.

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.

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?

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

0

enter image description here

Right click on the extention then there will be inspect pop-up options will be visible

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.