Using Firebug Console to log from GreaseMonkey Scripts

This is for all Greasemonkey developers who also use Firebug. Firebug is one of the best Firefox extension for developers.

To start with, almost every Greasemonkey developer uses built-in API method “GM_log()” to log debugging messages to Firefox’s javascript console.

While normal javascript developers use Firebug’s API method “console.log()” to log debugging messages to Firebug console.

Now problem is, if you try calling “console.log()” directly from Greasemonkey scripts, it will not work. As Firebug’s console object reside outside Greasemonkey scripts scope.

There are few workarounds to use Firebug console to log Greasemonkey debugging messages. But one which always work for me is detailed below.

Simplest way is to use…


You can use other firebug logging functions similarly by prefixing unsafeWindow to them. Complete information about Firebug console can be found here.

Next what if you are editing your older Greasemonkey script! A simple way to use text-editors search-and-replace feature and replace all occurrences of “GM_log” with “unsafeWindow.console.log”.

Well it may be nice for some but geeks way is different! I will recommend adding following lines of code at the beginning of your Greasemonkey scripts.

   var GM_log = unsafeWindow.console.log;

The beauty of above codes is, it will first check if Firebug console is available. If yes, then all output from GM_log will be sent to Firebug console. And when Firebug is not available, it will simply use Firefox’s built-in javascript console without any error!

Next if you want to switch back to Firefox’s built-in javascript console, you can do that by just removing or commenting above code.

Disable logging for all GM_log calls…

While developing Greasemonkey scripts, we use GM_log a lot to speed-up coding. But once we are done, ideally we should remove all unnecessary GM_log calls from script as keep Firefox or Firebug console busy.

But this may not sound good idea for some guys who code scritps for sites which changes so often. So better approach is to disable logging via GM_log by adding one line of code, as below, in the beginning of script.

var GM_log = function (){};

This will basically assign an empty function to GM_log. Even smarter approach is to use code like below…

var GM_Debug = 0;

if(!GM_Debug) {
   var GM_log = function(){};

Now all you have to change one character to enable/disable logging.

If you have noticed you are basically assigning a different function to GM_log to override it. Those who are new to javascript may find it little strange, but its perfectly valid!


gaurav September 24, 2008

i do not how to use Greasemonkey
plz tell me if u have time

GeorgeCalm November 24, 2009

// shorter version
var log = (unsafeWindow.console) ? unsafeWindow.console.log : GM_log;

Cory December 19, 2009

You can also do:

var console = unsafeWindow.console;

And then you can use Firebug as you normally would.

console.log(‘hello world!’);

Nate March 10, 2010

“var GM_log = unsafeWindow.console.log;”

After doing this, when trying to use GM_log(“str”);, I get the message “GM_log is not a function”. Did some new version of Greasemonkey or Firefox break this compatibility or am I using it wrong?

Garvin March 25, 2011

Important prerequisite: before the logging starts showing up in the Firebug console, you’ll have to go to about:config and set extensions.firebug.showChromeMessages to true.

NATE: there are two questions there with opposite answers. Did a new version break GM_log()? No. I imagine you were referring to GM_log() in a non-Greasemonkey context (guess: in the Firebug command line).