0

I'm working on a JavaScript extension for WhatsApp Web, and I'm facing a problem I can't seem to solve. The goal of my extension is to correct and manipulate the text in the message input of WhatsApp Web, but I'm stuck on an issue with text injection.

I can correctly retrieve the text from the last message in the input field using elements, but I'm unable to inject this text into the message input reliably. The text is retrieved fine, but the injection doesn't work as expected.

Here’s the code I’m using to retrieve and inject the text:

Retrieval Code (getInputText):

function getInputText() { // Select all spans containing text const allSpans = document.querySelectorAll("span.selectable-text.copyable-text.false[data-lexical-text='true']"); // Take the last visible span (last written message) const lastSpan = allSpans[allSpans.length - 1]; if (!lastSpan) { return null; } const text = lastSpan.innerText.trim(); return text || null; } 

Injection Code (setInputText):

function setInputText(text) { // Select the WhatsApp Web message input field where the text should be injected const messageBox = document.querySelectorAll("[contenteditable='true']")[1]; if (!messageBox) { return; } // Inject the text into the input field messageBox.innerHTML = text.replace(/ /gm, ''); // Replace spaces if needed } 

The issue is that while I can correctly retrieve the text from the last message, the injection doesn't work as expected. I’ve tried modifying the text directly in the innerHTML of the message input element, but the text doesn’t stay injected, probably due to the dynamic nature of the WhatsApp Web interface.

What I’ve tried:

  1. Using innerHTML to inject the text into the message input.
  2. Observing mutations with a MutationObserver to detect and re-inject the text if changes happen in the DOM.
  3. Modifying the content of the elements, but this doesn’t affect the message input.

I’m looking for a way to inject text into the WhatsApp Web input field without sending it — just replacing the old text with the new one. If anyone has faced this issue or has an idea on how to work around this limitation, I would really appreciate your help! Thanks in advance!

0

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.