Question

In a web page running in normal browsers, we can use console.log()/error()/warn() to print messages to the web console. However, in a page(html or xul) running in xulrunner application, it seems that console.log()/error()/warn() drop messages silently.

I set these prefs: pref("browser.dom.window.dump.enabled", true); pref("javascript.options.showInConsole", true); and run xulrunner with -console or -jconsole options.

So, is it possible to enable javascript console.log/error/warn in xulrunner applications?

I known that dump() function works for xulrunner with -console, but it is not standard, and there are web pages out of my control running in my xulrunner applications.

For an example demostrating this problem, you can checkout https://github.com/matthewkastor/XULRunner-Examples.git, and run the 'browser' application with an web page using console.

Was it helpful?

Solution

XULRunner will drop messages passed to console.log() unless you attach a debugger to it. This can be achieved by remote debugging: XULRunner as debuggee, firefox as debugger. Once the debugger attached to the XULRunner app, you can see console.log()ed messages in the debugger.

Instruction for remote debgging can be found at MDN.

You may also want to set devtools.debugger.remote-enabled pref of your firefox to true.

Use this code in your app:

  var windowtype = ...
  Components.utils.import('resource://gre/modules/devtools/dbg-server.jsm');
  DebuggerServer.chromeWindowType = windowtype;
  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
    DebuggerServer.addBrowserActors(windowtype);
  }
  DebuggerServer.openListener(6000);

If your page is (x)html, assign windowtype with null; if your page is xul, assign windowtype with the same value of the windowtype attribute of the xul:window element:

  <xul:window windowtype=... >
  ...
  </xul:window>

OTHER TIPS

You might use the nsIConsoleService.

For a simple log message :

function consoleLog(aMsg){
    var console;
    console = Components.classes["@mozilla.org/consoleservice;1"]
               .getService(Components.interfaces.nsIConsoleService);
    console.logStringMessage(aMsg);
}

console.log('My important message');

Ref : nsIConsoleService at MDN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top