♻️ refactor: Move similar javascript inline code out of line

- Move variables oid, oid_likes out of line
- Move background-blur out of line
- Move fetch-repo code out of line
This commit is contained in:
Served Smart
2025-06-04 23:51:03 +02:00
parent 8387c6ea0a
commit 37ab1055e7
18 changed files with 100 additions and 155 deletions

30
assets/js/fetch-repo.js Normal file
View File

@@ -0,0 +1,30 @@
function fetchRepo() {
const script = document.currentScript;
const repoURL = script && script.getAttribute("data-url") ? script.getAttribute("data-url")
: (console.error("data-url is null"), null);
const repoId = script && script.getAttribute("data-id") ? script.getAttribute("data-id")
: (console.error("data-id is null"), null);
const requestObjects =
repoId.startsWith("github") ? ["full_name", "description", "stargazers_count", "forks"]
: repoId.startsWith("gitlab") ? ["name_with_namespace", "description", "star_count", "forks_count"]
: ["full_name", "description", "stars_count", "forks_count"];
fetch(repoURL, {
headers: new Headers({
"User-agent": "Mozilla/4.0 Custom User Agent"
})
})
.then((response) => response.json())
.then((data) => {
requestObjects.forEach((requestObject) => {
let element = document.getElementById(`${repoId}-${requestObject}`);
if (requestObject === "stargazers_count" && repoId.startsWith("github")) {
element = document.getElementById(`${repoId}-stargazers`);
}
element ? (element.innerHTML = data[requestObject])
: (console.error(`element.innerHTML for '${repoId}-${requestObject}' is null`), null);
});
})
.catch((error) => console.error(error));
}
fetchRepo();