user1275375
user1275375

Reputation: 1391

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");

Upvotes: 15

Views: 29066

Answers (3)

Starwort
Starwort

Reputation: 83

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.

Upvotes: 1

yodog
yodog

Reputation: 6232

Wait a minute: if the question is about logging in the console with Greasemonkey (I could swear I saw the tag ), 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.

Upvotes: 5

Brock Adams
Brock Adams

Reputation: 93493

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.

Upvotes: 19

Related Questions