2011-07-16 63 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/