I'm trying to make a script (for Greasemonkey) that (while visiting a specific page),
if you click the link that I insert into the page
(the link "does nothing", i.e. I use event.preventDefault();
)
to open all links in page in new tabs,
but with a delay between each tab opening, eg. 500 ms.
I have this code (based on this answer)
var z = ($('table td').find('a')); // selection via jQuery
$("#mylink").on('click', function(event){
event.preventDefault();
var $this = $(this);
for (i=0; i<z.length; i++) {
window.open(z[i].href, '_blank');
}
});
The above works, but opens all links simultaneously.
So, I have tried enclosing the window.open
line into either:
setInterval(function() {
window.open(z[i].href, '_blank');
}, 500);
or:
setInterval("window.open('"+z[i].href+"','_blank')", 500);
but didn't help:
both did nothing, and I only get continuous TypeError: z[i] is undefined
errors in Browser Console when I click the link.
For what it's worth, if the fact that it's a link is what's causing the problem,
I have also the code for creating a button, instead of a link:
var z = ($('table td').find('a')); // selection via jQuery
var button = document.createElement("button");
button.id = 'mybutton';
button.type = "button";
button.value = "im a button";
context.appendChild(button);
$("#mybutton").on('click', function(event){
event.preventDefault();
var $this = $(this);
for (i=0; i<z.length; i++) {
window.open(z[i].href, '_blank');
}
});
Changing the window.open
line in either of the two ways described above doesn't help,
and I get the same errors in Browser Console.