Skip to content

Instantly share code, notes, and snippets.

@chadlavi
Last active March 24, 2022 22:31
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 chadlavi/828a5b96514634bbcb0bfa7e62a600c9 to your computer and use it in GitHub Desktop.
Save chadlavi/828a5b96514634bbcb0bfa7e62a600c9 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Emoji input replacer
// @version 0.1.1
// @description replace emojis in input with escaped html entities
// @author Chad Lavimoniere
// @include http*://6.*.org/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
/**
* Implemented so that common unicode chars don't get swept up in the mix, like ç, ñ, é, etc
*
* cf. https://jrgraphix.net/r/Unicode/
*/
const UNICODE_CUTOFF = 8399;
// slightly modified from https://dev.to/nikkimk/converting-utf-including-emoji-to-html-x1f92f-4951
const utf2Html = (s) => {
return [...s].map((c) => c.codePointAt() > UNICODE_CUTOFF ? `&#${c.codePointAt()};` : c).join("");
};
const replacer = (e) => {
const newValue = utf2Html(e.target.value);
e.target.value = newValue;
};
document.querySelector("#id_body")?.addEventListener("input", replacer);
document.querySelector("#id_comment-body")?.addEventListener("input", replacer);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment