I’m trying to send a message on Whatsapp Web (https://web.whatsapp.com/) only using Javascript, for study purposes.
Here is the DIV that has the message box :
<div class="fd365im1 to2l77zo bbv8nyr4 mwp4sxku gfz4du6o ag5g9lrv" contenteditable="true" role="textbox" spellcheck="true" title="Type a message" data-tab="10" data-lexical-editor="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;"><p class="selectable-text copyable-text"><br></p></div>
And here is my code :
document.getElementsByClassName('fd365im1 to2l77zo bbv8nyr4 mwp4sxku gfz4du6o ag5g9lrv')[0].innerText = 'My message content';
The code runs without errors, but the message field is not filled, nothing happens on the interface. This code used to run before, but not anymore.
What am i missing ? You can try your self accessing the whatsapp web and using Chrome devtools.
Advertisement
Answer
you can use
update*
// wait until paste all text function waitForPastedData(elem, old) { if (elem.childNodes && elem.childNodes.length == old) { return true } else { old = elem.childNodes.length return new Promise((resolve, reject) => { setTimeout(() => { resolve(waitForPastedData(elem, old)); }, 2000); }); } } async function send_text(text) { const dataTransfer = new DataTransfer(); dataTransfer.setData('text', text); const event = new ClipboardEvent('paste', { clipboardData: dataTransfer, bubbles: true }); let el = document.querySelector('#main .copyable-area [contenteditable="true"][role="textbox"]') el.focus() // select old text and replace with new document.execCommand("selectall"); el.dispatchEvent(event) return await waitForPastedData(el, 0) } await send_text('yourtext')