How do you log to Firebug from an extension?

南楼画角 提交于 2019-11-30 08:43:00
robr

Since you're not writing Javascript that executes within a window, console is not defined.

So you need to reference the Firebug extension first:

Firebug.Console.log(str);

To log to the console from inside a firefox extension’s javascript:

Application.console.log("Hello from my Firefox Extension!");

As far as I know you can only do that if you are creating a JetPack Add-on. Normal debugging is done with Venkman from Mozilla at http://www.mozilla.org/projects/venkman/

Firebug console is associated with a particular page, so it wouldn't be very convenient even if you managed to log messages there. Did you try Chromebug? I didn't use it, but I would expect to find a similar console for extensions to use there.

You could also use the regular Error Console, although you won't get all the niceties Firebug's console provides. You could install Console^2 https://addons.mozilla.org/en-US/firefox/addon/1815 to make using the Error Console a little less painful.

If in your extension you have access to the content Window object, you can unwrap it, and call the console methods directly:

window.wrappedJSObject.console.log('something important');

There are contexts in which even the Firebug object is unknown, like if you're trying to call it from a sidebar... in which case you have to go all the way back to the original window to get the firebug object:

 var Firebug = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
 .getInterface(Components.interfaces.nsIWebNavigation)
 .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
 .rootTreeItem
 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
 .getInterface(Components.interfaces.nsIDOMWindow).Firebug;

You can then from within your sidebar call Firebug like so:

Firebug.Console.log("foo");

This is documented here: https://developer.mozilla.org/en/Code_snippets/Sidebar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!