我想用javascript在WhatsApp上发送短信?

时间:2015-01-23 20:33:01

标签: javascript jquery web whatsapp

我正在尝试在chrome上发送whatsapp web版本的短信。 (www.web.whatsapp.com

这是代码:



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

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




但问题是,最初没有文本时输入框如下所示: enter image description here

只有当我在物理上写一些文字时,才会改变这一点:

enter image description here

现在我的脚本才能运行,因为它需要Send text button

我尝试使用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];

}




它没有用。

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

Here is the screen recording

8 个答案:

答案 0 :(得分:13)

所有选项对我都不起作用。多数民众赞成我所做的。

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

答案 1 :(得分:8)

在打开对话时尝试使用此代码段:

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

答案 2 :(得分:5)

正如Khalid Lafi所说,这是正确的剧本。他的代码在执行

时会返回错误
dispatch(document.querySelector("#compose-input div"), "textInput", "hello!");

这是因为您应该使用“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();

希望这有帮助。

答案 3 :(得分:3)

以下是更新的脚本。希望这会有所帮助。

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

答案 4 :(得分:0)

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

});

你必须比较==你在指定的地方=

答案 5 :(得分:0)

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

如果您打算填充文本字段,则只需设置val即可。在jQuery中,可以使用.val()来完成,例如:

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

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

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

答案 6 :(得分:0)

这是工作脚本:

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

答案 7 :(得分:0)

此功能将于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);
}

相关问题