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

心已入冬 提交于 2019-12-07 01:52:16

问题


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:

  • javascript.options.showInConsole
  • extensions.firebug.showChromeErrors
  • extensions.firebug.showChromeMessages

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?


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/10620458/gm-log-and-other-gm-functions-dont-work-in-greasemonkey-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!