Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to send text- messages on WhatsApp with javascript?

I am trying to send text messages on whatsapp web version on 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();

But the problem is , initially when no text is present the input box looks like this: enter image description here

And only when I physically write some text it changes to this:

enter image description here

And only now my script works since it requires the Send text button.

I tried Jquery code to simulate keypresses at $('.input)by 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];

}

It didn't work.

How can I get the Send text button by script?

Here is the screen recording :

like image 908
yask Avatar asked Nov 27 '22 03:11

yask


1 Answers

All options didn't work for me. Thats what I did.

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();
}
like image 129
Rodrigo Manguinho Avatar answered Dec 09 '22 17:12

Rodrigo Manguinho