5

I am trying to loop through all the links on a site and autoclick some links in order to vote for here is what I have so far:

    function x(){
        for(var e in document.getElementsByTagName("a")){ 
            alert(e.getAttribute("href"))
            e.click;
        }
    }

This currently does not work I think it maybe to do with something simple like the braces/; key, I'm an absolute beginner to javascript so please bear with me. I assume you get the drift of what I want to do, I have completed this task in another language but still did not get the vote to register, I believe this maybe something to do with the site using jquery? My question is, How can I get this simple script working for a start, and 2) Is there a different click method for Jquery I can use instead of what I have up there 3) How Can I check for 6 specific URLs and click only these. I also need to execute this from the browser using javascript:xxx_code_here

Any ideas?

Thank you

Nookster
  • 107
  • 1
  • 2
  • 13

6 Answers6

4

use jquery like

$("a").each(function ()
{
   $(this).trigger('click');//for clicking element
   var href = $(this).attr("href");
});

You can use trigger:

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • My problem is, I wish to execute the code from a browser, Is this possible? – Nookster May 18 '12 at 07:35
  • @Nookster - if you want to redirect to other page on click...than you can use "location.href = linkhref" which will cause page to redirect to other page.... – Pranay Rana May 18 '12 at 07:36
  • How can I execute it in a browser using format such as this: javascript:function x() { $("a").each(function (i, a) {alert(a.href);a.click();});}x(); With this code I get a.click function not defined – Nookster May 18 '12 at 07:39
  • @Nookster - you can do is $(this).trigger('click');//for clicking element – Pranay Rana May 18 '12 at 07:40
  • It's not working executing via the browser address bar, this is what I need it for - any ideas? – Nookster May 18 '12 at 07:45
  • Run this code via Inspect->Console while on a page: `var links = document.getElementsByTagName(“a”); for (i = 0; i < links.length; i++) { window.open(links[i].href,''); }` – Norm Dec 20 '16 at 01:45
1

With JQuery you could use this :

$("a").each(function(){
    alert($(this).attr('href'));
    $(this).trigger('click');
};
jbduzan
  • 1,106
  • 1
  • 14
  • 30
  • This looks good, But I need to execute it via the browser with javascript:xx_code_here I cannot get this code to work like this, any ideas? – Nookster May 18 '12 at 07:45
  • you can execute jquery code via browser, you have to use basic javascript without lib. Your code seems good to do it, just change e.click in this.click(), should work – jbduzan May 18 '12 at 07:48
  • How can I check for specific URLs and click only those ? – Nookster May 18 '12 at 07:52
1

This Worked for Me

Each link is opened in New Tab

$("a").each(function ()
{
    window.open($(this).attr('href'),"_blank");
});

If At first time this time links are not opened , then you have to enable popups on the webpage .

Adi
  • 903
  • 2
  • 15
  • 25
0

You can do this with pure-javascript

function x() { // Please find a better name!
    for(var a in document.getElementsByTagName("a")) {
        alert(a.href);
        a.click(); // Careful, IE only, see comments
    }
}

With jQuery, you have shorter code:

function x() {
    $("a").each(function (i, a) {
        alert(a.href);
        a.click();
    });
}
Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42
  • Hello, Thanks for the reply It's called X because I want to excute it from the browser like so: javascript:function x() { $("a").each(function (i, a) {alert(a.href);a.click();});}x(); However, This returns with a.click [undefined] is not a function, it displays the alert however.. – Nookster May 18 '12 at 07:38
  • 1
    As far as I'm aware the click() method is IE only, so if your planning on using pure js you should probably write a click method yourself. This thread describes how: http://stackoverflow.com/questions/902713/how-do-i-automatically-click-a-link-with-javascript – Simon May 18 '12 at 07:43
  • I need to invoke the click as if it was clicked via the user, I think jquery handles it but I also need to execute this in the browser address bar with javascript:xxxx any ideas? – Nookster May 18 '12 at 07:46
0
$("a").each(function ()
{
    window.location.href = $(this).attr('href');
    // $(this).trigger('click');
});
Priyank Patel
  • 3,495
  • 20
  • 20
0
javascript: var validUrls = ["http://targetsite.com/votefraud1","http://targetsite.com/votefraud2","http://targetsite.com/votefraud3"];

function x() { 
 for(var a in document.getElementsByTagName("a")) {
   if(validUrls.indexOf(a.href) != -1){
      window.open(a.href,'');
    }
  }
}

x();

If this doesn't work we might need more info on what exactly the click event is supposed to do on whatever site you're trying to game the voting system on

nvuono
  • 3,323
  • 26
  • 27