Skip to content

Instantly share code, notes, and snippets.

@envp
Last active July 31, 2016 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save envp/46197a793bdb3f45cd17b78a8af94282 to your computer and use it in GitHub Desktop.
Save envp/46197a793bdb3f45cd17b78a8af94282 to your computer and use it in GitHub Desktop.
A Simple script that converts text smileys to emojis
// ==UserScript==
// @name WhatsApp Text2Emoji
// @namespace http://tampermonkey.net/
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @version 0.2
// @description Simple script that converts text smileys to emojis
// @author Vaibhav Yenamandra
// @match https://web.whatsapp.com/
// @grant none
// ==/UserScript==
// SO Reference: http://stackoverflow.com/questions/4233265/contenteditable-set-caret-at-the-end-of-the-text-cross-browser
'use strict()';
// More to come :)
var emoji_map = {
':)' : "😊",
":')" : "😁",
';)' : "πŸ˜‰",
':D' : "πŸ˜ƒ",
'XD' : "πŸ˜†",
':P' : "πŸ˜›",
';P' : "😜",
'XP' : "😝",
'B)' : "😎"
};
var smileys = Object.keys(emoji_map);
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
function text_emoji_main(jNode) {
jNode.on("keyup", function() {
for(var s_idx in smileys) {
s = smileys[s_idx];
if(jNode.text().indexOf(s) >= 0) {
jNode.text(jNode.text().replace(s, emoji_map[s]) + " ");
}
placeCaretAtEnd(jNode.get(0));
}
});
}
waitForKeyElements("#main > footer > div.block-compose > div.input-container > div > div.input", text_emoji_main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment