TurtleBo TurtleBo - 1 year ago 190
Javascript Question

Tampermonkey/GreaseMonkey AutoClick -- other topics not helping

I play a game. On the game to earn money you must feed creatures and to do that you must open the creatures link and click a feed button. I was wondering if anyone could help me create JavaScript for Tampermonkey on Chrome that will automatically click this feed button when the page loads.

And here is the code for the button:

<span id="response_container">
<input type="hidden" name="feed_token" value="5216d2a45d4185bf3f0d156a145e5aed543717388f11a12000855403a0ced24cbe4ec2cd5327e43dd0a6ee66690de3f54c55625cddfe31131c4e0e498893bfe0">
<input type="button" class="btn-submit btn-blue large" id="feed_button" value="Feed Creature">

Here is one of the things that I've tried:

var feedButton = document.querySelector (
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
feedButton.dispatchEvent (clickEvent);

and another

var btnShowObserver = function() {
var feed_button = document.getElementsByClassName('btn-submit')[0];


function myFunction() {

I have read things from
Choosing and activating the right controls on an AJAX-driven site
Clicking a button on a page using a Greasemonkey/userscript in Chrome

And I am still struggling I think it's something to do with the format of the button (it has no div and most code I've seen is referring to that.)

Any ideas? I've tried my best but I am not overly familiar with javascript

Answer Source

The code from your question,

function myFunction() {

will work.

I am assuming that since it is inside a function, you forgot to call the function with myFunction().

Or, the easier way to do this, is to remove the function altogether, and change it to the following.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download