Search code examples
javascriptloggingfirebuggreasemonkeyuserscripts

Cross browser console.log for greasemonkey/chrome userscripts


How do you go about using console.log in greasemonkey scripts? I don't get any output.

// ==UserScript==
// @name          test
// @namespace     test
// @description   test
// @require       https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js
// @version       1.2
// ==/UserScript==

console.log('test');

I get the impression that you can use console.log normally in Chrome. Is there a nice wrapper so you can log to console in both Firefox and Chrome?


Solution

  • console.log will appear in the Error Console in Firefox (Tools -> Error Console OR CTRL+SHIFT+J)

    Changing it to unsafeWindow.console.log will make the log appear in Firebug. Make sure you read up on security concerns of unsafeWindow before using it.

    The other option to get the log to appear in Firebug, as you said, is to leave it as console.log but turn on extensions.firebug.showChromeErrors AND extensions.firebug.showChromeMessages. This can be done by typing about:config in the Firefox address bar. Then filter for Chrome. Find the two options and double click them to turn them on.

    All Firefox examples are for Firefox 5.0