如何搜索href并打开部分网址

时间:2011-07-16 15:53:35

标签: javascript firefox greasemonkey

鉴于我的GM脚本运行的这个示例HTML,我想创建一个代码来捕获http://www.mycharactersID.com/ID=234223

中的ID

JavaScript获得234223后,页面上的其他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>

谢谢。

4 个答案:

答案 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);
    }
});

它应该有用。

答案 1 :(得分:1)

这是一个完整的GM脚本,可以使用。

请注意,它会立即打开所有新标签页。要按顺序打开,您需要像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");
    }
} );

答案 2 :(得分: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
}

经过测试,似乎正在运作。

答案 3 :(得分: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/