BCPackages-Interface/assets/js/fetchPackages.js

45 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

async function fetchPackages(uri) {
let statusText = document.getElementById("statusText");
statusText.innerHTML = "Fetching packages, please wait...";
let response = await fetch(uri, {
method: "GET"
})
let responseText = await response.text()
if (response.status === 200) {
console.log('Response:', responseText);
try {
const parsedDocument = new DOMParser().parseFromString(responseText, "text/html");
const linksNodeList = parsedDocument.querySelectorAll("a");
linksNodeList.forEach((link) => {
const linkText = link.innerHTML.trim();
const forbiddenLinks = ["Name", "Last modified", "Size", "Description", "Parent Directory", "repodata/"];
if (!forbiddenLinks.includes(linkText) && !linkText.endsWith(".html")) {
const packageLink = document.createElement("a");
const newRedirect = uri + "/" + link.href.split("/").pop();
packageLink.href = newRedirect;
packageLink.innerHTML = linkText;
packageLink.className = "package";
packageLink.target = "_blank";
document.getElementById("packages").appendChild(packageLink);
console.log('Original URL:', link.href);
console.log('Modified URL:', newRedirect);
}
});
if (document.getElementById("packages").childElementCount === 0) {
statusText.innerHTML = "Nothing here yet!";
} else {
statusText.remove()
}
} catch (e) {
console.error('Parsing error:', e);
statusText.innerHTML = "Failed to fetch packages. Please try again later.";
}
} else if (response.status === 404) {
console.error('Response:', responseText);
statusText.innerHTML = "Nothing here yet!";
} else {
console.error('Response:', responseText);
statusText.innerHTML = "Failed to fetch packages. Please try again later.";
}
}