From e93fa6110bfe510216b558e1b256fac4c267b1a2 Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Wed, 23 Mar 2022 21:46:28 +0100 Subject: sort of convert encoding to typescript --- webAO/encoding.js | 85 ------------------------------------------------------- webAO/encoding.ts | 64 +++++++++++++++++++++++++++++++++++++++++ webAO/master.ts | 2 +- 3 files changed, 65 insertions(+), 86 deletions(-) delete mode 100644 webAO/encoding.js create mode 100644 webAO/encoding.ts (limited to 'webAO') diff --git a/webAO/encoding.js b/webAO/encoding.js deleted file mode 100644 index e6cc3ae..0000000 --- a/webAO/encoding.js +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Escapes a string to AO1 escape codes. - * @param {string} estring the string to be escaped - */ -export function escapeChat(estring) { - return estring - .replace(/#/g, '') - .replace(/&/g, '') - .replace(/%/g, '') - .replace(/\$/g, ''); -} - -/** - * Unescapes a string to AO1 escape codes. - * @param {string} estring the string to be unescaped - */ -export function unescapeChat(estring) { - return estring - .replace(//g, '#') - .replace(//g, '&') - .replace(//g, '%') - .replace(//g, '$'); -} - -/** - * Escapes a string to be HTML-safe. - * - * XXX: This is unnecessary if we use `createTextNode` instead! - * @param {string} unsafe an unsanitized string - */ -export function safeTags(unsafe) { - if (unsafe) { - return unsafe - .replace(/>/g, '>') - .replace(/ `\\u${(`000${ch.charCodeAt().toString(16)}`).slice(-4)}`); - } if (selectedEncoding === 'utf16') { - // Source: https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String - const buffer = new ArrayBuffer(estring.length * 2); - const result = new Uint16Array(buffer); - for (let i = 0, strLen = estring.length; i < strLen; i++) { - result[i] = estring.charCodeAt(i); - } - return String(result); - } - return estring; -} - -/** - * Decodes text on client side. - * @param {string} estring the string to be decoded - */ -export function decodeChat(estring) { - const selectedDecoding = document.getElementById('client_decoding').value; - if (selectedDecoding === 'unicode') { - // Source: https://stackoverflow.com/questions/7885096/how-do-i-decode-a-string-with-escaped-unicode - return estring.replace(/\\u([\d\w]{1,})/gi, (match, group) => String.fromCharCode(parseInt(group, 16))); - } if (selectedDecoding === 'utf16') { - // Source: https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String - return String.fromCharCode.apply(null, new Uint16Array(estring.split(','))); - } - return estring; -} - -/** - * XXX: a nasty hack made by gameboyprinter. - * @param {string} msg chat message to prepare for display - */ -export function prepChat(msg) { - // TODO: make this less awful - return unescapeChat(decodeChat(msg)); -} diff --git a/webAO/encoding.ts b/webAO/encoding.ts new file mode 100644 index 0000000..930886f --- /dev/null +++ b/webAO/encoding.ts @@ -0,0 +1,64 @@ +/** + * Escapes a string to AO1 escape codes. + * @param {string} estring the string to be escaped + */ +export function escapeChat(estring: string) { + return estring + .replace(/#/g, '') + .replace(/&/g, '') + .replace(/%/g, '') + .replace(/\$/g, ''); +} + +/** + * Unescapes a string to AO1 escape codes. + * @param {string} estring the string to be unescaped + */ +export function unescapeChat(estring: string) { + return estring + .replace(//g, '#') + .replace(//g, '&') + .replace(//g, '%') + .replace(//g, '$'); +} + +/** + * Escapes a string to be HTML-safe. + * + * XXX: This is unnecessary if we use `createTextNode` instead! + * @param {string} unsafe an unsanitized string + */ +export function safeTags(unsafe: string) { + if (unsafe) { + return unsafe + .replace(/>/g, '>') + .replace(/ String.fromCharCode(parseInt(group, 16))); +} + +/** + * XXX: a nasty hack made by gameboyprinter. + * @param {string} msg chat message to prepare for display + */ +export function prepChat(msg: string) { + // TODO: make this less awful + return unescapeChat(decodeChat(msg)); +} diff --git a/webAO/master.ts b/webAO/master.ts index 8fd8779..8c850c2 100644 --- a/webAO/master.ts +++ b/webAO/master.ts @@ -1,6 +1,6 @@ import FingerprintJS from '@fingerprintjs/fingerprintjs'; -import { unescapeChat, safeTags } from './encoding.js'; +import { unescapeChat, safeTags } from './encoding'; declare global { interface Window { -- cgit