1

I was wondering if anyone could help me create JavaScript for Tampermonkey on Chrome that will automatically click this button when the page loads.

And here is the code for the button:

<span id="container">
    <input type="hidden" name="token" value="5216d2a45d4185bf3f0d156a145e5aed543717388f11a12000855403a0ced24cbe4ec2cd5327e43dd0a6ee66690de3f54c55625cddfe31131c4e0e498893bfe0">
    <input type="button" class="btn-submit btn-blue large" id="button" value="Button">
</span>

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

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

and another

var btnShowObserver = function() {
  var button = document.getElementsByClassName('btn-submit')[0];
  button.getElementsByClassName('btn_confirm')[0].click();
}

another

function myFunction() {
    document.getElementById("button").click();
}

I have read things from Choosing and activating the right controls on an AJAX-driven site to 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

LucyTurtle
  • 1,043
  • 1
  • 17
  • 37
  • What are you trying to accomplish? Are you trying to "feed creature", close the popup, then click "next"? – ZomoXYZ Jun 05 '16 at 20:14
  • Just click feed creature. There should be no popup – LucyTurtle Jun 05 '16 at 20:21
  • If you can also add clicking next that would be cool, but at the moment just clicking feed would be spectacular – LucyTurtle Jun 05 '16 at 20:21
  • Have you tried using `.click()`? http://www.w3schools.com/jsref/met_html_click.asp – ZomoXYZ Jun 05 '16 at 20:26
  • That's HTML. I'm pretty sure Tampermonkey only uses javascript. So how would I implement that HTML on the site? – LucyTurtle Jun 05 '16 at 20:30
  • `.click()` *is* JavaScript. HTML is HyperText **Markup** Language, it is a markup language and not a scripting language. The first code block you used was HTML. What you are looking for is `document.getElementById("feed_button").click()`, which you already have in your question. I am assuming that since it is inside a function, you forgot to call the function. – ZomoXYZ Jun 05 '16 at 20:36

1 Answers1

2

The code from your question,

function myFunction() {
    document.getElementById("button").click();
}

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.

document.getElementById("button").click();
LucyTurtle
  • 1,043
  • 1
  • 17
  • 37
ZomoXYZ
  • 1,763
  • 21
  • 44