Skip to content

Commit

Permalink
Fix mod download dropdown, courtesy of Modrinth's API
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jan 1, 2024
1 parent 8a7dbbb commit 6d07e3c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 61 deletions.
10 changes: 7 additions & 3 deletions _layouts/default.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@
</script>
<script src="/scripts/datapack-editing.js"></script>
{% endif %}
{% if page.curse_project %}
{% if page.modrinth %}
<!-- Mod downloading dropdown, (almost) direct from our site! -->
<script src="/scripts/mod-download.js"></script>
<script>mountModDownloads('{{ page.curse_project }}', `{% include svg/download-icon.svg %}`);</script>
<script type="module">
import { mountModDownloads } from "/scripts/mod-download.js";
mountModDownloads('{{ page.slug }}', `{% include svg/download-icon.svg %}`);
</script>
{% endif %}
{% if page.curse_project %}
<!-- adding the ability to print the pages out -->
<script src="https://printjs-4de6.kxcdn.com/print.min.js"></script>
{% endif %}
Expand Down
11 changes: 11 additions & 0 deletions _sass/parts/site-header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
104 changes: 46 additions & 58 deletions scripts/mod-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <p>
* Requires the CurseForge description to contain the version list prefixed with
* <code>Versions:</code>
* @param {String} curseID Project ID, probably gotten from the above function
*
* @param {string} slug
* @param {Map<string, {mcVersion: McVersion, modVersions: ModVersion[]}>} versions
* @param down
* @param {string} downloadIcon
* @returns {Promise<void>}
*/
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<br />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);
}

0 comments on commit 6d07e3c

Please sign in to comment.