1

I am interested in creating an automated script that enter a status which are shown in textbox before submitting. I wish I can enter the textbox using keyboard event because I think there are some bot detection which implement the keyboard event check so simply textarea.value = "something" would not work.

Here are my code :

<html>

<head>
  <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
</head>

<body>
  <textarea id="reply_message"></textarea>
  <script>
    window.onload = function() {
      $('#reply_message').focus();
      var e = $.Event('keydown', {
        keyCode: 65
      });
      $('#reply_message').trigger(e);

    }
  </script>
</body>

</html>

I hope to enter letter 'a' on it, but it simply wont work, can anyone tell me how to do this? Thanks

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
dramasea
  • 3,370
  • 16
  • 49
  • 77
  • 2
    really not clear what higher level problem you are trying to solve. What do you mean by *"bot...keyboard event check"*? – charlietfl Feb 08 '17 at 18:10
  • You misunderstand what a keyevent is. You cannot use it to create keystrokes, you use it to detect. – Chris Caviness Feb 08 '17 at 18:14
  • @ibrahimmahrir do you know how to do that? – dramasea Feb 08 '17 at 18:14
  • @charlietfl For example I will put in a hidden input field called 'checktextInput' and everytime a keyboard event fired on the textarea, the value of the input field will also change subsquently, then after I post the status change, in the backend I will check if the value is matched. Note that there are too many code in it so I didnt read and analyse the code, but simply .value = "mycontent" will certainly not work – dramasea Feb 08 '17 at 18:17
  • @charlietfl Do you know how to do this? I am not sure which mechanism it used to protect against bot, the input is just my assumtion. There might be other measure, such as when user input there will be an ajax call to the server when keypress or keydown to check whether the user have entered exactly the same value. So the best bet is to simulate the entrance of the text to textarea like real human which is using keyboard event. Thanks – dramasea Feb 08 '17 at 18:25
  • Possible duplicate of [Is it possible to simulate key press events programatically?](http://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programatically) – Heretic Monkey Feb 08 '17 at 18:30
  • Did you take a look at http://stackoverflow.com/questions/17678161/how-to-trigger-enter-event-with-jquery/ ? – Our_Benefactors Feb 08 '17 at 18:35
  • @Our_Benefactors The question link you mentioned above explain how to 'listen' to an event, but not 'fire' an event – dramasea Feb 08 '17 at 18:42
  • @dramasea read it again. It certainly has the answer for what you're trying to do. – Our_Benefactors Feb 08 '17 at 19:03
  • @Our_Benefactors sorry for not reading the answer completely. I have change the key code from 13 to 65(which is keycode for 'a'). However I cant see the letter 'a' shown up in the input box – dramasea Feb 08 '17 at 19:09

1 Answers1

-3

Maybe try something like this:

function onTestChange() {
    var key = window.event.keyCode;

    // If the user has pressed enter
    if (key === 13) {
        document.getElementById("txtArea").value = document.getElementById("txtArea").value + "\n*";
        return false;
    }
    else {
        return true;
    }
}
Gchorba
  • 301
  • 2
  • 13