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

后端 未结 8 748
粉色の甜心
粉色の甜心 2020-12-30 16:54

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

This is the code:

相关标签:
8条回答
  • 2020-12-30 17:19

    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);
    
    0 讨论(0)
  • 2020-12-30 17:22

    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()
    
    0 讨论(0)
  • 2020-12-30 17:25

    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();
    }
    
    0 讨论(0)
  • 2020-12-30 17:26

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

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

    0 讨论(0)
  • 2020-12-30 17:27

    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();
    
    0 讨论(0)
  • 2020-12-30 17:30

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题