Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
like image 340
user1275375 Avatar asked Apr 13 '12 04:04

user1275375


People also ask

How do I debug a Greasemonkey script?

Open the global script debugger window via Tools → Web Developer → Browser Toolbox → Debugger (or Ctrl + Shift + Alt + I ) . Search for the name of your userscript and start debugging.

How do I use Greasemonkey scripts in Firefox?

Installing the Greasemonkey Extension. Click on the Firefox drop-down menu at the top left of the browser and select Add-ons. Type Greasemonkey into the add-ons search box at the top right of the browser. Find Greasemonkey in the list and click on Install.

How do I read a script on Greasemonkey?

To access Greasemonkey's management pane, click on the downward arrow on the right side of the button Greasemonkey to the right of your URL bar and select Manage User Scripts. This opens a tab with a list of your scripts. For each script, you'll find a button to disable or uninstall it.

Do Greasemonkey scripts work in Tampermonkey?

Tampermonkey. Tampermonkey is one of the most popular browser extensions with over 10 million users. Tampermonkey is used to run so-called userscripts (sometimes also called Greasemonkey scripts) on websites.


2 Answers

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.

like image 71
Brock Adams Avatar answered Sep 17 '22 16:09

Brock Adams


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.

like image 34
RASG Avatar answered Sep 19 '22 16:09

RASG