1

So, I'm trying to use a javascript shortcut to do a simple task on whatsapp web. I'm building ready messages for responding to my contacts, to run it, I load the whatsapp page and, after injecting jquery, I run:

$('.input').html("test")

The input is now set with the "test" message, but when I go back to the window and press "enter" the message won't send. Now if I type anything on the field, it will then be able to submit by pressing enter.

My question is: how to send an event to the .input div that actually simulates the text being typed on the form?

sigmaxf
  • 7,998
  • 15
  • 65
  • 125
  • Possible duplicate of [Force React to fire event through injected JavaScript](https://stackoverflow.com/questions/30145747/force-react-to-fire-event-through-injected-javascript) – Renati Mar 15 '18 at 08:51

2 Answers2

1

You manually trigger the change event with trigger().

$(".input").html("test").trigger('change');
4b0
  • 21,981
  • 30
  • 95
  • 142
1

You don't even need Jquery, just use this library https://github.com/bruno222/whatsapp-web-bot/blob/master/bot.js

The part you are needing is this:

Select the text area from the chatbox

messageBox = document.querySelectorAll("[spellcheck='true']")[0];

messageBox.innerHTML = message

eventFire(document.querySelector('span[data-icon="send"]'), 'click');


 //Dispatch an event (of click, por instance)
const eventFire = (el, etype) => {
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent(etype, true, true, window,0, 0, 0, 0, 0, false, false, false, false, 0, null);
    el.dispatchEvent(evt);
NicolasZ
  • 845
  • 4
  • 10
  • 26