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


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



My userscript prints some information using console.log().


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?


unsafeWindow


unsafeWindow



For example, I tried this userscript in Firefox:


// ==UserScript==
// @name console
// ==UserScript==
console.log("hello");




3 Answers
3



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.


console.log()



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.


unsafeWindow.console.log()


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.)


unsafeWindow



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.


unsafeWindow.console.log()


unsafeWindow



Note that Firefox currently supports console.log(), console.info(), console.warn(), and console.error() natively -- no Firebug required.


console.log()


console.info()


console.warn()


console.error()





it is working if i use unsafeWindow.console.log() in firebug but how do i get the output in webConsole. is there no way i get the output without using firebug.
– user1275375
Apr 13 '12 at 6:05



unsafeWindow.console.log()





See the updated answer.
– Brock Adams
Apr 13 '12 at 6:21





i tried this but nothing is displayed in web console
– user1275375
Apr 13 '12 at 6:37





I tried it, it works. Remember that Firebug must be disabled for the page and you must use unsafeWindow unless your script is injected.
– Brock Adams
Apr 13 '12 at 6:49


unsafeWindow





i removed firebug and added unsafeWindow my code now is // ==UserScript== // @name console // ==UserScript== unsafeWindow.console.log('hello'); still nothing is displayed in my web Console
– user1275375
Apr 13 '12 at 6:51



// ==UserScript== // @name console // ==UserScript== unsafeWindow.console.log('hello');



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?


GM_log


// ==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.


javascript.options.showInConsole


true





Yes, you can use GM_log, but it's not portable or unit testable. Also, it pretty much sucks compared to the console API -- both the native one now supported by most browsers, and the Firebug, extra-sweet, console API.
– Brock Adams
Apr 14 '12 at 12:06



GM_log


console



I found that (testing with Chrome/Tampermonkey) what you need is:
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.


window.log("<message goes here>");


unsafeWindow.console.log("<msg>");


unsafeWindow


console






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages