Search code examples
javascriptgreasemonkey

Simulate button click with JavaScript


I wanted to create a simple auto-skip with Greasemonkey, but I found that no solution found on Stack Overflow actually works for me.

I need to click this button:

<div tip="next" title="Next" class="linkable fa fa-arrow-right" ng-click="View.next()"></div>

It doesn't have any id nor name and is hidden deep inside many divs. I could bypass the click wiht simulating enter or right arrow press, but that also didn't work for me.


Solution

  • Try this:

    document.querySelector('div[tip="next"]').click()
    

    From what I saw these GreaseMonkey scripts run on page load. Using this I was able to simulate the click.

    // ==UserScript==
    // @name        test
    // @namespace   blargtest
    // @include     https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/
    // @version     1
    // @grant       none
    // ==/UserScript==
    setTimeout(
        function () {
            document.querySelector('div[tip="next"]').click();
        }, 1000
    );
    

    You could make that a setInterval and have it click next every second.