-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,52 @@ | ||
|
||
function parseArmaModpackPreset() | ||
{ | ||
selectedFile = uploadInput.files[0]; | ||
if (selectedFile) { | ||
var reader = new FileReader(); | ||
reader.readAsText(selectedFile, "UTF-8"); | ||
reader.onload = function (evt) { | ||
fileContent = evt.target.result | ||
parser = new DOMParser(); | ||
parsed = parser.parseFromString(fileContent, "text/html"); | ||
modlist = parsed.getElementsByClassName("mod-list")[0].querySelector("table > tbody") | ||
|
||
output = "" | ||
regex = new RegExp("[^a-zA-Z0-9]", "g"); | ||
for (var i = 0, row; row = modlist.rows[i]; i++) { | ||
modName = row.querySelector('[data-type="DisplayName"]').innerHTML | ||
|
||
modName = modName.replaceAll(regex, "") | ||
output += "@" + modName + ";" | ||
} | ||
output = output.slice(0, -1) | ||
document.getElementById("modString").textContent = output | ||
} | ||
reader.onerror = function (evt) { | ||
console.log("error reading file"); | ||
} | ||
} | ||
} | ||
|
||
function waitForElm(selector) { | ||
return new Promise(resolve => { | ||
if (document.querySelector(selector)) { | ||
return resolve(document.querySelector(selector)); | ||
} | ||
return new Promise(resolve => { | ||
if (document.querySelector(selector)) { | ||
return resolve(document.querySelector(selector)); | ||
} | ||
|
||
const observer = new MutationObserver(mutations => { | ||
if (document.querySelector(selector)) { | ||
observer.disconnect(); | ||
resolve(document.querySelector(selector)); | ||
} | ||
}); | ||
const observer = new MutationObserver(mutations => { | ||
if (document.querySelector(selector)) { | ||
observer.disconnect(); | ||
resolve(document.querySelector(selector)); | ||
} | ||
}); | ||
|
||
observer.observe(document.body, { | ||
childList: true, | ||
subtree: true | ||
}); | ||
}); | ||
observer.observe(document.body, { | ||
childList: true, | ||
subtree: true | ||
}); | ||
}); | ||
} | ||
|
||
waitForElm("#body > button").then((elm) => { | ||
uploadInput = document.getElementById("armaModpackFile") | ||
uploadInput.addEventListener( | ||
"change", | ||
() => { | ||
// Calculate total size | ||
let numberOfBytes = 0; | ||
for (const file of uploadInput.files) { | ||
numberOfBytes += file.size; | ||
} | ||
|
||
// Approximate to the closest prefixed unit | ||
const units = [ | ||
"B", | ||
"KiB", | ||
"MiB", | ||
"GiB", | ||
"TiB", | ||
"PiB", | ||
"EiB", | ||
"ZiB", | ||
"YiB", | ||
]; | ||
const exponent = Math.min( | ||
Math.floor(Math.log(numberOfBytes) / Math.log(1024)), | ||
units.length - 1, | ||
); | ||
const approx = numberOfBytes / 1024 ** exponent; | ||
const output = | ||
exponent === 0 | ||
? `${numberOfBytes} bytes` | ||
: `${approx.toFixed(3)} ${ | ||
units[exponent] | ||
} (${numberOfBytes} bytes)`; | ||
|
||
document.getElementById("fileNum").textContent = uploadInput.files.length; | ||
document.getElementById("fileSize").textContent = output; | ||
}, | ||
false, | ||
); | ||
}); | ||
waitForElm("body > button").then((elm) => { | ||
uploadInput = document.getElementById("armaModpackFile") | ||
}); |