2008-09-16 22 views
19

私はGreaseMonkeyスクリプトをwindow.onloadでwindow.DOMContentLoadedに発射しないように修正しようとしていますが、このイベントは発生しません。GreaseMonkeyスクリプトで "DOM Ready"イベントを実装する方法は?

私は、Firefox 2.0.0.16 /のGreaseMonkey 0.8.20080609

Thisを使用していますが変更、私は修正しようとしている完全なスクリプトです:

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

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

答えて

24

私はgreasemonkey dom readyとグーグルモンキーのスクリプトは実際には "準備ができて"実行されていると言うようにfirst resultグーグルグーグルonload呼び出しを実行し、スクリプトをすぐに実行します。

私はwindow.addEventListener ("load", function() {}, false);ラッピングを取り出して、それが完全に働きました。それは多くの場合です。このように反応すると、ページはスクリプトが適用された直後に表示され、すべての目に見えない質問が強調表示され、ちらつきは一切ありません。そして、多くの喜びがあった。

10

GreaseMonkeyスクリプト自体はDOMContentLoaded上で実行されるため、ロードイベントハンドラを追加する必要はありません。スクリプトがすぐに必要な処理を実行するだけです。

http://wiki.greasespot.net/DOMContentLoaded

+0

、私はGoogleの検索に水平に向けられた広告を着色する簡単なユーザスクリプトを記述しようとしていたような彼らは(私は実際に "ガスランド"を検索するときに私をガス業界がスポンサーとなっているページに連れて行くためのリンクをクリックしました)、それは機能しませんでした。私はその後、最高の答えを試みた: 'イベント'のためのaddEventListener、それは働いた。 –

1

@Sam:ええ、私は同じことをしようとしていた。

プレリリースのFirefox 3.6を使用して
// ==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) 
    } 

})(); 
関連する問題