parkour86
parkour86

Reputation: 193

Unable to click form button using tampermonkey

I am working on a tampermonkey/greasemonkey script that will click a button on a webpage when the page is loaded. Here is the code for the form.

<form>
    <input type="hidden" name="xsrf_token" value="497903b8" />
    <input type="hidden" name="do" value="" />
    <input type="hidden" name="code" value="1APdij" />
    <div data-do="entry_insert" class="sidebar__entry-insert">
        Click Here
        <span class="sidebar__entry__points">(3P)</span>
    </div>
    <div class="sidebar__entry-loading is-disabled is-hidden">
        <i class="fa fa-refresh fa-spin"></i>
        Please wait...
    </div>
</form>

Here is the one liner I am trying to use to click the button on the page. When the page loads and executes the one liner it doesn't do anything. I have also tried submit().

$('form').off().click();

Any help would be great. Thanks.

Upvotes: 0

Views: 833

Answers (1)

ekuusela
ekuusela

Reputation: 5292

Instead of the form, target the actual button when triggering the click event.

$('form [data-do="entry_insert"]').click()

Triggering submit for the form would work too if the JS for that form was listening for it.

Non-jQuery version:

var target = document.querySelectorAll('form [data-do="entry_insert"]')[0];
var e = new MouseEvent('click', {
   'view': window,
   'bubbles': true,
   'cancelable': true
});
target.dispatchEvent(e);

Upvotes: 1

Related Questions