From 6d07e3c9067e8c956bf078c3da49ef5eeafce81d Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Mon, 1 Jan 2024 14:56:24 +0100 Subject: [PATCH] Fix mod download dropdown, courtesy of Modrinth's API --- _layouts/default.liquid | 10 +++- _sass/parts/site-header.scss | 11 ++++ scripts/mod-download.js | 104 ++++++++++++++++------------------- 3 files changed, 64 insertions(+), 61 deletions(-) diff --git a/_layouts/default.liquid b/_layouts/default.liquid index b9ce09a2..0fded5ce 100644 --- a/_layouts/default.liquid +++ b/_layouts/default.liquid @@ -95,10 +95,14 @@ {% endif %} -{% if page.curse_project %} +{% if page.modrinth %} - - + +{% endif %} +{% if page.curse_project %} {% endif %} diff --git a/_sass/parts/site-header.scss b/_sass/parts/site-header.scss index 7cd041be..3b3b27c0 100644 --- a/_sass/parts/site-header.scss +++ b/_sass/parts/site-header.scss @@ -199,3 +199,14 @@ vertical-align: middle; cursor: pointer; } + +.download-notice { + @extend .admonition; + @extend .admonition-warning; + + display: inline-block; + max-width: 15em; + line-height: 95%; + font-size: 80%; + margin-bottom: 0.8em; +} \ No newline at end of file diff --git a/scripts/mod-download.js b/scripts/mod-download.js index a00a712a..ee04ecb5 100644 --- a/scripts/mod-download.js +++ b/scripts/mod-download.js @@ -10,71 +10,59 @@ for use. Yes I did make this comment nice to look at for a reason. */ +import {getVersions} from "./modrinth-api.js"; + + /** - * Gets the files for the mod

- * Requires the CurseForge description to contain the version list prefixed with - * Versions: - * @param {String} curseID Project ID, probably gotten from the above function + * + * @param {string} slug + * @param {Map} versions + * @param down + * @param {string} downloadIcon + * @returns {Promise} + */ +async function updateDropdown(slug, versions, down, downloadIcon) { + if (versions !== undefined) { + const li = document.createElement("li"); + const warning = document.createElement("em"); + warning.innerHTML = "WARNING: Not all versions may be supported."; + warning.classList.add('download-notice'); + li.appendChild(warning); + down.appendChild(li); + for (const [version, versionData] of versions) { + let li = document.createElement("li"); + let a = document.createElement("a"); + let span = document.createElement("span"); + + a.href = `https://modrinth.com/mod/${slug}/version/${versionData.modVersions[0].name}`; + span.innerHTML += version; + span.innerHTML += downloadIcon; + span.querySelector("svg").classList.add("download-icon"); + + a.appendChild(span); + li.appendChild(a); + down.appendChild(li); + } + } +} + +/** + * Gets the files for the mod + * + * @param {String} slug Project slug for use in the Modrinth API * @param {String} downloadIcon the HTML code for the download icon */ -async function mountModDownloads(curseID, downloadIcon) { +export async function mountModDownloads(slug, downloadIcon) { const down = document.getElementById("mod-download-dropdown"); - const dataPromise = fetch(`https://curse.nikky.moe/api/addon/${curseID}`).then(it => it.ok && it.json()); - const descPromise = fetch(`https://curse.nikky.moe/api/addon/${curseID}/description`).then(it => it.ok && it.text()); - let data; - let desc; try { - data = await dataPromise; - desc = await descPromise; - if (!data) down.append('Failed to fetch latest downloads'); + const data = await getVersions(slug); + if (data.size === 0) down.append('Failed to fetch latest downloads'); + else { + await updateDropdown(slug, data, down, downloadIcon); + } } catch (e) { + console.error(e); down.append('Failed to fetch latest downloads') } - let flag = false; - - let attempt = setInterval(async function() { - if (desc !== undefined && data !== undefined && !flag) { - let versions; - let flag2; - if(/versions:/i.test(desc)) { - desc = desc.slice(desc.indexOf("ersions:"), desc.indexOf("ersions:") + 250); - desc = desc.replace(/ /g, '-'); - const reg = /[0-9]\.[0-9]+(\.[0-9]*)*(-Snapshot)?/gi; - versions = desc.match(reg); - flag2 = true; - } else { - versions = [...new Set(data.gameVersionLatestFiles.map(f => f.gameVersion))]; - const li = document.createElement("li"); - const a = document.createElement("a"); - a.innerHTML = "WARNING: Not all versions
may be supported."; - li.appendChild(a); - down.appendChild(li); - } - versions.sort().reverse(); - for (const version of versions) { - let li = document.createElement("li"); - let a = document.createElement("a"); - let span = document.createElement("span"); - - a.href = `https://curse.nikky.moe/api/url/${curseID}?version=${version}`; - span.innerHTML += version; - span.innerHTML += downloadIcon; - span.querySelector("svg").classList.add("download-icon"); - a.appendChild(span); - li.appendChild(a); - down.appendChild(li); - } - if (flag2) { - let li = document.createElement("li"); - let a = document.createElement("a"); - a.href = data.websiteUrl; - a.innerHTML = "More Versions..."; - li.appendChild(a); - down.appendChild(li); - } - clearInterval(attempt); - flag = true; - } else if (flag) { clearInterval(attempt); } - }, 50); }