2011-08-10 7 views
2

jQueryを使用し、ドキュメントに「クラス」が存在するかどうかを検出するGreasemonkeyスクリプトがあります。Greasemonkeyでajaxコンテンツを持つiframeのコンテンツを取得する

var damageMessage = $(".mw_error").text(); 

ShankarSangoliのおかげで、彼または彼女は私に、iframeがロードされるときにクラスを見つけるための解決策を教えてくれました。

$("iframe").load(function(){ 

    var damageMessage = $(this).contents().find(".mw_error").text(); 

}); 

私が今持っている問題は、Ajax経由でiframeの負荷コンテンツ内のリンクの一部は、IFRAMEが更新されませんので、私はクラス「mw_error」の新しい値を取得傾けることです。私はAjaxの成功のコールバックを使用することを提案されましたが、私はこれを行う方法がわかりません。この上

すべてのヘルプははるかに高く評価されるだろう:)

答えて

2

あなたはAJAX呼び出しのために聞くことができますが、それは、ターゲットページに応じて、過度に複雑になります。野生ページのAJAX-ifiedコンテンツを取得するための最も強力な方法は、それをポーリングすることで、そのように:あなたは必要ないかもしれないので、

function myCode (jNode) { 
    //YOUR CODE HERE 
    var damageMessage = jNode.text(); 
    //... 
} 

waitForKeyElements (".mw_error", myCode, false, "iframe"); 

function waitForKeyElements (
    selectorTxt, /* Required: The jQuery selector string that 
         specifies the desired element(s). 
        */ 
    actionFunction, /* Required: The code to run when elements are 
         found. It is passed a jNode to the matched 
         element. 
        */ 
    bWaitOnce,  /* Optional: If false, will continue to scan for 
         new elements even after the first match is 
         found. 
        */ 
    iframeSelector /* Optional: If set, identifies the iframe to 
         search. 
        */ 
) 
{ 
    var targetNodes, btargetsFound; 

    if (typeof iframeSelector == "undefined") 
     targetNodes  = $(selectorTxt); 
    else 
     targetNodes  = $(iframeSelector).contents() 
              .find (selectorTxt); 

    if (targetNodes && targetNodes.length > 0) { 
     /*--- Found target node(s). Go through each and act if they 
      are new. 
     */ 
     targetNodes.each (function() { 
      var jThis  = $(this); 
      var alreadyFound = jThis.data ('alreadyFound') || false; 

      if (!alreadyFound) { 
       //--- Call the payload function. 
       actionFunction (jThis); 
       jThis.data ('alreadyFound', true); 
      } 
     }); 
     btargetsFound = true; 
    } 
    else { 
     btargetsFound = false; 
    } 

    //--- Get the timer-control variable for this selector. 
    var controlObj  = waitForKeyElements.controlObj || {}; 
    var controlKey  = selectorTxt.replace (/[^\w]/g, "_"); 
    var timeControl  = controlObj [controlKey]; 

    //--- Now set or clear the timer as appropriate. 
    if (btargetsFound && bWaitOnce && timeControl) { 
     //--- The only condition where we need to clear the timer. 
     clearInterval (timeControl); 
     delete controlObj [controlKey] 
    } 
    else { 
     //--- Set a timer, if needed. 
     if (! timeControl) { 
      timeControl = setInterval (function() { 
        waitForKeyElements ( selectorTxt, 
              actionFunction, 
              bWaitOnce, 
              iframeSelector 
             ); 
       }, 
       500 
      ); 
      controlObj [controlKey] = timeControl; 
     } 
    } 
    waitForKeyElements.controlObj = controlObj; 
} 


、GreasemonkeyのものiFrame上で実行できることに注意してくださいスクリプトの目的に応じて、あなたがiFrameにいるかどうかを知る以外のことをする。

逆に、計画していないときにGMスクリプトがiFrame URLで起動すると、予期しない結果が発生する可能性があります。例えば

、あなたはURL_Aでこのページ持っていたとします

<body> 
    <iframe src="URL_B"></iframe> 
</body> 

をそしてGMスクリプトの// @includeディレクティブ(s)は、両方のURL(// @include URL_*など)をカバーしました。

スクリプトにちょうどalert ('Script start.');がある場合、ページを読み込むと2つの警告が表示されます。

+0

GMスクリプトの指示文を使って、iframeが読み込まれたときにのみ実行しました。私はスクリプトを編集し、iframeパラメータを取り出し、今すぐ治療を行います。 :) –

+0

優れています。うれしいです。 –

+1

それはいくつかの平均的なコードのボスです。 +1 – Tim

関連する問題