“文档”在 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();
我想做的就是向 body 标记添加一个 onLoad 属性。当我转到“管理新用户脚本”-> 时出现此错误“编辑”。除此之外,脚本什么也没做,所以显然出了问题。
我运行的是 Firefox 3.6.13。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几件事:
当 Greasemonkey 没有设置正确的编辑器时,就会出现神秘的错误消息。
c:\Windows\System32\notepad。 exe
应该可以在大多数 Windows 系统上运行。由于 Greasemonkey 的沙箱/安全性,无法以这种方式添加事件侦听器。请参阅GM 陷阱、事件处理程序。
您需要使用
unsafeWindow 调用页面的 JS 函数
,例如
show_links()
。当使用经常失败的复杂ajax函数时,最好将它们包装在
try - catch
块中。该页面在 www.watch-series.com 和 watch-series.com 之间切换,因此两者都需要位于
@include
指令。把它们放在一起,你的脚本将变成:
Several things:
That cryptic error message has been found to happen when Greasemonkey does not have a proper editor set up.
c:\Windows\System32\notepad.exe
should work on most windows systems.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: