Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sakehl/4a843a22f7cdb58942d635784e8152c7 to your computer and use it in GitHub Desktop.
Save sakehl/4a843a22f7cdb58942d635784e8152c7 to your computer and use it in GitHub Desktop.
Disable automatic emoticon → emoji conversion in WhatsApp Web
// ==UserScript==
// @name WhatsApp Emoticon Preserver
// @namespace https://gist.github.com/sakehl/4a843a22f7cdb58942d635784e8152c7
// @version 0.6
// @author varkor, sakehl
// @description Disable automatic emoticon → emoji conversion in WhatsApp Web
// @match https://web.whatsapp.com/
// @grant none
// ==/UserScript==
(() => {
"use strict";
if (window.location.hostname !== "web.whatsapp.com") {
window.location = "https://web.whatsapp.com/";
} else {
const fixed = new Set();
const volatile = ["(y)", "(Y)", "(n)", "(N)", ":-)", ":)", ":-(", ":(", ":'(", ":-p", ":p", ":-P", ":P", ":O", "8)", ":-|", ":|", ":-\\", ":\\", ":-D", ":D", ":-*", "<3", "^_^", ">_<", ";-)"];
const fix = function () {
const input = document.querySelector('[data-testid=conversation-compose-box-input]');
if (input && !fixed.has(input)) {
input.addEventListener("beforeinput", (event) => {
const selection = window.getSelection();
if (selection.rangeCount >= 1) {
const range = selection.getRangeAt(0);
let containers = [range.startContainer, range.endContainer];
let start = range.startOffset;
let end = range.endOffset;
const walker = document.createTreeWalker(input, NodeFilter.SHOW_TEXT);
let node;
while (node = walker.nextNode()) {
if(containers[0] == node && containers[1] == node){
let possible_smiley = node.nodeValue.slice(start-2, start) + event.data;
let found_smiley = volatile.find(smiley => possible_smiley.endsWith(smiley));
if(!(found_smiley === undefined)){
node.nodeValue = node.nodeValue + "\u200B"
start += 1;
end += 1;
selection.removeAllRanges();
const range = document.createRange();
range.setStart(containers[0], start);
range.setEnd(containers[1], end);
selection.addRange(range);
}
}
}
}
});
fixed.add(input);
}
};
window.addEventListener("click", fix);
// window.addEventListener("paste", () => setTimeout(fix, 400));
fix();
}
})();
@ahmetberkinc
Copy link

Perfect! Thanks :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment