Last active
March 24, 2022 22:31
-
-
Save chadlavi/828a5b96514634bbcb0bfa7e62a600c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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