-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
72 lines (63 loc) · 1.91 KB
/
main.js
File metadata and controls
72 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
document.addEventListener("DOMContentLoaded", () => {
const username = "luau";
const repoNames = [
"UniversalSynSaveInstance",
"Executor-API-Docs",
"Dex",
"SomeHub",
];
const projectsContainer = document.getElementById("projects");
const cacheKey = "githubRepos";
const cacheExpiry = 60 * 60 * 1000; // 1 hour
let cachedData = null;
const now = Date.now();
function displayProjects(repos) {
repos.forEach((data) => {
const project = document.createElement("div");
project.classList.add("project");
project.innerHTML = `
<img src="img/logo.png" alt="${data.name}">
<h3>${data.name}</h3>
<p>${data.description || "No Description"}</p>
<a href="${data.html_url}" target="_blank">View on GitHub</a>
`;
projectsContainer.appendChild(project);
});
}
try {
cachedData = JSON.parse(localStorage.getItem(cacheKey) || "{}");
if (cachedData.timestamp && now - cachedData.timestamp < cacheExpiry) {
console.log("Using cache")
displayProjects(cachedData.repos);
return;
}
} catch (e) {
if (e.name === "SecurityError") {
console.warn("LocalStorage access is blocked. Proceeding without cache.");
} else {
console.error("Unexpected error:", e);
}
}
const promises = repoNames.map((repo) =>
fetch(`https://api.github.com/repos/${username}/${repo}`).then((response) =>
response.json()
)
);
Promise.all(promises)
.then((repos) => {
displayProjects(repos);
try {
localStorage.setItem(
cacheKey,
JSON.stringify({ timestamp: now, repos })
);
} catch (e) {
if (e.name === "SecurityError") {
console.warn(
"Unable to save data to localStorage due to security restrictions."
);
}
}
})
.catch((error) => console.error("Error fetching repository data:", error));
});