22

I want to make the input box think that I am typing numbers into it rather than changing the value of said input. So the first thing I did was make the input box a variable:

var input = document.querySelectorAll("input")[1];

Then what I did was focus the input box to make it seem like I had clicked in it with my mouse:

input.select();

This is the part that I am now stuck on. What I would like to do is make the input box think that I typed a number into it, say, 41. How can I do that?

0

3 Answers 3

21

You can use setTimeout and add one character each time like this :

var input = document.querySelectorAll("input")[1];
input.select(); // you can also use input.focus()
input.value="";

var text = "41";
var l=text.length;
var current = 0;
var time = 1000;


var write_text = function() {
  input.value+=text[current];
  if(current < l-1) {
    current++;
    setTimeout(function(){write_text()},time);
  } else {
    input.setAttribute('value',input.value);
  }
}
setTimeout(function(){write_text()},time);
<input type="text">
<input type="text">

This will work with any content you want to write, you simply need to adjust the variable with what you need.

Sign up to request clarification or add additional context in comments.

18 Comments

that kind of worked, but it didn't do quite what I want it to do. I forgot to mention that the input box is prefilled with "0". So when I run that function, it adds 41 to the end of it, making it display "041" in the input box. Also, when I write "input.select();", would that simulate me clicking behind the "0" in the input box? Thanks for the help. @Temani Afif
@MartyMcFarty if you want to ommit the 0, you can add at the start input.value="". And the input.select() will simply add the focus. Check again the answer i updated to get rid if 0
@MartyMcFarty maybe you want to simulate deleting content and then writing new one ?
Ty. That got rid of the 0. But it still isn't quite working how I want it to. The script I am trying to write is going to write into an input box how many coins I want to place onto a color (Roulette). So when I type into the input, "41", and click on, say, the red roulette button, it adds 41 coins to red. But when I use your code, it only changes what number is displayed in the input, rather than changing how many coins would be placed on red. So I was wondering if maybe instead of making the input box display a number, if it would be possible to simulate me typing "41".
@MartyMcFarty i think you are using input type number ? as in my code am using input text since i don't have enough information about your script
|
18

I know this was really really long ago, but I'm currently working on a very similar problem and encountering the exact same issues that the original poster was encountering, and I found this code snippet (credit):

element.dispatchEvent(new KeyboardEvent("keydown", {
    key: "e",
    keyCode: 69,        // example values.
    code: "KeyE",       // put everything you need in this object.
    which: 69,
    shiftKey: false,    // you don't need to include values
    ctrlKey: false,     // if you aren't going to use them.
    metaKey: false      // these are here for example's sake.
}));

And the specific key codes you need can be found here.

This is definitely an extremely overcomplicated way of doing things, you'd need something to translate whatever input string you have into a list of instances of KeyboardEvent, and then dispatch them one by one, but as far as I can tell this is the proper way to truly "simulate" keyboard input.

2 Comments

Hi, I am trying to come up with a Chrome extension which acts as password filler for twitter but simply setting the input value does not work. Can you help me with it?
@John you might try writing it to user's clipboard?
0

Here's an improved version of Temani Afif's answer.

I added:

  • A promise so you can execute stuff once finished filling the input
  • Parameters for selector for the input & the text
  • Random delay to make it more realistic (hardcoded to 70ms - 150ms)
function simulateTyping(text, selector) {
  return new Promise((resolve, reject) => {

    function random(min, max) { // min and max included 
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    const input = document.querySelector(selector);
    if (!input) {
      reject(`No element found for selector: ${selector}`);
      return;
    }

    input.focus();
    input.value = "";
    let current = 0;

    const writeText = () => {
      input.value += text[current];
      input.setAttribute('value', input.value); 

      if (current < text.length - 1) {
        current++;
        setTimeout(writeText, random(70, 150));
      } else {
        resolve(); // Typing finished
      }
    };

    writeText();
  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.