diff options
| author | sD <stoned@derpymail.org> | 2020-03-25 23:12:27 +0100 |
|---|---|---|
| committer | sD <stoned@derpymail.org> | 2020-03-25 23:12:27 +0100 |
| commit | 2ed9af82f21f1390d507f04638157b63bfd4b949 (patch) | |
| tree | 5cef9436218f376f32ba4c8cb2ef8911a626abc9 /webAO | |
| parent | 419ab5ba4e9de9726aacc4ad4a69a9809be27f65 (diff) | |
move encoding stuff to seperate js file
Diffstat (limited to 'webAO')
| -rw-r--r-- | webAO/client.js | 92 | ||||
| -rw-r--r-- | webAO/encoding.js | 85 | ||||
| -rw-r--r-- | webAO/master.js | 16 |
3 files changed, 90 insertions, 103 deletions
diff --git a/webAO/client.js b/webAO/client.js index 6fda3fc..2ddfb72 100644 --- a/webAO/client.js +++ b/webAO/client.js @@ -6,6 +6,8 @@ import Fingerprint2 from 'fingerprintjs2'; +import { unescapeChat, escapeChat, encodeChat, decodeChat, safe_tags } from './encoding.js'; + // Load some defaults for the background and evidence dropdowns import character_arr from "./characters.js"; import background_arr from "./backgrounds.js"; @@ -13,8 +15,6 @@ import evidence_arr from "./evidence.js"; import { EventEmitter } from "events"; -const version = 2.4; - let client; let viewport; @@ -2617,90 +2617,4 @@ export function toggleShout(shout) { selectedShout = shout; } } -window.toggleShout = toggleShout; - -/** - * Escapes a string to be HTML-safe. - * - * XXX: This is unnecessary if we use `createTextNode` instead! - * @param {string} unsafe an unsanitized string - */ -function safe_tags(unsafe) { - if (unsafe) { - return unsafe - .replace(/&/g, "&") - .replace(/</g, "<"); - //.replace(/"/g, """) - //.replace(/'/g, "'"); - } else { - return ""; - } -} - -/** - * Escapes a string to AO1 escape codes. - * @param {string} estring the string to be escaped - */ -function escapeChat(estring) { - return estring - .replace(/#/g, "<num>") - .replace(/&/g, "<and>") - .replace(/%/g, "<percent>") - .replace(/\$/g, "<dollar>"); -} - -/** - * Unescapes a string to AO1 escape codes. - * @param {string} estring the string to be unescaped - */ -function unescapeChat(estring) { - return estring - .replace(/<num>/g, "#") - .replace(/<and>/g, "&") - .replace(/<percent>/g, "%") - .replace(/<dollar>/g, "$"); -} - -/** - * Encode text on client side. - * @param {string} estring the string to be encoded - */ -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, function (ch) { - return "\\u" + ("000" + ch.charCodeAt().toString(16)).slice(-4); - }); - } else 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); - } else { - return estring; - } -} - -/** - * Decodes text on client side. - * @param {string} estring the string to be decoded - */ -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, function (match, group) { - return String.fromCharCode(parseInt(group, 16)); - }); - } else 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(","))); - } else { - return estring; - } -}
\ No newline at end of file +window.toggleShout = toggleShout;
\ No newline at end of file diff --git a/webAO/encoding.js b/webAO/encoding.js new file mode 100644 index 0000000..76e0b6d --- /dev/null +++ b/webAO/encoding.js @@ -0,0 +1,85 @@ +/** + * Escapes a string to AO1 escape codes. + * @param {string} estring the string to be escaped + */ +export function escapeChat(estring) { + return estring + .replace(/#/g, "<num>") + .replace(/&/g, "<and>") + .replace(/%/g, "<percent>") + .replace(/\$/g, "<dollar>"); +} + +/** + * Unescapes a string to AO1 escape codes. + * @param {string} estring the string to be unescaped + */ +export function unescapeChat(estring) { + return estring + .replace(/<num>/g, "#") + .replace(/<and>/g, "&") + .replace(/<percent>/g, "%") + .replace(/<dollar>/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 safe_tags(unsafe) { + if (unsafe) { + return unsafe + .replace(/&/g, "&") + .replace(/</g, "<"); + //.replace(/"/g, """) + //.replace(/'/g, "'"); + } else { + return ""; + } +} + +/** + * 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, function (ch) { + return "\\u" + ("000" + ch.charCodeAt().toString(16)).slice(-4); + }); + } else 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); + } else { + 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, function (match, group) { + return String.fromCharCode(parseInt(group, 16)); + }); + } else 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(","))); + } else { + return estring; + } +}
\ No newline at end of file diff --git a/webAO/master.js b/webAO/master.js index 7ceb748..660b6e2 100644 --- a/webAO/master.js +++ b/webAO/master.js @@ -1,7 +1,7 @@ const MASTERSERVER_IP = "master.aceattorneyonline.com:27014"; -const version = 2.4; import Fingerprint2 from 'fingerprintjs2'; +import { unescapeChat } from './encoding.js'; let masterserver; @@ -14,18 +14,6 @@ const server_description = []; server_description[-1] = "This is your computer on port 50001"; const online_counter = []; -/** - * Unescapes a string to AO1 escape codes. - * @param {string} estring the string to be unescaped - */ -function unescapeChat(estring) { - return estring - .replace(/<num>/g, "#") - .replace(/<and>/g, "&") - .replace(/<percent>/g, "%") - .replace(/<dollar>/g, "$"); -} - if (window.requestIdleCallback) { requestIdleCallback(function () { Fingerprint2.get(options, function (components) { @@ -170,7 +158,7 @@ function onMessage(e) { } else if (header === "servercheok") { const args = msg.split("#").slice(1); - document.getElementById("clientinfo").innerHTML = `Client version: ${args[0]}`; + document.getElementById("clientinfo").innerHTML = `Client version: ${version} expected: ${args[0]}`; } else if (header === "SV") { const args = msg.split("#").slice(1); |
