0

Say I visit a website which has the following code:

<input type="text" name="enter">
<input type="submit" name="button">

<a id="confirm">Confirm</a>

I need a script which I can run in the Chrome console to press the <a> element then type the text 'hello' into the input field and then click submit. I need this process to repeat every minute.

I have tried using this code.. but it doesn't do anything.

window.setInterval(function() { 
   document.querySelector("#confirm").click();     
   document.querySelector(".enter").value = "Hello";     
   document.querySelector(".button").click(); 
}, 1000);
bob jomes
  • 271
  • 1
  • 4
  • 13
  • Are you looking to build a macro? – Dylan Wheeler Jul 05 '16 at 19:26
  • @Confiqure sorry, what's that? – bob jomes Jul 05 '16 at 19:26
  • 1
    I macro is an execution of tasks by the computer (example: first click here, then type a letter, and finally click somewhere else). It's like a small set of instructions that you want the computer to be able to repeat – Dylan Wheeler Jul 05 '16 at 19:29
  • @Confiqure i suppose so yeah – bob jomes Jul 05 '16 at 19:29
  • You'd have better luck creating this type of script using a language that can you can use to take control of mouse and keyboard. I've done a number of macros using Java. Writing instructions on your website won't be of much use to you I don't think – Dylan Wheeler Jul 05 '16 at 19:31
  • 1
    Maybe because .click() is an event listener, not trigger – Chaitanya Jul 05 '16 at 19:39
  • 2
    The first line should be fine but after you looking for ".enter" and ".button" classes but in your code enter and button are the *name* of the tags. Add `class="enter"` and `class="button"` to the respective tags and everything should be work fine. – Domenico Luciani Jul 05 '16 at 19:49
  • @DomenicoLuciani I'm not able to edit the html – bob jomes Jul 06 '16 at 06:46

2 Answers2

0

If the inputs are placed in a form try this, in this case if you need to access by the name and not the class:

window.setInterval(function() { 
  var form = document.forms[0];
  document.querySelector("#confirm").click();
  form.querySelector('input[name=enter]').value ="Hello";
  form.querySelector('input[name=button]').click(); 
}, 1000);

Otherwise your script above will work if the inputs have these correct class names

-1

Instead try using trigger as:

$(document).ready(function() {
    var event = JQuery.Event("click");
    $('#confirm').click(function() {
        $('.enter').value("Hello");
        $('.button').trigger(event);
    });
    $('#confirm').trigger(event);
});

Now just put the code inside your window.setInterval() function.

Chaitanya
  • 719
  • 9
  • 23
  • not exactly. `click()`, when you do not pass args is a shortcut for `trigger('click')` – scrappedcola Jul 05 '16 at 20:02
  • That's wrong. .click() method is just a shorthand for .on( "click", handler ). Source: https://api.jquery.com/click/ – Chaitanya Jul 05 '16 at 20:05
  • try reading the entire sentence in the docs: "This method is a shortcut for .on( "click", handler ) in the first two variations, and .trigger( "click" ) in the third. ". In this case it's referencing the function call without args being a shortcut to `.trigger('click')`. – scrappedcola Jul 05 '16 at 20:07
  • 1
    The difference between the two is that `.click()` just calls `.trigger('click')` under the hood. So you are saving a function call that most of the time does not matter in any case. – scrappedcola Jul 05 '16 at 20:09
  • how do i use jquery in the chrome console – bob jomes Jul 06 '16 at 06:43
  • use CDN. Like: `var script = document.createElement("script"); script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js");` @bobjomes – Chaitanya Jul 06 '16 at 07:34
  • `Uncaught ReferenceError: JQuery is not defined` – bob jomes Jul 06 '16 at 14:29
  • Make sure to use `$(document).ready(function() { } );` http://stackoverflow.com/a/2195167/5804813 I have also edited the answer for this – Chaitanya Jul 06 '16 at 14:46