Files
AndorsTrailNameChanger/index.html
2023-01-28 21:47:30 +01:00

74 lines
2.1 KiB
HTML

<!DOCTYPE html>
<html>
<body>
<input id="new_name_input" type="text" />
<input id="savegame_input" type="file" />
<button id="do_it" >change name</button>
<script>
const START_OF_NAME_LEN_INDEX = 5;
const START_OF_NAME_INDEX = START_OF_NAME_LEN_INDEX + 1;
function change_name(int_array, targetName) {
var data = Array.from(int_array)
//remove old name bytes
console.log(data);
let old_name_len = data[START_OF_NAME_LEN_INDEX];
data.splice(START_OF_NAME_INDEX, old_name_len);
console.log(data);
//enter new name
data[START_OF_NAME_LEN_INDEX] = targetName.length;
for (let i = 0; i<targetName.length; i++){
data.splice(START_OF_NAME_INDEX + i, 0, targetName[i]);
}
console.log(data);
return data;
}
var download = (function () {
var dwnld = document.createElement("a");
document.body.appendChild(dwnld);
dwnld.style = "display: none";
return function (data, name) {
var blob = new Blob(data, {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
console.log(blob);
console.log(typeof blob);
console.log(url);
dwnld.href = url;
dwnld.download = name;
dwnld.click();
window.URL.revokeObjectURL(url);
};
}());
document.getElementById('do_it').addEventListener('click', function() {
let new_name = document.getElementById('new_name_input').value,
reader = new FileReader(),
file_input = document.getElementById('savegame_input'),
filename = file_input.files[0].name;
reader.onload = function() {
let arrayBuffer = this.result,
int_array = new Uint8Array(arrayBuffer);
let name_bytes = new TextEncoder().encode(new_name);
let result = Uint8Array.from(change_name(int_array, name_bytes));
console.log(result);
console.log(typeof result);
console.log("result str:\n" + result)
download([result], filename);
console.log('downloaded')
}
reader.readAsArrayBuffer(file_input.files[0]);
}, false);
</script>
</body>
</html>