如何让greasemonkey一个接一个地点击很多链接?

时间:2012-05-23 06:10:03

标签: mouseevent greasemonkey

有些链接如上所示

> <td><a href="http://Lucifase.com/pages/2000.php?refid=2000"
> target="_blank">2000</a><br></td> <td><a
> href="http://Lucifase.com/pages/3000.php?refid=3000"
> target="_blank">3000</a><br></td> <td><a
> href="http://Lucifase.com/pages/4000.php?refid=4000"
> target="_blank">4000</a><br></td> <td><a
> href="http://Lucifase.com/pages/5000.php?refid=5000"
> target="_blank">5000</a><br></td> <td><a
> href="http://Lucifase.com/pages/6000.php?refid=6000"
> target="_blank">6000</a><br></td>

我在第一步停止。我无法通过脚本触发器打开它们。这是我到目前为止:

 setTimeout(function() {
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("click", true, true, window,
                       0, 0, 0, 0, 0,
                       false, false, false, false,
                       0, null);
 var links = document.getElementsByTagName('a');
             if(links.href.search('refid') >= 0)

    links.dispatchEvent(evt);
 }, 1000);

但它不起作用,也不知道如何在新标签中逐一打开它们。

2 个答案:

答案 0 :(得分:3)

你是什么意思一个接一个?似乎一次“点击”所有链接都可以吗?

使用链接时,必须遵循href而不是尝试发送点击事件。以下代码应该只打开您想要的选项卡:

var linksToOpen = document.querySelectorAll ("td > a[href*='refid']");
for (var J = 0, numLinks = linksToOpen.length;  J < numLinks;  ++J) {
    window.open (linksToOpen[J].href, '_blank');
}


OP澄清更新:
要在每个链接之间延迟打开链接稍微复杂一些。像这样的代码会这样做:

var linksToOpen = document.querySelectorAll ("td > a[href*='refid']");

//--- linksToOpen is a NodeList, we want an array of links...
var linksArray  = [];
for (var J = 0, numLinks = linksToOpen.length;  J < numLinks;  ++J) {
    linksArray.push (linksToOpen[J].href);
}

openLinksOnDelay (linksArray);

function openLinksOnDelay (linkArray) {
    //--- Pop the first link off the array...
    var linkToOpen  = linkArray.shift ();
    if (linkToOpen)
        window.open (linkToOpen, '_blank');

    //--- Open the next of the remaining links after a delay.
    if (linkArray.length) {
        setTimeout ( function() {
                openLinksOnDelay (linkArray);
            },
            1000    //--- 1 second.  Use 60000 for 1 minute.
        );
    }
}

答案 1 :(得分:2)

是否需要点击鼠标,或者是否可以打开链接:

for(i=0;i<document.links.length;i++) {
  if(document.links[i].target != "_blank"){
     window.open(
       document.links[i].href,
       '_blank'
     );
  }
}