forked from modmuss50/fabric-versions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
102 lines (87 loc) · 3.91 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
import { getHtml, getJson } from './request.js';
const urlParams = new URLSearchParams(window.location.search);
const pageInfoDiv = document.getElementById('page-info');
const versionSelection = document.getElementById('versions');
/** @type {{version: string, stable: boolean}[]} */
var mcVersions = [];
(async () => {
if (urlParams.has('theme')) {
if (urlParams.get('theme') == 'legacy') {
pageInfoDiv.style.display = 'none';
document.body.setAttribute('data-theme', 'dark');
} else if (urlParams.get('theme') == 'dark') {
document.body.setAttribute('data-theme', 'dark');
}
} else {
let theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
document.body.setAttribute('data-theme', theme);
}
let link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
if (document.body.getAttribute('data-theme') == 'dark') {
link.href = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/styles/github-dark-dimmed.min.css';
} else {
link.href = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/styles/default.min.css';
}
document.head.appendChild(link);
console.log('Loading game version data...');
mcVersions = await getJson('https://meta.legacyfabric.net/v1/versions/game');
await initVersionSelection();
await loadData();
})();
function initVersionSelection() {
for (let version of mcVersions) {
let option = document.createElement('option');
option.text = version.version;
versionSelection.add(option);
}
if (urlParams.has('version')) {
versionSelection.value = urlParams.get('version');
} else {
versionSelection.value = '1.8.9';
}
versionSelection.onchange = () => {
let url = new URL(window.location.href);
url.searchParams.set('version', versionSelection.value);
window.location.href = url;
};
}
async function loadData() {
const mcVersion = versionSelection.value;
console.log('Loading loader version data...');
/** @type {{
* loader: {name: string, separator: string, build: number, maven: string, version: string, stable: boolean},
* mappings: {gameVersion: string, name: string, separator: string, build: number, maven: string, version: string, stable: boolean}
* }[]} */
let data = await getJson(`https://meta.legacyfabric.net/v1/versions/loader/${mcVersion}`);
let latest = data[0];
let codeBlocks = document.getElementsByName('code');
for (let block of codeBlocks) {
block.innerHTML = block.innerHTML.replace('{minecraft_version}', latest.mappings.gameVersion);
block.innerHTML = block.innerHTML.replace('{yarn_version}', latest.mappings.version);
block.innerHTML = block.innerHTML.replace('{loader_version}', latest.loader.version);
}
const versionUrl = 'https://api.modrinth.com/v2/project/legacy-fabric-api/version';
const mavenStr = 'net.legacyfabric.legacy-fabric-api:legacy-fabric-api:';
let apiLatest = undefined;
console.log('Loading api version data...');
try {
let apiData = await getJson(versionUrl);
let apiVersions = apiData.map((el) => el.version_number);
console.log('Found these api versions:', apiVersions);
apiLatest = `${apiVersions[0]}+${mcVersion}`;
} catch (error) {
// fallback if maven request fails
console.error(error);
console.warn('Failed to load latest api version, using hardcoded fallback!');
if (['1.12.2', '1.11.2', '1.10.2', '1.9.4', '1.8.9', '1.8', '1.7.10'].includes(mcVersion)) {
apiLatest = `1.9.1+${mcVersion}`;
}
}
for (let block of codeBlocks) {
block.innerHTML = block.innerHTML.replace('{fabric_version}', apiLatest);
block.innerHTML = block.innerHTML.replace('{fabric_maven}', mavenStr);
}
}