How do you log to Firebug from an extension? - debugging

I'm writing an extension for Firefox, and I need to log some data to Firebug's console. Within the scope of my addon, "console" is undefined, and "window.content.console" is also undefined. So how do I log to the console?

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

Related

Firefox command line print

Have a requirement to print html or html from vb6 application using only firefox as we have already for IE. So, need command to print using firefox.exe.
Something like this firefox.exe -print http://www.google.com which is not working. Is there anyway to do this? Thanks for your help.
There is no built-in support for a -print command line switch. See this document for the different command line switches supported for the Firefox browser. Support for such functionality would most probably have to be implemented through an extension.
If no extension exists that currently offers what your looking for, a Google search yielded this mozillaZine forum thread: "Printing to file from Commandline?" You could have a look at the discussion, or download and modify the referenced add-on from the source link.
Or else, you could always create your own application (probably not in VB6) by embedding the Gecko layout, but since the Mozilla killed the embedding API a while back, you'll have a lot of integration work to do (or else you could go with another layout engine like Webkit).
You can use the built in webbrowser control to print html via IE: http://www.vbforums.com/showthread.php?384076-Webbrowser-Control-Tip-and-Examples
If you must use firefox, there was a firefox active x control but I have not used it and dont know the status:
mozilla firefox activeX
or
http://www.iol.ie/~locka/mozilla/control.htm
or
https://bitbucket.org/geckofx
It also looks like there is a webkit floating around online (google it)

Logging an object to console.log

I'm developing a firefox add-on, and I'd like to log objects to console.log. Is there a way to expand the object description beyond the monotonic [object Object]? I mean if I have something like {hello: "world"} I'd like it's fields and values to be displayed like in node.js' console? Without actually implementing a loop iterating over the values.
Sometimes just a simply JSON.stringify(object) will work. I use that quite a bit.
If you're using Firefox nightly, you can do this:
set 'extensions.sdk.console.logLevel' in about:config to 'all'
use console.dir and look in the relatively new browser console:
https://www.evernote.com/shard/s1/sh/a6cb2922-a21d-431a-bc58-1800d4895ca1/d1f39b0af0b69f20c8d14e7cbb6e0d85
console.dir prints a nice expandable tree in the browser console.
Use ctrl+shift+j for console (but new firebug i think do it now too)
Remember console.log didnt work at SDK 1.14! See Official blog.
Use console.error

console.log is not working when used in a Firefox, Greasemonkey script

My userscript prints some information using console.log().
This works fine in Chrome, but when I install this userscript in Firefox (Greasemonkey), the web console in Firefox is not displaying anything.
I searched for a solution and some suggested to use unsafeWindow but it is also not showing any output. Moreover unsafeWindow cannot be used for chrome. I even installed Firebug but it was no use. How can I resolve this?
For example, I tried this userscript in Firefox:
// ==UserScript==
// #name console
// ==UserScript==
console.log("hello");
You mean it doesn't work when installed via Greasemonkey, right?
Not long ago, Greasemonkey broke console.log (New! Bug report). Now, to see the results of a plain console.log() call from a Greasemonkey, you need to look in Firefox's Error console, not Firebug's.
You can see FF's Error console by pressing: CtrlShiftJ.
However, you can use unsafeWindow.console.log() in both Chrome and Greasemonkey scripts. Chrome now has limited support for unsafeWindow.
If you use unsafeWindow, you have access to the full range of Firebug's logging functions from Greasemonkey. (Firebug must be installed and they still might not work in Chrome userscripts; I haven't tested that way in a while.)
In Firefox, if Firebug is not installed, or it is not active for the page, then unsafeWindow.console.log() calls will display to the New "Web Console" (CtrlShiftK).
You need to use the unsafeWindow when inside a Greasemonkey script.
Note that Firefox currently supports console.log(), console.info(), console.warn(), and console.error() natively -- no Firebug required.
Wait a minute: if the question is about logging in the console with Greasemonkey (I could swear I saw the tag greasemonkey), why not use the GM_log method?
// ==UserScript==
// #name GM_log Example
// #namespace http://www.example.com/
// ==/UserScript==
GM_log("This is an example of GM_log");
Or am I missing something?
PS: you can also check for javascript.options.showInConsole in about:config. it should be true.
I found that (testing with Chrome/Tampermonkey) you need:
window.log("<message goes here>");, not unsafeWindow.console.log("<msg>");,
as unsafeWindow and console come up as undefined.
Try that, as I'm pretty sure that's the way you're supposed to do it in later versions of browsers, etc.

Can Firefox code trigger any functions in the extension?

I have modified some code inside my Firefox. Just add on some extra functions for my isolation network. Besides that I also want to create my own FF extension for this particular purpose. I just need some information:
Can the code inside my Firefox, call any function Javascript declared in my extension?
Generally yes, but your question is not specific enough. Both Firefox and an extension can have JS and C code, code in XPCOM components, code running in a specific window, JS modules, etc.
Assuming the "Firefox code" is running in some window (e.g. you edit browser.js) and the "extension function" you want to call also exists in that window (e.g. you overlay chrome://browser/content/browser.xul, which is the URI of the main Firefox window, in which the browser.js code runs), you can just call it as you would normally do.

Why is console.log statements not appearing in my FireBug console anymore?

I'm using FF 3.6 and FireBug 1.5.0
my console.log statements are no longer appearing in my firebug console. is anyone else experiencing this? is there a setting somewhere that got switched that I don't know about?
This can happen if the javascript you are debugging tries to write something to the console variable. As an example, I stuck the following code into a random javascript file:
console = {};
console.log('asdfasdf');
After trying to set console to an empty object, console.log didn't do anything.
To fix this, you should only need to figure out where your script is trying to change the console variable, and then refresh the page.

Resources