Thomas Schmidt
Thomas Schmidt

Reputation: 81

GM_log and other GM functions don't work in Greasemonkey scripts

I have created a "Hello World" Greasemonkey script in Firefox which contains only one line of code:

GM_log("Hello World");

This did not seem to function, at least it did not produce any output in my firebug console.
The same with other GM_... functions like GM_wait

When I replaced:

GM_log("Hello World");

with:

alert("Hello World")

it worked (so the script headers are not the problem).

I have also set the following about:config options to true:

Is there some other setting to change for GM_... functions to work in Greasemonkey scripts?

Do I have to change other firebug settings for GM_log messages to show in the firebug console?

Upvotes: 7

Views: 8488

Answers (3)

gml
gml

Reputation: 71

The reason for this is a new special Metadata Block imperative: @grant, added in GM 1.0. If you need GM_log to work, you have to add this line into your script Metadata Block: "// @grant GM_log" , otherwise it will not work. You can read about this feature at http://wiki.greasespot.net/@grant.

Upvotes: 7

Brock Adams
Brock Adams

Reputation: 93493

Where did you get GM_wait? That is not a GM function; some people just name their ad-hoc functions that. Note that GM version 0.9.19 broke a lot of timing functionality, but this was fixed in version 0.9.20.

As for GM_log(), that works but it doesn't always put its message in a sensible location. On later versions of Greasemonkey, GM_log() writes to Firefox's Error Console -- which you can open by pressing CtrlShiftJ.
But, as Comentarist said, there is no good reason to use GM_log anymore. It's of limited functionality and does not port well.

All the good browsers now support console.log() natively (No Firebug required), but on Firefox, this also tends to output to Firefox's Error Console.

To use Firebug's excellent logging functions (well worth a look), you currently must use unsafeWindow like so:

unsafeWindow.console.clear ();
unsafeWindow.console.log ("Hello World!");

Upvotes: 3

Commentator
Commentator

Reputation: 650

I'd recommend you forget about GM_log() and use:

console.log('hello world');

http://wiki.greasespot.net/GM_log

Like it says "since GM_log will only display a single string at a time, users with Firebug installed may prefer to use console.log instead."

But about your question, I couldn't say why.

Upvotes: 1

Related Questions