Skip to content

Commit

Permalink
finished modpreset string parser
Browse files Browse the repository at this point in the history
  • Loading branch information
OshidaBCF committed Jan 12, 2024
1 parent e48868e commit 148636d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 62 deletions.
11 changes: 6 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
<head>
<meta charset="UTF-8" />
<title>Arma 3 Toolkit</title>
<script src="js/arma3.js"></script>
<script src="js/arma3.js" defer></script>
</head>
<body>
<h1>Arma 3 Toolkit</h1>
<p>Modpark string generate for commandline</p>
<label for="armaModpackFile">Select a file:</label>
<input type="file" id="armaModpackFile" accept=".html">
<br><br>
<button type="button", onclick="parseArmaModpackPreset()">Run Parser</button>
<label for="fileNum">Selected files:</label>
<output id="fileNum">0</output>;
<label for="fileSize">Total size:</label>
<output id="fileSize">0</output>
<br><br>
<label for="modString">Modpack String : </label>
<br>
<textarea id="modString" autocorrect="off" style="width: 800px; height: 200px;" readonly></textarea>
</body>
</html>
99 changes: 42 additions & 57 deletions js/arma3.js
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")
});

0 comments on commit 148636d

Please sign in to comment.