diff --git a/src/core/script/saveScript.ts b/src/core/script/saveScript.ts index 491faf0..f2887e7 100644 --- a/src/core/script/saveScript.ts +++ b/src/core/script/saveScript.ts @@ -6,6 +6,7 @@ import { getCurrentParagraphName, updateParagraph } from "../paragraphs/paragrap import { getPlayer, setPlayer, showNameDiv } from "../player/playerInfo"; import { loadPronounsRadioBtn, showPronouns } from "../player/pronouns"; import DOMPurify from 'DOMPurify'; +import { b64DecodeUnicode, b64EncodeUnicode } from "../../tools/system"; /** * Create a new save and stringify it. @@ -86,7 +87,7 @@ export function exportStorageSave(saveSlot: string) { Copy and keep the code bellow to load later`); let saveOutput = document.querySelector(`#saveOutput`); (saveOutput).value = ``; //clear old save - (saveOutput).value = `${btoa(retrievedSave!)}`; //encode to Base64 + (saveOutput).value = `${b64EncodeUnicode(retrievedSave!)}`; //encode to Base64 (saveOutput).select(); } @@ -101,7 +102,7 @@ export function exportSave() { Copy and keep the code bellow to load later`); let saveOutput = document.querySelector(`#saveOutput`); (saveOutput).value = ``; //clear old save - (saveOutput).value += `${btoa(save())}`; //encode to Base64 + (saveOutput).value += `${b64EncodeUnicode(save())}`; //encode to Base64 (saveOutput).select(); } @@ -111,7 +112,7 @@ export function exportSave() { */ export function loadSaveCode() { let loadCode = (document.querySelector("#saveOutput")).value; - loadCode = atob(loadCode); + loadCode = b64DecodeUnicode(loadCode); console.log(loadCode); let retrievedSave = JSON.parse(loadCode); console.log(retrievedSave) diff --git a/src/tools/system.ts b/src/tools/system.ts new file mode 100644 index 0000000..2141fb4 --- /dev/null +++ b/src/tools/system.ts @@ -0,0 +1,21 @@ +/** + * Encode unicode string to base64 + * @param str string to be encoded + * @returns encoded string + */ +export function b64EncodeUnicode(str: string) { + return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { + return String.fromCharCode(parseInt(p1, 16)) + })) +} + +/** + * Decode unicode base64 string + * @param str base64 string to be decoded + * @returns decoded string + */ +export function b64DecodeUnicode(str: string) { + return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) { + return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) + }).join('')) +} \ No newline at end of file