Lagniah / FacebookEmojiBreaker

// ==UserScript==
// @name        FacebookEmojiBreaker
// @description Adds an invisible space character when the user types the first character of an emoji, so that facebook cannot recognize it and replace it with its terrible-looking images. Long live the ASCII emojis!
// @namespace   feb
// @include     https://www.facebook.com/*
// @version     1.0.2
// @grant       none
// ==/UserScript==

// The invisible space character that is used to separate emojis' components
var character = '\u2063';

// Characters that the script will recognize as emoji beginning, and thus
// append the separator character to
var triggerCharacters = [58, 59, 60, 109]; // [':', ';', '<', '-']

// Returns caret in element 'el'
function getCaret(el) { 
  if (el.selectionStart) { 
    return el.selectionStart; 
  }
  else if (document.selection) { 
    el.focus(); 

    var r = document.selection.createRange(); 
    if (r === null) {
      return 0; 
    } 

    var re = el.createTextRange(), 
        rc = re.duplicate(); 
    re.moveToBookmark(r.getBookmark()); 
    rc.setEndPoint('EndToStart', re); 

    return rc.text.length; 
  }  
  return 0; 
}

// Adds characters 'content' in the active element at cursor position
function addCharacters(content) {
  var current = document.activeElement;
  var caret = getCaret(current);

  current.value = current.value.substring(0, caret) + content + current.value.substring(caret, current.value.length);
}

function handleKeyPress(e) {
	var keynum;

	// If the key pressed corresponds a trigger character, then the
	// separator character 'character' is inserted.
  if(e.which && triggerCharacters.indexOf(e.which) != -1) {		
		addCharacters(String.fromCharCode(e.which) + character);
		e.preventDefault();
	}
}

// Main content column
document.getElementById("contentArea")
	.addEventListener("keypress", handleKeyPress, false);
	
// Bottom chat dock
document.getElementById("pagelet_dock")
	.addEventListener("keypress", handleKeyPress, false);

// Fullpage chat
document.getElementById("js_26")
	.addEventListener("keypress", handleKeyPress, false);