2012-01-23 68 views
2

我不知道mutch關於JavaScript,但我發現這個greasemonekey腳本,我試圖編輯。如果result.responseText.match不存在

var rating = document.links; 

for (i = 0; i < rating.length; i++) { 
if (rating[i].href.indexOf("/shows/") != -1){ 

    GM_xmlhttpRequest({ 
     method: 'get', 
     url: rating[i].href, 
     onload: function (i) {return function(result) { 

       rate = result.responseText.match(/<span class="rating">(.*)<\/span>/); 
       result = rate[1].substring(0,3); 

       rat = document.createElement("div"); 
       rat.className = 'rate'; 
       rat.innerHTML = result; 
       rating[i].parentNode.insertBefore(rat, rating[i].nextSibling); 
     }}(i) 
    }); 
} 
} 

所以搜索網頁上的所有鏈接,如果鏈接中包含/顯示/它搜索的值:

<span class="rating"><\span> 

如果發現它顯示的結果在我創建的股利。到現在爲止還挺好!

但其中的一些環節doenst有<span class="rating"><\span>

在那些我喜歡說「沒有發現」,但我無法理解了它如何很多:(

回答

1
rate = result.responseText.match(/<span class="rating">(.*)<\/span>/); 
if(rate){ 
    // Existing code. 
    result = rate[1].substring(0,3); 

    rat = document.createElement("div"); 
    rat.className = 'rate'; 
    rat.innerHTML = result; 
    rating[i].parentNode.insertBefore(rat, rating[i].nextSibling); 
}else{ 
    // Your new "not found" code. 
    // Something like this, depending upon what you want to do: 

    rat = document.createElement("div"); 
    rat.className = 'rate-not-found'; 
    rat.innerHTML = "Rate not found."; 
    rating[i].parentNode.insertBefore(rat, rating[i].nextSibling); 
} 
+0

完美,感謝鏈接! –