-1

I am creating a script that automatically clicks the "accept request" button on a social media site. To execute it I use tampermonkey.

$( document ).ready(function() {

    addFriend();

});

function addFriend(){

    $(".acceptRequest").each(function(){

        var button = this;

            setTimeout(function(){
                button.click();
            },1);

    });

}

When this runs I get the

Uncaught ReferenceError:

however when clicking the button myself it obviously is working.

So what is the diffrence between clicking it myself and letting jquery click it?

EDIT: The exact error is this:

Uncaught ReferenceError: manageRequest is not defined
    at HTMLButtonElement.onclick (requests:2107)
    at Object.trigger (userscript.html?id=2ad30bde-066c-4303-83a2-eae54846284e:5)
    at HTMLButtonElement.eval (userscript.html?id=2ad30bde-066c-4303-83a2-eae54846284e:5)
    at Function.each (userscript.html?id=2ad30bde-066c-4303-83a2-eae54846284e:5)
    at w.fn.init.each (userscript.html?id=2ad30bde-066c-4303-83a2-eae54846284e:5)
    at w.fn.init.trigger (userscript.html?id=2ad30bde-066c-4303-83a2-eae54846284e:5)
    at eval (userscript.html?id=2ad30bde-066c-4303-83a2-eae54846284e:45)

And this is the button I'm trying to click:

<button type="button" class="acceptRequest" onclick="manageRequest(bunch of userinformation here)"><span>accept</span></button>
kaspervdh
  • 16
  • 2

1 Answers1

1

Don't use click, instead use trigger, which execute handlers attached to the matched element for a given event type. So, your code would look like the one below.

setTimeout(function(){
    $(button).trigger( "click" );
},1);

You can learn more about jQuery's trigger() in here.

Alain Cruz
  • 4,757
  • 3
  • 25
  • 43