diff --git a/src/main.js b/src/main.js index 8f6dd14..32575c5 100644 --- a/src/main.js +++ b/src/main.js @@ -1,9 +1,52 @@ +function b64decode(data) { + let length = (data.length >> 2) * 3; + if (data.length % 4) + length += (data.length % 4) - 1; + + const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; + + const table = [] + for (let i = 0; i < alphabet.length; i++) + table[alphabet.charCodeAt(i)] = i; + + const result = new Uint8Array(length); + for (let i = 0; i < data.length / 4; i++) { + const value = + (table[data.charCodeAt(4 * i + 0)] << 18) | + (table[data.charCodeAt(4 * i + 1)] << 12) | + (table[data.charCodeAt(4 * i + 2)] << 6) | + (table[data.charCodeAt(4 * i + 3)] << 0); + + result[3 * i + 0] = value >> 16; + result[3 * i + 1] = value >> 8; + result[3 * i + 2] = value >> 0; + } + + if (data.length % 4 == 2) { + const value = + (table[data.charCodeAt(data.length - 2)] << 6) | + (table[data.charCodeAt(data.length - 1)] << 0); + result[result.length - 1] = value >> 4; + } + + if (data.length % 4 == 3) { + const value = + (table[data.charCodeAt(data.length - 3)] << 12) | + (table[data.charCodeAt(data.length - 2)] << 6) | + (table[data.charCodeAt(data.length - 1)] << 0); + result[result.length - 2] = value >> 10; + result[result.length - 1] = value >> 2; + } + + return result; +} + window.addEventListener('load', function() { const base64 = window.location.hash.substring(1) - const zlib = Uint8Array.fromBase64(base64, { alphabet: 'base64url' }); + const zlib = b64decode(base64); - const uncompressed = pako.inflate(new Uint8Array(zlib)); + const uncompressed = pako.inflate(zlib); let decoded = new TextDecoder().decode(uncompressed);