9

I am using Chrome browser Version 52.0.2743.116 (64-bit) on Mac OSX 10.11.6.

I have a chrome extension with a background.js where I have a console.log statement for debugging purposes. However, I am unable to see the output in the Chrome browser console. Alert works fine but console.log outputs nothing.

alert("CSS code: "+css);
console.log("CSS code:"+css);

I have checked to make sure "All" messages are selected, the context is set to "top", etc. When the page loads, I see other console.log messages (not from my extension), the alert window pop up but nothing printed to the console.

How do I see console.log output?

EDIT: Adding the function code where the alert and console.log statements appear.

function doSomething(tab) {
    var tabUrl = tab.url;

        if (tabUrl && tabUrl.indexOf("my-site.com") != -1) {

            var css = "";

            if (hideAds == true) {
                css += ".adsBanner {display: none !important;} .promo {display: none !important;} ";
            }

            alert("CSS code: "+css);
            console.log("CSS code:"+css);

            chrome.tabs.insertCSS(tab.id, {
                code: css
            });

        }       
}

EDIT 2: In response to the comment that this is a duplicate of this question, I want to print console.log statements from the background file, I don't have a popup.html file. The other question has solutions to execute console.log on the background page called from the popup page.

Community
  • 1
  • 1
zeeshan
  • 699
  • 2
  • 7
  • 12
  • I initially thought it might be the script not being loaded but if you're sure the alert is showing, could you share the rest of your code so we can replicate? – Tom Nijs Aug 11 '16 at 11:58
  • 2
    Can you try switching the statements so that console log is executed first and let us know what happens? (if you haven't done this already) – mkaran Aug 11 '16 at 12:04
  • I have added the function code. @mkaran I switched the statements but no dice. – zeeshan Aug 11 '16 at 12:09
  • @zeeshan This looks like a duplicate of http://stackoverflow.com/questions/3829150/google-chrome-extension-console-log-from-background-page – styfle Aug 11 '16 at 12:10
  • OK found this previously answered here that solved my problem: http://stackoverflow.com/questions/10257301/where-to-read-console-messages-from-background-js-in-a-chrome-extension – zeeshan Aug 11 '16 at 12:24
  • Possible duplicate of [Where to read console messages from background.js in a Chrome extension?](https://stackoverflow.com/questions/10257301/where-to-read-console-messages-from-background-js-in-a-chrome-extension) – brasofilo Jul 06 '19 at 13:52
  • I had a filter set on mine from another project – Willy Richardson Aug 12 '21 at 21:21

2 Answers2

9

This has been previously answered here. It seems the background.js has its own console window that needs to be watched (launched from the extension's inpsect view link under chrome://extensions).

Community
  • 1
  • 1
zeeshan
  • 699
  • 2
  • 7
  • 12
  • 4
    Cheers for this - While having my first go at extensions I did not realize the console output of your background script does get a separate console and it was driving my crazy! – Templerschaf Nov 12 '16 at 17:28
-7

The answer to your problem is to use document.write();