Am I trying to send text messages to WhatsApp using javascript?

I am trying to send text messages to whatsapp web version in chrome. ( www.web.whatsapp.com )

This is the code:

document.getElementsByClassName("input")[1].innerHTML="This message was written via JS script! ";

var input = document.getElementsByClassName("icon btn-icon icon-send");
input[0].click();
Run codeHide result

But the problem is that initially, when the text is missing, the input field looks like this: enter image description here

And only when I physically write some text, does it change to this:

enter image description here

And only now my script is working, as it is required for it Send text button.

I tried jQuery code to simulate keystrokes in $ ('. Input) using the following function:

function pressKey() {
  var e = jQuery.Event("keypress");
  e.which = 32; // # space
  $(".input").trigger(e)[1];
  e.which = 91;
  $(".input").trigger(e)[1];
  e.which = 32; // # space
  $(".input").trigger(e)[1];
  e.which = 32; // # space
  $(".input").trigger(e)[1];

}
Run codeHide result

This did not work.

How can I get a button Send texton a script?

Here is the screen recording :

+4
7

, :

function dispatch(target, eventType, char) {
   var evt = document.createEvent("TextEvent");    
   evt.initTextEvent (eventType, true, true, window, char, 0, "en-US");
   target.focus();
   target.dispatchEvent(evt);
}


dispatch(document.querySelector("#compose-input div"), "textInput", "hello!");

function triggerClick() {
  var event = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': true
  });
  document.querySelector(".icon.btn-icon.icon-send").dispatchEvent(event)
}
triggerClick()
+8

. .

function sendMessage(message) {
    var evt = new Event('input', {
        bubbles: true
    });

    var input = document.querySelector("div.input");
    input.innerHTML = message;
    input.dispatchEvent(evt);

    document.querySelector(".icon-send").click();
}
+13

, script.

dispatch(document.querySelector("#compose-input div"), "textInput", "hello!");

, "input.div" "# compose-input div". script.

function dispatch(target, eventType, char) {
    var evt = document.createEvent("TextEvent");    
    evt.initTextEvent (eventType, true, true, window, char, 0, "en-US");
    target.focus();
    target.dispatchEvent(evt);
}


dispatch(document.querySelector("div.input"), "textInput", "hello!");

function triggerClick() {
var event = new MouseEvent('click', {
  'view': window,
  'bubbles': true,
  'cancelable': true
 });
document.querySelector(".icon.btn-icon.icon-send").dispatchEvent(event);
}

triggerClick();

, .

+5

script. , .

var input = document.querySelector('.block-compose .input');
setTimeout(function(){
    for(var j = 0; j < 1; j++) {
        input.innerHTML = "Hello";
        input.dispatchEvent(new Event('input', {bubbles: true}));
        var button = document.querySelector('.block-compose button.icon-send');
        button.click();
    }
},1000);
+2
$(".input").on("keypress",function(e){ 
 if(e.which == 32 ||  e.which == 13){ alert('msg sent')};

});

== , =

0

KeyPress , .

If you intend to fill in the text box, you just need to set val. In jQuery, this can be done using .val(), for example:

function pressKey() {
  $(".input").val('test');
}

There is probably an event listener in the WhatsApp input field waiting for a keyup event, and if the field is full, switch it to the submit button. If so, you can manually trigger an event that will trigger a WhatsApp code (not native).

function pressKey() {
  $(".input").val('test').trigger($.Event('keyup'));
}
0
source

This is a working script:

  function dispatch(target, eventType, char) {
            var evt = document.createEvent("TextEvent");
            evt.initTextEvent (eventType, true, true, window, char, 0, "en-US");
            target.focus();
            target.dispatchEvent(evt);
        }


        dispatch(document.querySelector(".input-container > .input-emoji .input"), "textInput", "hello!");

        function triggerClick() {
            var event = new MouseEvent('click', {
                'view': window,
                'bubbles': true,
                'cancelable': true
            });
            document.querySelector(".icon.btn-icon.icon-send").dispatchEvent(event)
        }
        triggerClick();
0
source

All Articles