3

Following these instructions, I went to about:debugging -> This Firefox and clicked Inspect on my extension, which shows a console. But no logs show up there when I trigger my extension. Using the list-cookies example, I added the last two lines:

gettingAllCookies.then((cookies) => {

    //set the header of the panel
    var activeTabUrl = document.getElementById('header-title');
    var text = document.createTextNode("Cookies at: "+tab.title);
    var cookieList = document.getElementById('cookie-list');

    console.log('I can't see this log!');
    cookieList.parentNode.appendChild(document.createTextNode(Date()));

When I invoke the popup, I see the current date/time in the popup, but no log shows up in the console.

I tried setting extensions.sdk.console.logLevel and restarting as mentioned here (even though I think that's for older versions), but it didn't help.

I thought maybe there's a console permission or something I might need to add to the manifest, but didn't find any such thing.

Complete code for reference. I only changed the lines marked with +/-:

function showCookiesForTab(tabs) {
  //get the first tab object in the array
  let tab = tabs.pop();

  //get all cookies in the domain
  var gettingAllCookies = browser.cookies.getAll({url: tab.url});
  gettingAllCookies.then((cookies) => {

    //set the header of the panel
    var activeTabUrl = document.getElementById('header-title');
    var text = document.createTextNode("Cookies at: "+tab.title);
    var cookieList = document.getElementById('cookie-list');
-   activeTabUrl.appendChild(text);
+
+   console.log('I can't see this log!');
+   cookieList.parentNode.appendChild(document.createTextNode(Date())); // I see this updated even though I don't see the log


    if (cookies.length > 0) {
      //add an <li> item with the name and value of the cookie to the list
      for (let cookie of cookies) {
        let li = document.createElement("li");
        let content = document.createTextNode(cookie.name + ": "+ cookie.value);
        li.appendChild(content);
        cookieList.appendChild(li);
      }
    } else {
      let p = document.createElement("p");
      let content = document.createTextNode("No cookies in this tab.");
      let parent = cookieList.parentNode;

      p.appendChild(content);
      parent.appendChild(p);
    }
  });
}

//get active tab to run an callback function.
//it sends to our callback an array of tab objects
function getActiveTab() {
  return browser.tabs.query({currentWindow: true, active: true});
}
getActiveTab().then(showCookiesForTab);
erosman
  • 7,094
  • 7
  • 27
  • 46
hrt
  • 63
  • 4

2 Answers2

4

Firefox console has been divided into different areas. The result of console.log() can be viewed in the relative area.

  • Multiprocess Browser Console Ctrl+Shift+J
    Mostly logs by Firefox itself
  • Web Developer Tools Ctrl+Shift+I or F12
    Logs by Tab/Webpage and Content scripts of addons
  • Extension Toolbox about:debugging#/runtime/this-firefox ➜ XYZaddon ➜ Inspect
    Logs by background scripts of XYZaddon

Update

Based on comments, here is a tested simplified code that you can work on. The log shows on Extension Toolbox.

async function showCookiesForTab() {
  // get the first tab object in the array
  const tabs = await browser.tabs.query({currentWindow: true, active: true});

  // get all cookies in the domain
  const cookies = await browser.cookies.getAll({url: tabs[0].url});
  console.log(cookies);
  // Array(6) [ {…}, {…}, {…}, {…}, {…}, {…} ]
}

showCookiesForTab();
erosman
  • 7,094
  • 7
  • 27
  • 46
  • Hmmm, I checked those other places but the log doesn't show up there, either. Not in Browser Console, not in Developer Tools, and not in about:debugging. – hrt Jan 25 '22 at 07:02
  • @hrt Is that code complete? Are there errors? Where does the code run? Getting cookies require "cookies" permission. Does your add-on have it? – erosman Jan 25 '22 at 09:22
  • Yes, it has the cookies permission and runs without errors. I see the textnode with the date get appended (the line that executes after the console log), but I don't see the log. I included just the top portion of the code for brevity, but you can see the rest of the code in the link I provided (it's straight from Mozilla's examples). The only change I made was to add the console.log statement. – hrt Feb 21 '22 at 05:44
  • @hrt Mozilla's example runs the code in browserAction popup. The `console.log` would appear only in **Extension Toolbox**. If the log does not appear, it could mean the the code doesn't reach `.then()`. Without seeing the complete code, it is all guesswork. – erosman Feb 21 '22 at 06:20
  • I know it reached `.then()` because I can see the popup updated with the current date as mentioned in the description. Note this line that was added _after_ the log statement `document.createTextNode(Date())` – hrt Feb 28 '22 at 20:44
  • Updated with the full code – hrt Feb 28 '22 at 23:58
  • @hrt Reply updated – erosman Mar 01 '22 at 04:46
0

I had a similar issue. I didn’t figure out the cause, but I find a way to see the console.log in the Extension Toolbox.

I added a background script to handle most of the popup.js logic. And since there is a background script running, I can see the log.

Still don’t why I couldn’t see the log in the first place.

Joyescat
  • 507
  • 5
  • 11