'document' is undefined in Greasemonkey

允我心安 提交于 2019-12-05 14:16:31

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!