0

I created a bookmarklet for a site a while back that ticks multiple checkboxes on a page:

javascript:javascript:;$("input[name='unfollow[]']").attr({checked:true});

Unfortunately the UI has changed and instead of checkboxes there are buttons that need to be clicked. Here is an example of the HTML of one of the buttons:

<button class="process mode-basic Unfollow" data-verb="unfollow">Unfollow</button>

There can be up to 100 of these buttons. How do I create a bookmarklet that clicks all of these buttons? Is it possible to build up a delay between each click?

Thanks.

iagdotme
  • 1,033
  • 2
  • 21
  • 38

2 Answers2

1
$('button[data-verb="unfollow"]').on({
    click: function() {
        $('#result').append($(this).text() + ' was clicked<br/>');
    }
});

$('#unfollow-all').on({
    click: function() {
        $('button[data-verb="unfollow"]').each(function(index) {
            var el = $(this);
            setTimeout(function() {
                clickButton(el);
            }, 500 * index);
        });
    }
});

function clickButton(button) {
    button.click();
}

fiddle: http://jsfiddle.net/6AJNc/1/

devnull
  • 2,790
  • 22
  • 25
  • Thanks! Unfortunately this doesn't work within a bookmarklet. Perhaps that the bookmarklet needs to be marked up as one function. Not sure. – iagdotme Sep 11 '13 at 11:23
  • if you move things around it does work :P accepted answer does the same thing, so I won't update mine – devnull Sep 11 '13 at 12:32
  • Thanks. I am sure it does, but I managed to get the other answer to work really well for me. I feel bad now! I'll vote your answer up because it was a very good one. – iagdotme Sep 11 '13 at 16:57
  • why would you feel bad? :) you got two solutions for your problem :P – devnull Sep 11 '13 at 17:40
1

Assuming the page has jQuery loaded, you can click each one with a delay in between:

(function(){
    var unfollowButtons = $('button.Unfollow');
    var index = unfollowButtons.length-1;
    unfollow();

    function unfollow(){
        if(index >= 0){
            $(unfollowButtons[index--]).click();
            setTimeout(unfollow, 500);
        }
    }
})();
Ben Hutchison
  • 4,823
  • 4
  • 26
  • 25