Twentysided Unicode Fixer

Allow Twentysided comments to contain unicode

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Twentysided Unicode Fixer
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Allow Twentysided comments to contain unicode
// @author       Retsam
// @match        https://www.shamusyoung.com/twentysidedtale/?p=*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license MIT
/* jshint esversion:6 */
// ==/UserScript==

const escapeUnicodeForChar = (c) => {
    const code = c.charCodeAt(0);
    return code > 126 ? `&#${code};` : c;
}

/** Transforms a string one char at time based on the map function provided */
const mapChars = (str, mapChar) => str.split("").map(mapChar).join("");

const replaceUnicodeOnSubmit = (evt) => {
    // Only mess with the comment form
    if(evt.target.id !== "commentform") return;

    const commentEl = evt.target.querySelector("#comment");
    if(!commentEl) return;

    commentEl.value = mapChars(commentEl.value, escapeUnicodeForChar);
};

(function() {
    'use strict';

    // Listen for all submit events - the comment form moves around, easier to just catch all events and filter for the one that we're interested in.
    document.body.addEventListener("submit", replaceUnicodeOnSubmit);
})();