不到十分钟前,我决定为 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。
几件事:
-
当 Greasemonkey 没有设置正确的编辑器时,就会出现这个神秘的错误消息 http://userscripts.org/topics/59474.
- Open 关于:配置在您的浏览器中。
- 过滤油猴编辑器.
- 输入有效编辑器的有效路径。我喜欢TextPad http://www.textpad.com/products/textpad/index.html, but
c:\Windows\System32\notepad.exe
应该适用于大多数 Windows 系统。
- You might需要重新启动火狐。
由于 Greasemonkey 的沙箱/安全性,无法以这种方式添加事件侦听器。看GM 陷阱、事件处理程序 http://commons.oreilly.com/wiki/index.php/Greasemonkey_Hacks/Getting_Started#Pitfall_.232%3a_Event_Handlers.
你需要use unsafeWindow调用页面的JS函数 http://commons.oreilly.com/wiki/index.php/Greasemonkey_Hacks/Getting_Started#Pitfall_.238%3a_Calling_Remote_Page_Scripts, like show_links()
.
当使用经常失败的复杂 ajax 函数时,最好将它们包装在try - catch
blocks.
该页面在之间切换www.watch-series.com and 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)