Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  83] [ 2]  / answers: 1 / hits: 18066  / 10 Years ago, fri, january 23, 2015, 12:00:00

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



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



enter



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 :


More From » jquery

 Answers
6

Try this snippet, while opening a conversation:



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()

[#68107] Wednesday, January 21, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gideons

Total Points: 197
Total Questions: 106
Total Answers: 108

Location: Moldova
Member since Sat, Jan 29, 2022
2 Years ago
;