-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
183 lines (164 loc) · 6.8 KB
/
popup.js
File metadata and controls
183 lines (164 loc) · 6.8 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
document.addEventListener("DOMContentLoaded", () => {
// grab elements
const usernameInput = document.getElementById("username");
const saveBtn = document.getElementById("saveBtn");
const clearBtn = document.getElementById("clearBtn");
const themeToggle = document.getElementById("themeToggleIcon");
const status = document.getElementById("status");
const profileCard = document.getElementById("profile");
const profilePic = document.getElementById("profilePic");
const profileName = document.getElementById("profileName");
const profileRank = document.getElementById("profileRank");
const problemsSolved = document.getElementById("problemsSolved");
const unsolvedList = document.getElementById("unsolvedList");
let statusTimer = null;
// small status popup
function showStatus(msg, type = "info") {
if (statusTimer) clearTimeout(statusTimer);
status.textContent = msg;
status.style.color = type === "error" ? "red" : "#10a37f";
status.style.opacity = 1;
statusTimer = setTimeout(
() => {
status.style.opacity = 0;
},
type === "error" ? 5000 : 2000
);
}
// unique key for a problem
function problemKey(p) {
return `${p.contestId ?? "NA"}-${p.index ?? p.name ?? "NA"}`;
}
// load saved data
chrome.storage.sync.get(["cfUsername", "darkMode"], (data) => {
if (data.cfUsername) {
usernameInput.value = data.cfUsername;
clearBtn.style.display = "inline-block";
fetchProfile(data.cfUsername);
}
if (data.darkMode) document.body.classList.add("dark-mode");
});
// toggle theme
themeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
chrome.storage.sync.set({
darkMode: document.body.classList.contains("dark-mode"),
});
});
// press enter to save
usernameInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") saveBtn.click();
});
// save username
saveBtn.addEventListener("click", () => {
const uname = usernameInput.value.trim();
if (!uname) return showStatus("Please enter a username", "error");
chrome.storage.sync.set({ cfUsername: uname }, () => {
showStatus("Username saved");
clearBtn.style.display = "inline-block";
fetchProfile(uname);
});
});
// clear username
clearBtn.addEventListener("click", () => {
usernameInput.value = "";
chrome.storage.sync.remove("cfUsername");
profileCard.classList.add("d-none");
clearBtn.style.display = "none";
});
// fetch profile + submissions
async function fetchProfile(username) {
try {
const [statusRes, userRes] = await Promise.all([
fetch(
`https://codeforces.com/api/user.status?handle=${username}&from=1&count=1000`
).then((r) => r.json()),
fetch(
`https://codeforces.com/api/user.info?handles=${username}`
).then((r) => r.json()),
]);
if (statusRes.status !== "OK" || userRes.status !== "OK")
throw new Error("API error");
// solved + unsolved
const submissions = statusRes.result;
const solved = new Set();
const unsolvedMap = new Map();
submissions.forEach((sub) => {
const key = problemKey(sub.problem);
if (sub.verdict === "OK") solved.add(key);
else if (!solved.has(key) && !unsolvedMap.has(key))
unsolvedMap.set(key, sub.problem);
});
// update profile
const user = userRes.result[0];
profilePic.src = user.titlePhoto;
profileName.textContent = user.handle;
profileName.href = `https://codeforces.com/profile/${user.handle}`;
profileName.style.color = document.body.classList.contains(
"dark-mode"
)
? "#5eead4"
: "#10a37f";
profileRank.textContent = user.rank
? `${user.rank} (${user.rating})`
: "";
problemsSolved.textContent = `Solved: ${solved.size}`;
// badges
const container = document.querySelector(
"#profile .d-flex.gap-2.flex-wrap"
);
container.innerHTML = "";
container.appendChild(problemsSolved);
const maxRatingBadge = document.createElement("span");
maxRatingBadge.className = "badge bg-primary max-rating-badge";
maxRatingBadge.textContent = `Max Rating: ${user.maxRating ?? "-"}`;
container.appendChild(maxRatingBadge);
// unsolved list
unsolvedList.innerHTML = "";
let count = 0;
for (const [k, problem] of unsolvedMap) {
if (count >= 5) break;
const li = document.createElement("li");
li.className = "list-group-item";
li.style.opacity = 0;
const a = document.createElement("a");
a.href = problem.contestId
? `https://codeforces.com/problemset/problem/${problem.contestId}/${problem.index}`
: `https://codeforces.com/problemset?search=${encodeURIComponent(
problem.name
)}`;
a.target = "_blank";
a.rel = "noopener noreferrer";
a.textContent =
problem.name ?? `${problem.contestId}/${problem.index}`;
const badge = document.createElement("span");
badge.className = "ms-2 small";
badge.style.color = document.body.classList.contains(
"dark-mode"
)
? "#e0e0e0"
: "#6b7280";
badge.textContent = problem.rating ?? "-";
li.appendChild(a);
li.appendChild(badge);
unsolvedList.appendChild(li);
setTimeout(() => {
li.style.transition = "opacity 0.8s ease";
li.style.opacity = 1;
}, 50);
count++;
}
// show card
profileCard.classList.remove("d-none");
profileCard.classList.add("profile-animate");
setTimeout(() => {
profileCard.classList.add("show");
}, 50);
status.style.opacity = 0;
} catch (err) {
showStatus("Error: Invalid username or API error", "error");
profileCard.classList.add("d-none");
console.error(err);
}
}
});