我想用javascript在WhatsApp上发送短信? [英] I am trying to send text- messages on WhatsApp with javascript?
本文介绍了我想用javascript在WhatsApp上发送短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在chrome上发送whatsapp web版本的短信。
(:
解决方案
在打开对话时尝试此片段:
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!);
函数triggerClick(){
var event = new MouseEvent('click',{
'view':window,
'bubbles':true,
'cancelable':true
});
document.querySelector(。icon.btn-icon.icon-send)。dispatchEvent(event)
}
triggerClick()
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:
And only when I physically write some text it changes to this:
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 :
解决方案
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()
这篇关于我想用javascript在WhatsApp上发送短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文