在Greasemonkey中未定义'document' [英] 'document' is undefined in Greasemonkey
问题描述
不到十分钟前,我决定为Greasemonkey编写我的第一个脚本.我对此零经验.另外,我的JavaScript有点生锈,因为自从我上次编写代码以来已经有一段时间了.但是我不知道为什么Greasemonkey会给我这个错误:
Line: 9
Char: 2
Error: 'document' is undefined
Code: 800A1391
Source: Microsoft JScript runtime error
这是我的剧本:
// ==UserScript==
// @name Easier WatchSeries
// @namespace n/a
// @include http://www.watch-series.com/episode/*
// ==/UserScript==
function thing()
{
document.body.setAttribute('onload', show_links(document.getElementById('idepisod').value));
}
thing();
我要做的就是将一个onLoad属性添加到body标签.当我转到管理新用户脚本"->编辑"时,出现此错误.除此之外,脚本什么都不做,所以很明显出了点问题.
我正在运行Firefox 3.6.13.
几件事:
-
已发现,当Greasemonkey没有正确设置编辑器时,就会发生神秘的错误消息一个>.
- 在浏览器中打开 about:config .
- 过滤 greasemonkey.editor .
- 输入有效编辑器的有效路径.我喜欢 TextPad ,但是
c:\Windows\System32\notepad.exe
在大多数Windows系统上都可以使用. li> - 您可能需要重新启动Firefox.
-
由于Greasemonkey的沙箱/安全性,无法以这种方式添加事件侦听器.请参见 GM错误,事件处理程序.. p>
-
您需要使用
unsafeWindow
调用页面的JS函数,例如show_links()
. -
使用经常失败的复杂ajax函数时,最好将它们包装在
try - catch
块中. -
该页面在 www.watch-series.com 和 watch-series.com 之间切换,因此两者都必须位于
@include
指令中.
放在一起,您的脚本将变成:
// ==UserScript==
// @name Easier WatchSeries
// @namespace n/a
// @include http://www.watch-series.com/episode/*
// @include http://watch-series.com/episode/*
// ==/UserScript==
function my_func()
{
try
{
unsafeWindow.show_links(document.getElementById('idepisod').value);
}
catch (zError)
{
alert (zError); //-- Use console.log() in place of alert(), if running Firebug.
}
}
window.addEventListener ("load", my_func, false);
Not more than ten minutes ago I decided to write my first script for Greasemonkey. I have zero experience with it. Also, my JavaScript is a bit rusty as it's been a while since I last wrote code in it. But I can't figure out why Greasemonkey is giving me this error:
Line: 9
Char: 2
Error: 'document' is undefined
Code: 800A1391
Source: Microsoft JScript runtime error
Here's my script:
// ==UserScript==
// @name Easier WatchSeries
// @namespace n/a
// @include http://www.watch-series.com/episode/*
// ==/UserScript==
function thing()
{
document.body.setAttribute('onload', show_links(document.getElementById('idepisod').value));
}
thing();
All I want to do is add an onLoad attribute to the body tag. I get this error when I go to "Manage New User Scripts" -> "Edit". Other than that the script does nothing so obviously something is wrong.
I'm running Firefox 3.6.13.
Several things:
- Open about:config in your browser.
- Filter on greasemonkey.editor.
- Enter a valid path to a valid editor. I like TextPad, but
c:\Windows\System32\notepad.exe
should work on most windows systems. - You might need to restart Firefox.
Event listeners cannot be added that way due to Greasemonkey's sandbox/security. See GM pitfalls, event handlers.
You need to use
unsafeWindow
to call a page's JS functions, likeshow_links()
.When using complicated ajax functions that often fail, it's a good idea to wrap them in
try - catch
blocks.That page switches between www.watch-series.com and watch-series.com, so both need to be in the
@include
directives.
Putting it all together, your script would become:
// ==UserScript==
// @name Easier WatchSeries
// @namespace n/a
// @include http://www.watch-series.com/episode/*
// @include http://watch-series.com/episode/*
// ==/UserScript==
function my_func()
{
try
{
unsafeWindow.show_links(document.getElementById('idepisod').value);
}
catch (zError)
{
alert (zError); //-- Use console.log() in place of alert(), if running Firebug.
}
}
window.addEventListener ("load", my_func, false);
这篇关于在Greasemonkey中未定义'document'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!