forked from YadavAkhileshh/Alien-Invasion-Defense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contributor.js
95 lines (84 loc) · 3.38 KB
/
contributor.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const repoOwner = "YadavAkhileshh";
const repoName = "Alien-Invasion-Defense";
const contributorsUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/contributors`;
const repoUrl = `https://api.github.com/repos/${repoOwner}/${repoName}`;
async function fetchContributorData() {
try {
// Start with the first page of contributors
let contributors = [];
let url = contributorsUrl;
while (url) {
const contributorsRes = await fetch(url);
const contributorsPage = await contributorsRes.json();
// Add the current page of contributors to the total list
contributors = contributors.concat(contributorsPage);
// Check the Link header for pagination information
const linkHeader = contributorsRes.headers.get("Link");
if (linkHeader) {
const nextPageUrl = parseLinkHeader(linkHeader);
url = nextPageUrl ? nextPageUrl : null; // Get the next page URL or stop if no next page
} else {
url = null; // No Link header means we're on the last page
}
}
const repoRes = await fetch(repoUrl);
const repoData = await repoRes.json();
// Update the stats section
const statsGrid = document.getElementById("statsGrid");
statsGrid.innerHTML = `
<div class="contributor-stat-card"><h3>${contributors.length}</h3><p>Contributors</p></div>
<div class="contributor-stat-card"><h3>${contributors.reduce((sum, { contributions }) => sum + contributions, 0)}</h3><p>Total Contributions</p></div>
<div class="contributor-stat-card"><h3>${repoData.stargazers_count}</h3><p>GitHub Stars</p></div>
<div class="contributor-stat-card"><h3>${repoData.forks_count}</h3><p>Forks</p></div>
`;
// Update the contributors section
const contributorsContainer = document.getElementById("contributors");
contributorsContainer.innerHTML = contributors.map(({ login, contributions, avatar_url, html_url }) => `
<div class="contributor-card">
<img src="${avatar_url}" alt="${login}'s avatar">
<p><strong>${login}</strong></p>
<p>Contributions: ${contributions}</p>
<a href="${html_url}" target="_blank">GitHub Profile</a>
</div>
`).join('');
} catch (error) {
console.error("Error fetching data:", error);
}
}
// Function to parse the Link header and get the URL for the next page
function parseLinkHeader(linkHeader) {
const links = linkHeader.split(',').reduce((acc, part) => {
const [url, rel] = part.split(';');
const match = rel.match(/rel="(\w+)"/);
if (match) {
acc[match[1]] = url.trim().slice(1, -1); // Remove surrounding <> from URL
}
return acc;
}, {});
return links.next; // Return the URL of the next page, or undefined if there's no next
}
window.onscroll = function () {
const scrollUpBtn = document.getElementById("scrollUpBtn-cn");
if (
document.body.scrollTop > 100 ||
document.documentElement.scrollTop > 100
) {
scrollUpBtn.style.display = "block";
} else {
scrollUpBtn.style.display = "none";
}
const totalHeight =
document.documentElement.scrollHeight - window.innerHeight;
const scrollPosition = window.pageYOffset;
const scrollPercentage = (scrollPosition / totalHeight) * 100;
document.getElementById(
"progress-bar-cn"
).style.width = `${scrollPercentage}%`;
};
function scrollToTop() {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
fetchContributorData();