Twentysided Unicode Fixer

Allow Twentysided comments to contain unicode

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
})();