问题
There are some links which looks like above
> <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>
And I stop in first step.I can't open each of them by script trigger. Here is which I have so far:
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);
But it doesn't work,also don't know how to make them open in new tab one by one.
回答1:
What do you mean one by one? It appears that "clicking" all of the links at once is okay?
With links, must of the time, just follow the href
instead of trying to send a click event. The following code should open just the tabs you want:
var linksToOpen = document.querySelectorAll ("td > a[href*='refid']");
for (var J = 0, numLinks = linksToOpen.length; J < numLinks; ++J) {
window.open (linksToOpen[J].href, '_blank');
}
Update for OP clarification:
To open the links with a delay between each one is slightly more complicated. Code like this will do it:
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.
);
}
}
回答2:
Does it need to be mouse clicks or can it open the links with this:
for(i=0;i<document.links.length;i++) {
if(document.links[i].target != "_blank"){
window.open(
document.links[i].href,
'_blank'
);
}
}
来源:https://stackoverflow.com/questions/10714395/how-to-make-greasemonkey-open-lots-of-links-in-new-tabs-one-by-one