当前位置:首页> 正文

关于javascript:如何在GreaseMonkey脚本中实现” DOM Ready”事件?

关于javascript:如何在GreaseMonkey脚本中实现” DOM Ready”事件?

How to implement “DOM Ready” event in a GreaseMonkey script?

我正在尝试将GreaseMonkey脚本修改为从window.onload触发到window.DOMContentLoaded,但是此事件从不触发。

我正在使用FireFox 2.0.0.16 / GreaseMonkey 0.8.20080609

这是我要修改的完整脚本,更改为:

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

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

所以我用谷歌搜索了研究对象,并且第一个结果似乎是表明该脚本实际上是在" DOM ready"下运行的,因此您只需要删除onload调用并立即运行该脚本即可。

我删除了window.addEventListener ("load", function() {}, false);package,并且效果很好。这样,它的响应速度更快,页面立即显示,并已应用了脚本,并且突出显示了所有看不见的问题,根本没有闪烁。还有很多喜乐...是的。


GreaseMonkey脚本本身是在DOMContentLoaded上执行的,因此无需添加加载事件处理程序-只需让脚本立即执行所需的任何操作即可。

http://wiki.greasespot.net/DOMContentLoaded


@Sam:是的,我正在尝试相同的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// ==UserScript==
// @name           Stack Overflow highlight viewed questions
// @namespace      *
// @include        http://stackoverflow.com/questions
// @include        http://stackoverflow.com/questions?*
// @include        http://stackoverflow.com/questions
// @include        http://stackoverflow.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:\\/\\/stackoverflow.com\\/questions\\/([0-9]+)\\/";
    const QUESTION_URL ="http:\\/\\/stackoverflow.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)
    }

})();

展开全文阅读

相关内容