2

This question is different from console.log in firefox is not working?, as this question is regarding the extension debug panel, not simply web page console.

Now I am in the extension debug panel: console.info() cannot output hi message, what's wrong?

The screenshot is as follows: console.info showing nothing

Also alert function seems not work in Firefox extension, what's wrong? Are some functions disabled by Firefox extension?

Makyen
  • 31,849
  • 12
  • 86
  • 121
hugemeow
  • 7,777
  • 13
  • 50
  • 63

1 Answers1

4

Your screenshot shows that you have "Logging" turned off. You need to click on "Logging" and enable at least Logging->Log. Without that enabled, you are specifically telling the console not to show the hi you are attempting to print.

Without logging enabled: without logging

With logging enabled: with logging

However, when testing to double check, I was able to get it into a mode where the hi was not printed in the console even when "Logging" was enabled. When I closed that instance of the debugger and opened a new instance the hi was already printed in the console. On the other hand, it appears that any new instance will retain such output from prior instances. Given that I managed to get it into a mode where the hi did not print when Logging was enabled, there are still some problems with this new functionality. I was unable to duplicate this a second time.

As to alerts: No, there is nothing disabled. You are probably running into a common problem of having the variable window not defined in the context/scope in which you are running. Firefox browser extensions often/usually run in a context where window is not defined. This ends up being a problem for many people, as they assume that window will be defined for them.

Window undefined

If window is defined depends on how the portion of your code that is currently running was entered. Even if window is defined, it is often not defined as the window which you are expecting (the window of the current tab). You will probably need to obtain a reference to the window object for the most recently accessed window/tab.

If a browser window exists (in some instances you could be running where no browser window exists, yet, e.g. at start-up), you can obtain a reference to the most recent browser window, document, and gBrowser with:

if (window === null || typeof window !== "object") {
    //If you do not already have a window reference, you need to obtain one:
    //  Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
    /* Add-on SDK:
    var window = require('sdk/window/utils').getMostRecentBrowserWindow();
    //*/
    //* Overlay and bootstrap (from almost any context/scope):
    var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
                         .getService(Components.interfaces.nsIWindowMediator)
                         .getMostRecentWindow("navigator:browser");        
    //*/
}
if (typeof document === "undefined") {
    //If there is no document defined, get it
    var document = window.content.document;
}
if (typeof gBrowser === "undefined") {
    //If there is no gBrowser defined, get it
    var gBrowser = window.gBrowser;
}

If you are running the code in response to an event (e.g. a button command event), you can obtain the current window with:

var window = event.view

Defining the window:
Define window

and show a window.alert('Hello'):
An Alert

Note: If you are wanting to be natively compatible with multi-process Firefox (Electrolysis, or e10s), then gaining access to the contents of the current document is more complex. There are shims in place which should make your code continue to work with multi-process Firefox for some time, but they may/will eventually go away.

References:

  1. nsIWindowMediator
  2. Working with windows in chrome code
  3. SDK: window/utils
  4. SDK: windows

Large portions of this were copied from my earlier answers, including this link, and this one.

Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121