在Greasemonkey中未定义'document' [英] 'document' is undefined in Greasemonkey

查看:78
本文介绍了在Greasemonkey中未定义'document'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不到十分钟前,我决定为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.

解决方案

几件事:

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

    1. 在浏览器中打开 about:config .
    2. 过滤 greasemonkey.editor .
    3. 输入有效编辑器的有效路径.我喜欢 TextPad ,但是c:\Windows\System32\notepad.exe在大多数Windows系统上都可以使用. li>
    4. 可能需要重新启动Firefox.

  2. 由于Greasemonkey的沙箱/安全性,无法以这种方式添加事件侦听器.请参见 GM错误,事件处理程序.. p>

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

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

  5. 该页面在 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:

  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);

这篇关于在Greasemonkey中未定义'document'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆