-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
201 lines (199 loc) · 7.22 KB
/
popup.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Convert unixTime to human readable
function unixToHumanReadableTime(unixTime) {
const milliseconds = unixTime * 1000; // 1575909015000
const dateObject = new Date(milliseconds);
const humanDateFormat = dateObject.toLocaleString(); //2019-12-9 10:30:15
return humanDateFormat;
}
//Converts millisecond to HH:MM:SS
function convertHMS(value) {
const sec = parseInt(value, 10); // convert value to number if it's string
let hours = Math.floor(sec / 3600); // get hours
let minutes = Math.floor((sec - hours * 3600) / 60); // get minutes
let seconds = sec - hours * 3600 - minutes * 60; // get seconds
// add 0 if value < 10; Example: 2 => 02
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
return hours + ":" + minutes + ":" + seconds; // Return is HH : MM : SS
}
document.addEventListener("DOMContentLoaded", async () => {
//About the Coder
const list = document.getElementById("list");
const fullName = document.getElementById("fullName");
const CF_id = document.getElementById("CF-id");
fetch("https://codeforces.com/api/user.info?handles=tourist", {
method: "GET",
})
.then((res) => {
return res.json();
})
.then((users) => {
// console.log(users);
let firstName = users.result[0].firstName;
let lastName = users.result[0].lastName;
const CFHandle = users.result[0].handle;
const rating = users.result[0].rating;
const maxRating = users.result[0].maxRating;
const country = users.result[0].country;
// if name is not given then put handle there
if (firstName == undefined || lastName == undefined) {
firstName = CFHandle;
lastName = "";
}
let lper = 90;
let rper = 10;
const nameStr = `
<tr>
<td style = "width = ${lper}%" ><b>Name<b></td>
<td style = "width = ${rper}%">${
firstName + " " + lastName
} </td>
</tr>
<tr>
<td style = "width = ${lper}%"><b>Handle<b></td>
<td style = "width = ${rper}%"><a target="_blank" href="https://codeforces.com/profile/${CFHandle}">${CFHandle}</a> </td>
</tr>
<tr>
<td style = "width = ${lper}%"><b>Current Rating<b></td>
<td style = "width = ${rper}%">${rating} </td>
</tr>
<tr>
<td style = "width = ${lper}%"><b>Maximum Rating<b></td>
<td style = "width = ${rper}%">${maxRating} </td>
</tr>
<tr>
<td style = "width = ${lper}%"><b>Country<b></td>
<td style = "width = ${rper}%">${country} </td>
</tr> `;
fullName.innerHTML = nameStr;
})
.catch((er) => console.log(er));
/*--------------------------------------------------------------------------------*/
//upcoming contests
fetch("https://codeforces.com/api/contest.list?gym=false")
.then((res) => {
return res.json();
})
.then((contests) => {
console.log(contests);
const contestList = document.getElementById("contestList");
let futureContests = [];
//console.log(contests);
// all future contests has a contest Phase
contests.result.forEach((contest) => {
if (contest.phase === "BEFORE") {
futureContests.push([
contest.durationSeconds,
contest.name,
contest.startTimeSeconds,
contest.relativeTimeSeconds,
]);
}
});
// console.log(futureContests);
// sort function in increasing order of time
futureContests.sort(function (x, y) {
const xtime = unixToHumanReadableTime(x[2]);
const ytime = unixToHumanReadableTime(y[2]);
if (xtime < ytime) {
return 1;
}
if (xtime > ytime) {
return -1;
}
return 0;
});
const newTable = futureContests
.map((contest) => {
const startTimeReadable = unixToHumanReadableTime(contest[2]);
const timeReadable = convertHMS(contest[0]);
let timeLeft = -contest[3];
let days = parseInt(timeLeft / 86400);
let hours = parseInt((timeLeft % 86400) / 3600);
let minutes = parseInt(((timeLeft % 86400) % 3600) / 60);
let time = days + " Days " + hours + " Hours " + minutes + " Minutes";
return `<tr>
<td style = "width = 60%"><a target = "_blank" href = "https://codeforces.com/contests">${contest[1]}</a></td>
<td style = "width = 20%">${time} </td>
<td style = "width = 20%">${timeReadable}</td>
</tr> `;
})
.join("");
let tableCode = `
<tr>
<th>Name</th>
<th>Time Remaining</th>
<th>Duration</th>
</tr> `;
tableCode += newTable;
contestList.innerHTML = tableCode;
})
.catch((er) => console.log(er));
/*--------------------------------------------------------------------------------*/
//Questions suggestions
const questionButton = document.getElementById("questionButton");
const minDifficulty = document.getElementById("minDifficulty").value;
const maxDifficulty = document.getElementById("maxDifficulty").value;
questionButton.addEventListener("click", function () {
const tag = document.getElementById("questionKeyword").value.toLowerCase();
fetch(`https://codeforces.com/api/problemset.problems?tags=${tag}`)
.then((res) => {
return res.json();
})
.then((questions) => {
// console.log(questions);
//rating range minDifficulty and maxDifficulty
let questionList = [];
questions.result.problems.forEach((problem) => {
if (
problem.rating <= maxDifficulty &&
problem.rating >= minDifficulty
) {
questionList.push([
problem.contestId,
problem.index,
problem.name,
problem.rating,
]);
}
});
// Shuffle array and choose first 5
let shuffledQuestions = questionList.sort(() => 0.5 - Math.random());
// console.log(shuffled.length);
shuffledQuestions = shuffledQuestions.slice(
0,
Math.min(5, shuffled.length)
);
//console.log(questionList);
const questionListHTML = document.getElementById("questionList");
//add it to the div
const newQuesTable = shuffledQuestions
.map((question) => {
return `<tr>
<th>${question[2]}</th>
<th><a target="_blank" href="https://codeforces.com/problemset/problem/${question[0]}/${question[1]}">Solve It!!</a> </th>
<th>${question[3]}</th>
</tr> `;
})
.join("");
let tableCode = `
<table class="table table-striped" id="questionsListTable">
<tr>
<th>Name</th>
<th>Link</th>
<th>Rating</th>
</tr> `;
tableCode += newQuesTable;
tableCode += `</table>`;
questionListHTML.innerHTML = tableCode;
})
.catch((er) => console.log(er));
});
});