TheGreenPig / Apate

A BetterDiscord Plugin that allows End-to-End encryption and hiding messages in messages!
65 stars 17 forks source link

Corrupted message when including an emoji #9

Closed fabJunior closed 3 years ago

fabJunior commented 3 years ago

When the second word of the cover message is an emoji, on certain occasions the hidden message will break.

For example, if I want to hide the message "done" with the message Mhhh :shibathink: *done* the result is as follows: Discord_jPqer38d36

I also tried to hide the message "ok" with so :AquaInspect: mhh *ok* and the result: Discord_deOXYaUcJz

With a longer message Alright :ablobnod: will it work *or will it fail?*: Discord_kNOmBU3qAB


Usually, the last character is altered/deleted (or last few characters) and in some occasions the whole hidden message is reduced to nothing, as if no message was hidden. However, sometimes, nothing bad happens, so it's a bit of a gamble.

What I've understood is that when I type a message, like Mhhh :shibathink: *done*, the text that the reveal worker will get is Mhhh (with a trailing space) instead of Mhhh :shibathink: and that's what's breaking some messages.

In fact, it seems to work fine when I make the revealWorker get the innerHTML of the message instead of the textContent in the addHiddenMessageBanners method.

// const textContent = messageContainer.querySelector(
//  `div[class*="contents-"][role="document"] > div[class*="markup-"][class*="messageContent-"]`
// ).textContent;

const textContent = messageContainer.querySelector(
    `div[class*="contents-"][role="document"] > div[class*="markup-"][class*="messageContent-"]`
).innerHTML;

After fiddling a bit with the code, I came up with this possibly more elegant fix in addHiddenMessageBanners where the const textContent is initialized:

const domMessage = messageContainer.querySelector(
    `div[class*="contents-"][role="document"] > div[class*="markup-"][class*="messageContent-"]`
).cloneNode(true);

const messageEmojis = domMessage.querySelectorAll(`span[class*="emojiContainer-"]`);

for (let emoji of messageEmojis) {
    emoji.innerHTML = emoji.children[0].alt;
}

const textContent = domMessage.textContent;

The result is textContent having the proper :emoji: text:

Discord_G5LAL6V7mM

TheGreenPig commented 3 years ago

I implemented your suggested fix, I hope it works now.