ryu-js / lib /snapsave /util.js
randydev's picture
Rename lib/snapsave/utils.js to lib/snapsave/util.js
e9dbfd4 verified
raw
history blame
1.84 kB
export function getEncodedSnapSave(data) {
return data.split('decodeURIComponent(escape(r))}(')[1]
?.split('))')[0]
?.split(',')
?.map(v => v.replace(/"/g, '').trim()) || [];
}
export function getDecodedSnapSave(data) {
return data.split('getElementById("download-section").innerHTML = "')[1]
?.split('"; document.getElementById("inputData").remove(); ')[0]
?.replace(/\\(\\)?/g, '') || '';
}
export function decryptSnapSave(data) {
const args = getEncodedSnapSave(data);
if (args.length === 0) {
throw new Error("Failed to extract encoded SnapSave data.");
}
const decoded = decodeSnapSave(...args);
return getDecodedSnapSave(decoded);
}
export function decodeSnapSave(...args) {
if (args.length < 6) {
throw new Error("Invalid SnapSave decryption arguments.");
}
let [h, u, n, t, e, r] = args;
function decode(d, e, f) {
const g = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split('');
let h = g.slice(0, e);
let i = g.slice(0, f);
let j = d.split('').reverse().reduce((a, b, c) => {
let index = h.indexOf(b);
return index !== -1 ? a + index * Math.pow(e, c) : a;
}, 0);
let k = '';
while (j > 0) {
k = i[j % f] + k;
j = Math.floor(j / f);
}
return k || '0';
}
let r = '';
for (let i = 0, len = h.length; i < len; i++) {
let s = "";
while (h[i] !== n[e]) {
s += h[i];
i++;
}
for (let j = 0; j < n.length; j++) {
s = s.replace(new RegExp(n[j], "g"), j.toString());
}
r += String.fromCharCode(decode(s, e, 10) - t);
}
return decodeURIComponent(encodeURIComponent(r));
}