GM_log有一个参数:日志的字符串。在将信息输出到错误控制台后,用户脚本会一如既往地执行。
例 3.1. 记录到错误控制台然后继续(
gmlog.user.js
)
if (/^http:\/\/diveintogreasemonkey\.org\//.test(window.location.href)) {
GM_log('running on Dive Into Greasemonkey site w/o www prefix');
} else {
GM_log('running elsewhere');
}
GM_log('this line is always printed');
安装这个用户脚本后打开 http://diveintogreasemonkey.org/,这两行就会出现在错误控制台中:
Greasemonkey: http://diveintomark.org/projects/greasemonkey//Test Log: running on Dive Into Greasemonkey site w/o www prefix Greasemonkey: http://diveintomark.org/projects/greasemonkey//Test Log: this line is always printed
如您所见,Greasemonkey 从用户脚本元数据段中取得命名空间和脚本名称,再把作为传给GM_log的参数日志消息算进来,做为显示在错误控制台中显示的信息。
如果您访问的不是http://diveintogreasemonkey.org/,那么下面这两条信息会显示在错误控制台中。
Greasemonkey: http://diveintomark.org/projects/greasemonkey//Test Log: running elsewhere Greasemonkey: http://diveintomark.org/projects/greasemonkey//Test Log: this line is always printed
我已经厌倦去挖掘日志信息的最大长度。它超过了255个字符。还有,输出的信息在错误控制台中可以正确断行,可以向下滚动来查看日志消息其余部分。为日志着迷吧!