我正在尝试使用 javascript 在 WhatsApp 上发送短信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28118268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:26:37  来源:igfitidea点击:

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

javascriptjquery

提问by yask

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

我正在尝试在 chrome 上的 whatsapp 网络版本上发送短信。( 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.

直到现在我的脚本才有效,因为它需要Send text button.

I tried Jquery code to simulate keypresses at $('.input)by following function:

我尝试使用 Jquery 代码通过以下函数模拟 $('.input) 处的按键操作:

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 textbutton by script?

如何Send text通过脚本获取按钮?

Here is the screen recording:

这是屏幕录制

回答by Rodrigo Manguinho

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

回答by Khalid Lafi

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

回答by Luca d'Addabbo

As Khalid Lafi said, this is the correct script. His code does will return an error when executing

正如 Khalid Lafi 所说,这是正确的脚本。他的代码确实会在执行时返回错误

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

This is because you should use "input.div" instead of "#compose-input div". The following script is working for me.

这是因为您应该使用“input.div”而不是“#compose-input div”。以下脚本对我有用。

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

Hope this helps.

希望这可以帮助。

回答by Shubham

The following is the updated script. Hope this helps.

以下是更新后的脚本。希望这可以帮助。

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

回答by nino.porcino

This works in Dec 2019. Original snippet by Shubham modified by Cami Rodriguez (see comments above).

这在 2019 年 12 月有效。 Shubham 的原始片段由 Cami Rodriguez 修改(见上面的评论)。

function write_in_chat(text) {
   var input = document.querySelector('#main [contenteditable~=true]');
   setTimeout(() => {       
      input.innerHTML = text;
      input.dispatchEvent(new Event('input', {bubbles: true}));
      var button = document.querySelector('button>span[data-icon="send"]').parentElement;
      button.click();
   }, 500);
}

回答by Nadeemmnn Mohd

$(".input").on("keypress",function(e){ 
 if(e.which == 32 ||  e.which == 13){ alert('msg sent')};

});

you have to compare == where as you are assigning =

您必须在分配时比较 ==

回答by Keithamus

Firing an event with a KeyPress into an input will not actually populate the text area because the population is part of the native event.

使用 KeyPress 将事件触发到输入中实际上不会填充文本区域,因为填充是本机事件的一部分。

If your intention is to populate a text field, you'll simply need to set the val. In jQuery this can be done with .val(), e.g.:

如果您打算填充文本字段,则只需设置 val。在 jQuery 中,这可以通过.val(),例如:

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

The WhatsApp input box probably has an event listener waiting for a keyup event, and if the field is populated, switching it to the send button. If this is the case then you canmanually trigger an event which will trigger WhatsApp's (non-native) code.

WhatsApp 输入框可能有一个等待 keyup 事件的事件侦听器,如果该字段已填充,则将其切换到发送按钮。如果是这种情况,那么您可以手动触发一个事件,该事件将触发 WhatsApp 的(非本地)代码。

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

回答by Liad Livnat

This is the 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();