“文档”在 Greasemonkey 中未定义

发布于 2024-10-14 19:16:25 字数 703 浏览 3 评论 0原文

不到十分钟前,我决定为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2024-10-21 19:16:25

有几件事:

  1. 当 Greasemonkey 没有设置正确的编辑器时,就会出现神秘的错误消息。

    1. 在浏览器中打开 about:config
    2. greasemonkey.editor 上进行过滤。
    3. 输入有效编辑器的有效路径。我喜欢 TextPad,但 c:\Windows\System32\notepad。 exe 应该可以在大多数 Windows 系统上运行。
    4. 可能需要重新启动 Firefox。
  2. 由于 Greasemonkey 的沙箱/安全性,无法以这种方式添加事件侦听器。请参阅GM 陷阱、事件处理程序

  3. 您需要使用unsafeWindow 调用页面的 JS 函数,例如 show_links()

  4. 当使用经常失败的复杂ajax函数时,最好将它们包装在try - catch块中。

  5. 该页面在 www.watch-series.comwatch-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);

Several things:

  1. That cryptic error message has been found to happen when Greasemonkey does not have a proper editor set up.

    1. Open about:config in your browser.
    2. Filter on greasemonkey.editor.
    3. Enter a valid path to a valid editor. I like TextPad, but c:\Windows\System32\notepad.exe should work on most windows systems.
    4. You might need to restart Firefox.
  2. Event listeners cannot be added that way due to Greasemonkey's sandbox/security. See GM pitfalls, event handlers.

  3. You need to use unsafeWindow to call a page's JS functions, like show_links().

  4. When using complicated ajax functions that often fail, it's a good idea to wrap them in try - catch blocks.

  5. 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文