If I call console.log('something');
from the popup page, or any script included off that it works fine.
However as the background page is not directly run off the popup page it is not included in the console.
Is there a way that I can get console.log()
's in the background page to show up in the console for the popup page?
is there any way to, from the background page call a function in the popup page?
Any extension page (except content scripts) has direct access to the background page via
chrome.extension.getBackgroundPage()
.That means, within the popup page, you can just do:
To make it easier to use:
Now if you want to do the same within content scripts you have to use Message Passing to achieve that. The reason, they both belong to different domains, which make sense. There are many examples in the Message Passing page for you to check out.
Hope that clears everything.
It's an old post, with already good answers, but I add my two bits. I don't like to use console.log, I'd rather use a logger that logs to the console, or wherever I want, so I have a module defining a log function a bit like this one
When I call log("this is my log") it will write the message both in the popup console and the background console.
The advantage is to be able to change the behaviour of the logs without having to change the code (like disabling logs for production, etc...)
You can open the background page's console if you click on the "background.html" link in the extensions list.
To access the background page that corresponds to your extensions open
Settings / Extensions
or open a new tab and enterchrome://extensions
. You will see something like this screenshot.Under your extension click on the link
background page
. This opens a new window. For the context menu sample the window has the title:_generated_background_page.html
.The simplest solution would be to add the following code on the top of the file. And than you can use all full Chrome console api as you would normally.
You can still use console.log(), but it gets logged into a separate console. In order to view it - right click on the extension icon and select "Inspect popup".
To answer your question directly, when you call
console.log("something")
from the background, this message is logged, to the background page's console. To view it, you may go tochrome://extensions/
and click on thatinspect view
under your extension.When you click the popup, it's loaded into the current page, thus the console.log should show log message in the current page.