BCPackages-Interface/assets/js/fetchPackages.js

32 lines
1.5 KiB
JavaScript

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()
console.log('Response:', responseText);
try {
const parsedDocument = new DOMParser().parseFromString(responseText, "text/html");
const linksNodeList = parsedDocument.querySelectorAll("a");
statusText.remove()
linksNodeList.forEach((link) => {
const linkText = link.innerHTML.trim();
const forbiddenLinks = ["Name", "Last modified", "Size", "Description", "Parent Directory"];
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);
}
});
} catch (e) {
console.error('Parsing error:', e);
statusText.innerHTML = "Failed to fetch packages. Please try again later.";
}
}