2011-07-16 59 views
1

的開放部分鑑於我的GM腳本運行,我想使捕獲從http://www.mycharactersID.com/ID=234223如何搜索href和URL

的ID JavaScript的有234223後代碼這個例子HTML和其他ID的上這一頁。然後用新標籤中的不同鏈接打開它們。我的意思是HTML中的所有ID。

例如:window.open("http://www.mycharactersID.com/TalkID=234223")

Burada takılan <b>12</b> karakter bulunmaktadır.<br><br> 


      <table border="0" cellpadding="2" cellspacing="0" width="400"> 

      <tbody><tr> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=1858480">Alexandra&nbsp;Anthony</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr class="DarkColumnHL"> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=2624518">Igor&nbsp;Arnaudov</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=1318025">Ashanti&nbsp;Dunn</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr class="DarkColumnHL"> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=621305">Abigail&nbsp;Eliopoulos</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=1853122">Fynn Linus&nbsp;Hargasser</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr class="DarkColumnHL"> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=2347156">Sabela&nbsp;Hernani</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=595514">Maaja&nbsp;Jürisson</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr class="DarkColumnHL"> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=1329193">Sixtine&nbsp;Karakaya</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=156315">Umut&nbsp;Koç</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr class="DarkColumnHL"> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=821852">Shanice&nbsp;Manning</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=393396">Demircan&nbsp;Özdal</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      <tr class="DarkColumnHL"> 
       <td width="234">&nbsp; 

       <a href="CharacterDetails.asp?action=view&amp;CharacterID=550724">Mélodie&nbsp;Stavropoulos</a> 

       </td> 
       <td align="right" width="158"> 

       &nbsp; </td> 
      </tr> 

      </tbody></table> 

      <br> 
      Not: En fazla 100 karakter listelenmektedir.<br><br> 

謝謝。

+0

爲什麼你的代碼在問題的一半? –

+0

你的意思是'HTTP://www.mycharactersID.com/ ID = 123222'?注意'?'字符。 –

回答

1

這裏有一個完整的通用腳本,將工作。

請注意,它會一次打開所有新選項卡。要按順序打開,您需要將它們排隊,如this answer

// ==UserScript== 
// @name   _Open lots of tabs 
// @include   http://mycharactersID.com/YOUR_PATH/* 
// @include   http://www.mycharactersID.com/YOUR_PATH/* 
// @require   http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js 
// ==/UserScript== 

$("td > a[href*='CharacterID']").each (function() { 
    var ID = this.href.match (/CharacterID=(\d+)$/i); 
    if (ID && ID.length > 1) { 
     ID = ID[1]; 
     window.open ("http://www.mycharactersID.com/TalkID=" + ID, "_blank"); 
    } 
}); 
+0

你可以改變你的腳本到我的新的HTML。我無法編輯它。當我編輯腳本時,İt不工作:( –

+1

完成。對於這些類型的問題,選擇節點選擇符取決於頁面的精確細節。因此,請始終鏈接到頁面,如果它是公開可見的。 ,將頁面的源文件保存到pastebin.com並鏈接到該頁面(如果頁面不公開或需要註冊) –

+0

你是偉大的人!謝謝你的幫助,每次我問一些問題,你幫助了我。 ! –

2

下面是使用JQuery解決方案:

$('table tr td a[href^="http://www.mycharactersID.com/ID="]').each(function(){ 
    var regExp=/id=(\d+)/i; 
    var matches=$(this).attr('href').match(regExp); 
    if(matches) 
    { 
     window.open('url/ID='+parseInt(matches[1]), w_name, w_params); 
    } 
}); 

它應該工作。

+0

基本正確,但有3個問題:(1)選擇器不夠具體。通過測試所有的鏈接,爲這樣一個小/通用的術語;假陽性鏈接將被發現的可能性很大。 (2)如果沒有匹配會發生什麼? (一個例外!)。 (3)可能會在新網址中添加一個額外的「id =」。調整代碼,我會upvote。 –

+0

查看編輯後的代碼。 –

+0

好多了。 +1,因爲我說我會的,但仍然存在錯誤:'matches [1]'總是未定義的,'parseInt('id = 666')''是'NaN',而不是666.看看正則表達式其他線索的答案。 ;) –

0

那麼我不知道如何打開鏈接,但捕獲數據很容易。

var idArray = document.body.innerHTML.match(/"http:\/\/www\.mycharactersID\.com\/ID=([0-9]*)"/g); //captures the entire link 
for(var i = 0; i < idArray.length; i++){ 
    idArray[i] = idArray[i].replace(/"http:\/\/www\.mycharactersID\.com\/ID=([0-9]*)"/, "$1"); //Now just the ID part 
} 

經過測試,似乎正在工作。

0

這個作品

HTML

<a href="http://www.mydomain.com/ID=25645" onclick="OpenMyPage(this); return false;">Click HyperLink</a> 

的JavaScript

function OpenMyPage(hyperlink) {var source = hyperlink.attributes.href.value; var pattern = new RegExp("(\\d+)$"); source.match(pattern); alert(RegExp.$1);} 

http://jsfiddle.net/FunkyFresh84/ZEAMZ/35/