Debugging a XULRunner Application
From MDC
The easiest way to debug a XULRunner application is to get the system to tell you what's wrong! There are two different consoles available and various preferences which will ensure that the information you need to know is displayed on them.
Contents |
[edit] See also
[edit] Prefs
Setting the following prefs will make your debugging life much easier!
Preferences should be in a .js file in the directory %appname%/defaults/preferences/. It doesn't matter what the file is called (as long as the extension is .js), so debug.js is as good as any!
/* debugging prefs */
pref("browser.dom.window.dump.enabled", true);
pref("javascript.options.showInConsole", true);
pref("javascript.options.strict", true);
pref("nglayout.debug.disable_xul_cache", true);
pref("nglayout.debug.disable_xul_fastload", true);
[edit] Console
To enable a console on windows, start xulrunner with the -console argument. Note that -console goes after the application.ini argument: e.g. xulrunner.exe /path/to/application.ini -console
To output text to the console, use dump("my text here\n");. This will only work if the pref pref("browser.dom.window.dump.enabled", true) is enabled.
[edit] JavaScript Console
To enable the JS console, start xulrunner with the -jsconsole argument. Note: if you are not using the stub executable to launch the application, the -jsconsole argument is after the application.ini argument: e.g. xulrunner.exe /path/to/application.ini -jsconsole
By default the JS console only shows errors from web content. To show errors in chrome Javascript, the pref pref("javascript.options.showInConsole", true) must be set.
To log JS errors to disk, set the environment variable "XRE_CONSOLE_LOG" to the path to the filename. i.e. export XRE_CONSOLE_LOG=/path/to/logfile or set XRE_CONSOLE_LOG=C:\path\to\logfile.
[edit] To output messages to the JS console
There are two ways to output messages to the JS console:
-
Components.utils.reportError(str)will outputstras an "error" (i.e. with the red stop icon next to it). -
jsdump(str)(function defined below) will outputstras a "message" with a speech bubble icon next to it:
function jsdump(str)
{
Components.classes['@mozilla.org/consoleservice;1']
.getService(Components.interfaces.nsIConsoleService)
.logStringMessage(str);
}
(See also a MozillaZine Knowledge Base article on this topic.)
[edit] Venkman
The easiest way to permanently add Venkman to your XULRunner-based application is the following one (successfully tested with XULRunner 1.8.0.1, xulrunner-1.9a7pre.en-US.win32):
- Get Venkman from addons.mozilla.org (v0.9.87 at the time of writing)
- Unzip the package and copy the file chrome/venkman.jar to the chrome directory of your XULRunner-based application.
- Copy components/venkman-service.js to the components directory of your application.
- Add
<script src="chrome://venkman/content/venkman-overlay.js"/>to one of your XUL windows. - Add UI to open Venkman to your window (it could be a menu item or a toolbar button). Make it call
start_venkman()when activated. - Add the following lines to your chrome/chrome.manifest:
content venkman jar:venkman.jar!/content/venkman/ locale venkman en-US jar:venkman.jar!/locale/en-US/venkman/ skin venkman modern/1.0 jar:venkman.jar!/skin/modern/venkman/
(This can also be used in Firefox 1.5 to launch venkman from html buttons in a chrome-loaded page; since venkman.jar gets put into <package>chrome/venkman.jar the chrome.manifest that works for me needs "chrome/" added to venkman.jar:
content venkman jar:chrome/venkman.jar!/content/venkman/ locale venkman en-US jar:chrome/venkman.jar!/locale/en-US/venkman/ skin venkman modern/1.0 jar:chrome/venkman.jar!/skin/modern/venkman/
[edit] Troubleshooting
[edit] x-jsd is not a registered protocol
If the message "x-jsd is not a registered protocol" appears when trying to open *.js files in Venkman, then add an empty file called .autoreg in the same directory as your XULRunner binary. If .autoreg already exists, then edit it to force the last modified time to be updated. XULRunner will then notice the new components file.
When adding a component to a XULRunner application, change the BuildID in application.ini.
http://developer.mozilla.org/en/docs/Troubleshooting_XPCOM_components_registration
[edit] function toOpenWindowByType is not defined
If you get an Error that "function toOpenWindowByType is not defined" you may add the following function to your scripts (doesn't seem to be in the venkman overlay):
function toOpenWindowByType(inType, uri) {
var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
window.open(uri, "_blank", winopts);
}
[edit] I can't see my pages, functions ...
If your pages are not in the loaded script window, uncheck the menu item "debug\exclude Browser Files" and find them in open windows tab