diff options
Diffstat (limited to 'webAO')
| -rw-r--r-- | webAO/encoding.ts (renamed from webAO/encoding.js) | 33 | ||||
| -rw-r--r-- | webAO/master.ts | 2 |
2 files changed, 7 insertions, 28 deletions
diff --git a/webAO/encoding.js b/webAO/encoding.ts index e6cc3ae..930886f 100644 --- a/webAO/encoding.js +++ b/webAO/encoding.ts @@ -2,7 +2,7 @@ * Escapes a string to AO1 escape codes. * @param {string} estring the string to be escaped */ -export function escapeChat(estring) { +export function escapeChat(estring: string) { return estring .replace(/#/g, '<num>') .replace(/&/g, '<and>') @@ -14,7 +14,7 @@ export function escapeChat(estring) { * Unescapes a string to AO1 escape codes. * @param {string} estring the string to be unescaped */ -export function unescapeChat(estring) { +export function unescapeChat(estring: string) { return estring .replace(/<num>/g, '#') .replace(/<and>/g, '&') @@ -28,7 +28,7 @@ export function unescapeChat(estring) { * XXX: This is unnecessary if we use `createTextNode` instead! * @param {string} unsafe an unsanitized string */ -export function safeTags(unsafe) { +export function safeTags(unsafe: string) { if (unsafe) { return unsafe .replace(/>/g, '>') @@ -41,21 +41,7 @@ export function safeTags(unsafe) { * Encode text on client side. * @param {string} estring the string to be encoded */ -export function encodeChat(estring) { - const selectedEncoding = document.getElementById('client_encoding').value; - if (selectedEncoding === 'unicode') { - // This approach works by escaping all special characters to Unicode escape sequences. - // Source: https://gist.github.com/mathiasbynens/1243213 - return estring.replace(/[^\0-~]/g, (ch) => `\\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); - } +export function encodeChat(estring: string) { return estring; } @@ -63,23 +49,16 @@ export function encodeChat(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') { +export function decodeChat(estring: string) { // 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) { +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 { |
