2

I'm building an extension for Edge but I am having trouble debugging my background.js. I have not been able to get it to print to the console at all. I am able to print to console just fine through content.js. Here are some console.log statements that I have tried:

background.js

console.log("PLEASE PRINT");

browser.browserAction.onClicked.addListener(function(tab) {
    console.log("CLICKED BUTTON");
    browser.tabs.executeScript(null,{code:"window.print()"});
});

browser.tabs.onActivated.addListener(activeInfo => {
    console.log("onActivated listener");
});

browser.tabs.onActivated.addListener(function(activeInfo) {
    console.log("onActivated listener");
});

function printALog() {
    console.log("i pray");
}

function handleActivated(activeInfo) {
  console.log("PLEASE");
}


browser.tabs.onActivated.addListener(handleActivated);
browser.windows.onFocusedChanged.adddListener(handleActivated);
browser.webNavigation.onCompelted.addListener(handleActivated);

Am I doing something wrong or is this just not possible using Edge's developer tools?

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
moe
  • 51
  • 3
  • Which console are you watching? background console or web page console? What's expected when you call `window.print()`? It's just for printers, not console. – Haibara Ai Mar 03 '17 at 08:51
  • window.print() brings out the print window. It was taken from a tutorial video that Microsoft posted. I didn't know there was a background console! How would I get to it? – moe Mar 06 '17 at 02:25
  • See [Background script debugging](https://learn.microsoft.com/en-us/microsoft-edge/extensions/guides/debugging-extensions) – Haibara Ai Mar 06 '17 at 02:41
  • Thanks! This is exactly what I was looking for. – moe Mar 08 '17 at 02:09

1 Answers1

0

Extension page which runs background.js basically is a special standalone page. It doesn't share windows object and console output with other web page tabs.

In Edge, background page can be inspected through extension management menu (as Haibara Ai mentioned, here's the link). You will see your console output there.

But please note, if you inject any of your extension scripts into the page context using manifest.json content_scripts property (and you would want that if you plan to interact with the page content), they would in fact use that page context for console output. Sometimes it's useful, but sometimes you'd want to have all logs in one place. In this case you can send debug messages to background console using browser.runtime.sendMessage and relaying these messages to console in the background page.

Anatoly Sazanov
  • 1,814
  • 2
  • 14
  • 24