2016-03-08 18 views
0

我正在製作一個bot的東西,它被設計爲移動到文件的下一個'頁面'並搜索一個字符串,並重復這個動作直到找到該字符串。一切工作,在搜索字符串,但它只能導航到下一頁一次(所以它只能去online.php?page = 2。我相信這是因爲iframe.contentWindow.load只觸發一次,開代碼的第一次運行。因此,當它移動到一個新的頁面,它不是持續的。每次iframe重新導航時執行操作。 (javascript)

任何想法?

document.body.innerHTML = '' //Out with the old 
var iframe = document.createElement('iframe'); 
var increm = 1 
iframe.src = 'online.php?page=' + parseInt(increm) 
iframe.style.height= '3000px' 
iframe.style.width = '100%' 
document.body.appendChild(iframe); //In with the new 

$(iframe.contentWindow).load(function() { //Once EVERYTHING has loaded 
    runcode() 
}); 

function runcode(){ 
    whole = iframe.contentWindow.document.body.innerText 
    var sub = "StringToSearchFor"; 
    if(whole.indexOf(sub) > -1){ //If the string is found on the page 
     console.log('The string has been found on this page') 
    }else{ //Otherwise 
     increm = increm+1 
     iframe.src = 'online.php?page=' + parseInt(increm); //Move to the next page 
     $(iframe.contentWindow).load(function() { //rinse 
      runcode() //repeat 
     }); 
    } 
} 

回答

1

你有2個負載處理程序,其中一個是自己的函數中,調用runco​​de ()。

您只需要1個,但不要將其附加到更改的內容上,刪除的內容意味着處理程序已刪除,因此請執行以下操作:

$(iframe).load(function() { runcode() }); 

OR

$(iframe).on("load", function() { runcode() }); 

OR

$(iframe).on("load", ".newchilddiv", function() { runcode() });  

編輯:也的innerText導致錯誤

whole = iframe.contentWindow.document.body.innerText 
+0

我有一個處理程序(第一個)沒成功所以我添加了第二個,看看它是否會解決,兩種情況下的結果都是相同的。我原本以爲第一個負載處理程序會在每次iframe重新導航時重新觸發。有沒有可能通過iframe重新導航的具體方式,而不僅僅是改變src? – TheDmOfJoes

+0

嘗試用別的東西替換innerText;) – yezzz

+0

innerText是我如何搜索文檔的主體以查看特定字符串是否匹配。我可以使用innerHTML,但我認爲這不會有太大的區別... – TheDmOfJoes

相關問題