2012-01-23 20 views
2

私はjavascriptについてmutchを知らないが、私が編集しようとしているこの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) 
    }); 
} 
} 

だから、ページ上のすべてのリンクを検索し、リンクが含まれている場合/ショー/それが値を検索:その見つかったそれは私が作成したdiv要素に結果を示している場合

<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

完璧、おかげでたくさん:(ためにどのようにそれを考え出したことができないと言うのが好き、これらのリンクを<span class="rating"><\span>

を持ってdoenst ! –

関連する問題