mirror of
https://github.com/zopiya/blog.git
synced 2025-11-03 20:16:47 +08:00
3.1 KiB
3.1 KiB
title, date, summary, description, toc, readTime, autonumber, math, showTags, hidePagination, hideBackToTop
| title | date | summary | description | toc | readTime | autonumber | math | showTags | hidePagination | hideBackToTop |
|---|---|---|---|---|---|---|---|---|---|---|
| Contributors | 2025-01-01 | List of Typo's contributors | List of Typo's contributors | false | false | false | false | false | true | false |
Fetching GitHub data, hang tight!
The theme has accumulated over 300 stars on Github, and
currently counts over 20 contributors:
<script>
async function fetchGitHubData() {
const cacheKey = "githubData";
const cacheExpiryKey = "githubDataExpiry";
const cacheExpiryTime = 3600 * 1000; // 1 hour in milliseconds
// Check if cached data exists and is still valid
const cachedData = localStorage.getItem(cacheKey);
const cachedExpiry = localStorage.getItem(cacheExpiryKey);
const now = new Date().getTime();
if (cachedData && cachedExpiry && now < cachedExpiry) {
const { starCount, contributors } = JSON.parse(cachedData);
updateUI(starCount, contributors);
return;
}
try {
// Fetch star count
const repoResponse = await fetch("https://api.github.com/repos/tomfran/typo");
const repoData = await repoResponse.json();
const starCount = repoData.stargazers_count;
// Fetch contributors
const contributorsResponse = await fetch("https://api.github.com/repos/tomfran/typo/contributors");
const contributors = await contributorsResponse.json();
// Cache data
localStorage.setItem(cacheKey, JSON.stringify({ starCount, contributors }));
localStorage.setItem(cacheExpiryKey, now + cacheExpiryTime);
updateUI(starCount, contributors);
} catch (error) {
console.error("Error fetching GitHub data:", error);
document.getElementById("star-count").textContent = "Failed to fetch star count.";
document.getElementById("contributors-count").textContent = "Failed to fetch contributors count.";
}
}
function updateUI(starCount, contributors) {
document.getElementById("loading-message").style.display = "none";
document.getElementById("content").style.display = "block";
document.getElementById("star-count").textContent = `${starCount}`;
document.getElementById("contributors-count").textContent = `${contributors.length}`;
const contributorsList = document.getElementById("contributors-list");
contributorsList.innerHTML = "";
contributors.forEach(contributor => {
const listItem = document.createElement("li");
listItem.style.marginBottom = ".5rem";
listItem.innerHTML = `