-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
82 lines (71 loc) · 3.07 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function fetchVersions(minecraft) {
const request = new XMLHttpRequest();
request.onload = () => applyVersions(request, minecraft);
request.open("GET", "https://api.modrinth.com/v2/project/architectury-api/version");
request.setRequestHeader("User-Agent", "Juuxel/architectury-api-versions");
request.send();
// Clear children of version holder
removeByClassName("version-box");
// Create spinner
const spinner = document.createElement("div");
spinner.classList.add("spinner");
document.getElementById("spinner-holder").append(spinner);
// Disable buttons
const buttons = document.getElementsByClassName("version-button");
for (let i = 0; i < buttons.length; i++) {
buttons.item(i).disabled = true;
}
}
function applyVersions(request, minecraft) {
if (request.status != 200) {
console.log("Could not retrieve versions!", request.status, request.response);
return;
}
const response = JSON.parse(request.response);
const sorted = response.filter(version => version.game_versions.includes(minecraft))
.sort((a, b) => new Date(b).getTime() - new Date(a).getTime());
removeByClassName("spinner");
addVersionBox("CurseForge", "All versions for " + minecraft, "curseforge", "https://www.curseforge.com/minecraft/mc-mods/architectury-api/files?version=" + minecraft);
addLoader(sorted, "Fabric", "fabric");
addLoader(sorted, "Forge", "forge");
if (addLoader(sorted, "Quilt", "quilt")) {
document.getElementById("version-holder").classList.add("threecolumns");
} else {
document.getElementById("version-holder").classList.remove("threecolumns");
}
// Re-enable buttons
const buttons = document.getElementsByClassName("version-button");
for (let i = 0; i < buttons.length; i++) {
buttons.item(i).disabled = false;
}
}
function removeByClassName(className) {
const elements = document.getElementsByClassName(className);
const length = elements.length;
for (let i = 1; i <= length; i++) {
const element = elements.item(length - i);
element.remove();
}
}
function addLoader(versions, name, id) {
const filtered = versions.filter(version => version.loaders.includes(id));
if (filtered.length == 0) return false;
const version = filtered[0];
addVersionBox(name, version.name, "modrinth", "https://modrinth.com/mod/architectury-api/version/" + version.id);
return true;
}
function addVersionBox(title, versionName, id, link) {
const versionBox = document.createElement("div");
versionBox.classList.add("version-box");
versionBox.classList.add(id);
const loaderName = document.createElement("span");
loaderName.classList.add("card-header");
loaderName.textContent = title;
versionBox.append(loaderName);
const versionElement = document.createElement("a");
versionElement.classList.add("version");
versionElement.textContent = versionName;
versionElement.href = link;
versionBox.append(versionElement);
document.getElementById("version-holder").append(versionBox);
}