2011-08-10 2 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(); 

}); 

지금이 문제는 아약스를 통해 iframe을로드 내용의 링크 중 일부는 iframe을 새로 고침하지 않도록 내가 클래스 "mw_error"에 대한 새로운 가치를 얻을하지 못할 것입니다. 그것은 아약스 성공 콜백을 사용하는 것이 좋습니다지만 어떻게 해야할지 잘 모르겠습니다. 이에

어떤 도움이 많이 주시면 감사하겠습니다 :)

답변

2

당신은 아약스 호출을 수신 할 수 있지만, 그 대상 페이지에 따라 지나치게 복잡해진다. 야생 페이지 아약스 - 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; 
} 


당신이 필요하지 않을 수 있도록 그리스 몽키는 Iframe을 실행할 수 있음을주의 스크립트의 목적에 따라 iFrame에 있다면 다른 것을하기 위해서입니다.

반대로, GM 스크립트가 계획되지 않은 경우 iFrame URL에서 실행되는 경우 예기치 않은 결과가 발생할 수 있습니다.

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

을 그리고 GM 스크립트의 // @include 지침 (들) 두 URL (// @include URL_* 등) 적용 :

예를 들어

, 당신은 URL_A에서이 페이지를했다 가정합니다.

그런 다음 스크립트에 alert ('Script start.');이있는 경우 페이지를로드 할 때 2 개의 경고가 표시됩니다.

+0

GM 스크립트의 지시문을 사용하여 iframe이로드 될 때만 실행했습니다. 스크립트를 편집하고 iframe 매개 변수를 꺼내서 지금 치료를합니다. :) –

+0

우수. 다행 이군요. –

+1

그건 뜻밖의 코드 보스입니다. +1 – Tim

관련 문제