Domanda

Sto cercando di modificare il mio script GreaseMonkey dall'attivazione su window.onload a window.DOMContentLoaded, ma questo evento non si attiva mai.

Sto utilizzando FireFox 2.0.0.16 / GreaseMonkey 0.8.20080609

Questo è lo script completo che sto cercando di modificare, cambiando:

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

A

window.addEventListener ("DOMContentLoaded", doStuff, false);
È stato utile?

Soluzione

Quindi ho cercato su Google Greasemonkey dom pronto e il primo risultato sembrava dire che lo script Greasemonkey è effettivamente in esecuzione su "DOM ready", quindi devi solo rimuovere la chiamata onload ed eseguire immediatamente lo script.

Ho rimosso il window.addEventListener ("load", function() { E }, false); avvolgimento e ha funzionato perfettamente.Suo tanto più reattiva in questo modo, la pagina appare immediatamente con il tuo script applicato e tutte le domande invisibili evidenziate, senza alcun sfarfallio.E c'era molta gioia....sì.

Altri suggerimenti

Gli script GreaseMonkey vengono eseguiti essi stessi su DOMContentLoaded, quindi non è necessario aggiungere un gestore di eventi di caricamento: basta che lo script faccia tutto ciò che è necessario immediatamente.

http://wiki.greasespot.net/DOMContentLoaded

@Sam:sì, stavo provando lo stesso:

// ==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)
    }

})();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top