如何在GreaseMonkey脚本中实现“DOM Ready”事件?

时间:2022-10-27 23:51:26

I'm trying to modify my GreaseMonkey script from firing on window.onload to window.DOMContentLoaded, but this event never fires.

我正在尝试修改我的GreaseMonkey脚本,从window.onload触发到window.DOMContentLoaded,但此事件永远不会触发。

I'm using FireFox 2.0.0.16 / GreaseMonkey 0.8.20080609

我正在使用FireFox 2.0.0.16 / GreaseMonkey 0.8.20080609

This is the full script that I'm trying to modify, changing:

这是我正在尝试修改的完整脚本,正在更改:

window.addEventListener ("load", doStuff, false);

to

window.addEventListener ("DOMContentLoaded", doStuff, false);

3 个解决方案

#1


24  

So I googled greasemonkey dom ready and the first result seemed to say that the greasemonkey script is actually running at "DOM ready" so you just need to remove the onload call and run the script straight away.

所以我用Google搜索了paintmonkey dom,并且第一个结果似乎表明,greasemonkey脚本实际上是在“DOM ready”运行,所以你只需要删除onload调用并立即运行脚本。

I removed the window.addEventListener ("load", function() { and }, false); wrapping and it worked perfectly. It's much more responsive this way, the page appears straight away with your script applied to it and all the unseen questions highlighted, no flicker at all. And there was much rejoicing.... yea.

我删除了window.addEventListener(“load”,function(){和},false);包装,它完美地工作。通过这种方式响应速度更快,页面会立即显示,并且您的脚本会应用到该页面,所有未见的问题都会突出显示,根本不会闪烁。并且有很多欢乐......是的。

#2


10  

GreaseMonkey scripts are themselves executed on DOMContentLoaded, so it's unnecessary to add a load event handler - just have your script do whatever it needs to to immediately.

GreaseMonkey脚本本身是在DOMContentLoaded上执行的,因此不需要添加一个load事件处理程序 - 只需让你的脚本立即执行它需要的任何操作。

http://wiki.greasespot.net/DOMContentLoaded

http://wiki.greasespot.net/DOMContentLoaded

#3


1  

@Sam: yeah, I was trying the same:

@Sam:是的,我也在尝试:

// ==UserScript==
// @name           Stack Overflow highlight viewed questions
// @namespace      *
// @include        http://*.com/questions
// @include        http://*.com/questions?*
// @include        http://*.com/questions
// @include        http://*.com/questions?*
// @version        0.55 (DOM-Ready instead of onload)
// ==/UserScript==

(function() {

    // Customizable items
    // var fav_tags = ["python", "database", "mysql"];          // Your favorite tags
    const UNSEEN_BACK_COLOR = "rgb(225,210,210)";     // Backcolor for the question already seen
    const FAV_TAG_BACK_COLOR = "rgb(210,210,225)";  // Backcolor for the favorite tags

    // Internal to the DOM
    // const QUESTION_URL = "http:\/\/*.com\/questions\/([0-9]+)\/";
    const QUESTION_URL = "http:\/\/*.com\/questions\/([0-9]+)\/";
    const TAG_PREFIX = "show questions tagged ";

    const SEEN_MARK = "x";
    //

    var seen_q = [];
    var seen_q_str = "";

    var seen_q_str = GM_getValue ("seen_q", "");
    var seen_q = seen_q_str.split("|");

    var fav_tags_str = GM_getValue ("fav_tags", "")
    var fav_tags = fav_tags_str.split(" ")

    var already_run = false;

    GM_registerMenuCommand ("Set favorite tags", askTags);

    // window.addEventListener ("DOMContentLoaded", doStuff, false);
    if (! doStuff()) {
        window.addEventListener ("load", doStuff, false);
    }

    function doStuff() {

        var elements = window.document.getElementsByTagName('A');

        if (! elements || already_run) {
            return false;
        } else {
            already_run = true;
        }

        GM_log ("here");

        for (elem = 0; elem < elements.length; elem++) {
            if (elements[elem].href.match (QUESTION_URL)) {
                curr_q = RegExp.$1;

                // Already seen?
                if ((seen_q.length < curr_q) || (seen_q [curr_q] != SEEN_MARK)) {
                    elements[elem].style.backgroundColor = UNSEEN_BACK_COLOR;
                    seen_q [curr_q] = SEEN_MARK;
                }

                // Is a favorite tag?
                node = elements[elem].parentNode.parentNode;
                for (tag = 0; tag <= fav_tags.length; tag++) {
                    if (node.innerHTML.match ("'" + fav_tags[tag] + "'")) {
                        node.style.backgroundColor = FAV_TAG_BACK_COLOR;
                        break;
                    }
                }

                // return (0);
            }
        }

        seen_q_str = seen_q.join("|");
        GM_setValue ("seen_q", seen_q_str);

        return true;
    }


    function askTags() {
        fav_tags_str = prompt("Favorite tags (separated by spaces)", fav_tags_str);
        GM_setValue ("fav_tags", fav_tags_str)
    }

})();

#1


24  

So I googled greasemonkey dom ready and the first result seemed to say that the greasemonkey script is actually running at "DOM ready" so you just need to remove the onload call and run the script straight away.

所以我用Google搜索了paintmonkey dom,并且第一个结果似乎表明,greasemonkey脚本实际上是在“DOM ready”运行,所以你只需要删除onload调用并立即运行脚本。

I removed the window.addEventListener ("load", function() { and }, false); wrapping and it worked perfectly. It's much more responsive this way, the page appears straight away with your script applied to it and all the unseen questions highlighted, no flicker at all. And there was much rejoicing.... yea.

我删除了window.addEventListener(“load”,function(){和},false);包装,它完美地工作。通过这种方式响应速度更快,页面会立即显示,并且您的脚本会应用到该页面,所有未见的问题都会突出显示,根本不会闪烁。并且有很多欢乐......是的。

#2


10  

GreaseMonkey scripts are themselves executed on DOMContentLoaded, so it's unnecessary to add a load event handler - just have your script do whatever it needs to to immediately.

GreaseMonkey脚本本身是在DOMContentLoaded上执行的,因此不需要添加一个load事件处理程序 - 只需让你的脚本立即执行它需要的任何操作。

http://wiki.greasespot.net/DOMContentLoaded

http://wiki.greasespot.net/DOMContentLoaded

#3


1  

@Sam: yeah, I was trying the same:

@Sam:是的,我也在尝试:

// ==UserScript==
// @name           Stack Overflow highlight viewed questions
// @namespace      *
// @include        http://*.com/questions
// @include        http://*.com/questions?*
// @include        http://*.com/questions
// @include        http://*.com/questions?*
// @version        0.55 (DOM-Ready instead of onload)
// ==/UserScript==

(function() {

    // Customizable items
    // var fav_tags = ["python", "database", "mysql"];          // Your favorite tags
    const UNSEEN_BACK_COLOR = "rgb(225,210,210)";     // Backcolor for the question already seen
    const FAV_TAG_BACK_COLOR = "rgb(210,210,225)";  // Backcolor for the favorite tags

    // Internal to the DOM
    // const QUESTION_URL = "http:\/\/*.com\/questions\/([0-9]+)\/";
    const QUESTION_URL = "http:\/\/*.com\/questions\/([0-9]+)\/";
    const TAG_PREFIX = "show questions tagged ";

    const SEEN_MARK = "x";
    //

    var seen_q = [];
    var seen_q_str = "";

    var seen_q_str = GM_getValue ("seen_q", "");
    var seen_q = seen_q_str.split("|");

    var fav_tags_str = GM_getValue ("fav_tags", "")
    var fav_tags = fav_tags_str.split(" ")

    var already_run = false;

    GM_registerMenuCommand ("Set favorite tags", askTags);

    // window.addEventListener ("DOMContentLoaded", doStuff, false);
    if (! doStuff()) {
        window.addEventListener ("load", doStuff, false);
    }

    function doStuff() {

        var elements = window.document.getElementsByTagName('A');

        if (! elements || already_run) {
            return false;
        } else {
            already_run = true;
        }

        GM_log ("here");

        for (elem = 0; elem < elements.length; elem++) {
            if (elements[elem].href.match (QUESTION_URL)) {
                curr_q = RegExp.$1;

                // Already seen?
                if ((seen_q.length < curr_q) || (seen_q [curr_q] != SEEN_MARK)) {
                    elements[elem].style.backgroundColor = UNSEEN_BACK_COLOR;
                    seen_q [curr_q] = SEEN_MARK;
                }

                // Is a favorite tag?
                node = elements[elem].parentNode.parentNode;
                for (tag = 0; tag <= fav_tags.length; tag++) {
                    if (node.innerHTML.match ("'" + fav_tags[tag] + "'")) {
                        node.style.backgroundColor = FAV_TAG_BACK_COLOR;
                        break;
                    }
                }

                // return (0);
            }
        }

        seen_q_str = seen_q.join("|");
        GM_setValue ("seen_q", seen_q_str);

        return true;
    }


    function askTags() {
        fav_tags_str = prompt("Favorite tags (separated by spaces)", fav_tags_str);
        GM_setValue ("fav_tags", fav_tags_str)
    }

})();