-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (43 loc) · 2.46 KB
/
script.js
File metadata and controls
54 lines (43 loc) · 2.46 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
document.getElementById("search-btn").addEventListener("click", async () => {
const username = document.getElementById("user-input").value.trim();
if (!username) return alert("Please enter a valid username");
const proxyUrl = 'https://cors-anywhere.herokuapp.com/';
const targetUrl = 'https://leetcode.com/graphql';
const query = `
query getUserStats($username: String!) {
allQuestionsCount { difficulty count }
matchedUser(username: $username) {
submitStats {
acSubmissionNum { difficulty count }
totalSubmissionNum { difficulty submissions }
}
}
}
`;
const response = await fetch(proxyUrl + targetUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query, variables: { username } })
});
if (!response.ok) return alert("Failed to fetch user data.");
const { data } = await response.json();
const easy = data.matchedUser.submitStats.acSubmissionNum.find(x => x.difficulty === "Easy");
const medium = data.matchedUser.submitStats.acSubmissionNum.find(x => x.difficulty === "Medium");
const hard = data.matchedUser.submitStats.acSubmissionNum.find(x => x.difficulty === "Hard");
const totalEasy = data.allQuestionsCount.find(x => x.difficulty === "Easy").count;
const totalMedium = data.allQuestionsCount.find(x => x.difficulty === "Medium").count;
const totalHard = data.allQuestionsCount.find(x => x.difficulty === "Hard").count;
document.getElementById("easy-label").innerText = `${easy.count}/${totalEasy}`;
document.getElementById("medium-label").innerText = `${medium.count}/${totalMedium}`;
document.getElementById("hard-label").innerText = `${hard.count}/${totalHard}`;
document.getElementById("easy-circle").style.setProperty("--progress", `${(easy.count / totalEasy) * 100}%`);
document.getElementById("medium-circle").style.setProperty("--progress", `${(medium.count / totalMedium) * 100}%`);
document.getElementById("hard-circle").style.setProperty("--progress", `${(hard.count / totalHard) * 100}%`);
const cards = data.matchedUser.submitStats.totalSubmissionNum.map(obj => {
return `<div class="card">
<h4>${obj.difficulty} Submissions</h4>
<p>${obj.submissions}</p>
</div>`;
}).join("");
document.getElementById("stats-cards").innerHTML = cards;
});