Thiago Machado
Thiago Machado

Reputation: 1

How to get text content from whatsapp web with google chrome extension

I created a script with a simple text to prefix the name of the person before the message.

The format I want to follow is:

"Thiago: Hi"

But I can't figure out why emoji messages don't do that. Simple text messages can do it successfully, but when I add any emojis, the script breaks and the name is not added.

This is the content.js i'm using for the chrome extension

  chrome.storage.local.get(["savedName"], function (result) {
    const savedName = result.savedName || "Nome Padrão";

    const editor = document.querySelector("#main div[contenteditable='true']");
    if (editor) {
      let FullMessage = `*${savedName}:*\n`;

      editor.addEventListener("keydown", (event) => {
        if (
          event.key.length === 1 &&
          !event.ctrlKey &&
          !event.altKey &&
          !event.metaKey
        ) {
          FullMessage += event.key;
        }
      });

      const sendMessage = () => {
        const userMessage = editor.innerText
          .replace(`*${savedName}:*\n`, "")
          .trim();
        const newMessage = `*${savedName}:*\n${userMessage}`;

        editor.focus();
        const range = document.createRange();
        range.selectNodeContents(editor);
        const selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(range);
        document.execCommand("insertHTML", false, newMessage);

        const inputEvent = new Event("input", { bubbles: true });
        editor.dispatchEvent(inputEvent);

        const sendButton = document.querySelector("span[data-icon='send']");
        if (sendButton) {
          setTimeout(() => sendButton.click(), 50);
        }
      };

      editor.addEventListener(
        "keydown",
        (event) => {
          if (event.key === "Enter") {
            event.preventDefault();
            event.stopImmediatePropagation();
            sendMessage();
          }
        },
        true
      );

      const sendButton = document.querySelector("span[data-icon='send']");
      if (sendButton) {
        sendButton.addEventListener("click", () => {
          sendMessage();
        });
      }
    }
  });
}

function waitForMainElement() {
  const mainElement = document.querySelector("#main");
  if (mainElement) {
    const editor = document.querySelector("#main div[contenteditable='true']");
    if (editor) {
      addNameToMessage();
    } else {
    }
  } else {
  }
  setTimeout(waitForMainElement, 1000);
}

waitForMainElement();

Upvotes: 0

Views: 92

Answers (1)

htrehrthtr
htrehrthtr

Reputation: 98

The differences I found between messages with and without emojis.

With Emojis

  • Contains an extra <span aria-label="You:"></span> immediately inside the _amk6 _amlo container.

Without Emojis

  • The outer container includes an extra class (_amjx)

Upvotes: 0

Related Questions