-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
265 lines (200 loc) · 9.99 KB
/
script.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
document.addEventListener('DOMContentLoaded', async function () {
function getLocalStorage(key) {
return new Promise((resolve) => {
chrome.storage.local.get(key, resolve);
});
}
function setLocalStorage(data) {
return new Promise((resolve) => {
chrome.storage.local.set(data, resolve);
});
}
async function generateDefaultLeaderboard() {
const friendsData = await getLocalStorage('friends');
const friends = friendsData.friends || [];
const leaderboardContainer = document.getElementById('leaderboard');
leaderboardContainer.innerHTML = '';
friends.forEach((friend, index) => {
const userHtml = `
<div class="oneUser">
<div class="topRow">
<div class="friendsRank">${index + 1}</div>
<div class="userInfo">
<a href=${`https://leetcode.com/${friend}`} target="_blank" class="username">@${friend}</a>
<div class="rank">: <b>-</b></div>
</div>
<div class="deleteUser" data-username="${friend}">
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">
<line x1="0" y1="0" x2="100" y2="100" stroke="white" stroke-width="4"/>
<line x1="100" y1="0" x2="0" y2="100" stroke="white" stroke-width="4"/>
</svg>
</div>
</div>
</div>`;
leaderboardContainer.innerHTML += userHtml;
});
document.querySelectorAll('.deleteUser').forEach(deleteUserElement => {
deleteUserElement.addEventListener('click', async function () {
const usernameToDelete = this.dataset.username;
const data = await getLocalStorage('friends');
const friends = data.friends || [];
const updatedFriends = friends.filter(friend => friend !== usernameToDelete);
await setLocalStorage({ 'friends': updatedFriends });
await generateDefaultLeaderboard();
await updateLeaderboard();
});
});
}
let apiData = null;
async function fetchData() {
if (!apiData) {
try {
const apiURL = await findUrl(); // Wait for the Promise to resolve
console.log("apiURL : ", apiURL);
const response = await fetch(apiURL);
apiData = await response.json();
const weeklySundayElement = document.querySelector('.weeklySunday');
weeklySundayElement.textContent = '';
const biweeklySaturdayEvenElement = document.querySelector('.biweeklySaturdayEven');
biweeklySaturdayEvenElement.textContent = '';
} catch (error) {
console.error('Error fetching data:', error);
}
}
}
async function updateLeaderboard() {
const friendsData = await getLocalStorage('friends');
const friends = friendsData.friends || [];
const leaderboardContainer = document.getElementById('leaderboard');
friends.forEach((friend, index) => {
const matchingUser = apiData.total_ranks_simplified.find(user => user.username.toLowerCase() === friend.toLowerCase());
if (matchingUser) {
const rankElement = leaderboardContainer.querySelector(`.oneUser:nth-child(${index + 1}) .rank b`);
rankElement.textContent = `#${matchingUser.rank + 1}`;
}
});
}
async function findUrl() {
// console.log("HELP");
let contestStatus = "No Contest Running";
let contestURL = null;
function getCurrentIndianTime() {
const now = new Date();
const indianTime = new Date(now.getTime() + (5.5 * 60 * 60 * 1000));
const indianDay = indianTime.getUTCDay();
const indianWeekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const currentIndianDay = indianWeekDays[indianDay];
const hours = indianTime.getUTCHours();
const minutes = indianTime.getUTCMinutes();
const currentTimeString = `${hours < 10 ? '0' + hours : hours}:${minutes < 10 ? '0' + minutes : minutes}`;
return {
day: currentIndianDay,
time: currentTimeString
};
}
function calculateWeeklyContestNumber(startDate) {
const oneDay = 24 * 60 * 60 * 1000;
// Start date: December 24, 2023
const contestStartDate = new Date(startDate);
// Current date
const currentDate = new Date();
// Calculate the number of days between start date and current date
const daysPassed = Math.round(Math.abs((contestStartDate.getTime() - currentDate.getTime()) / (oneDay)));
// Calculate the number of Sundays passed
const sundaysPassed = Math.floor(daysPassed / 7);
// Initial contest number
const initialContestNumber = 377;
// const initialContestNumber = 376;
// Calculate the current contest number
const currentContestNumber = initialContestNumber + sundaysPassed;
return currentContestNumber;
}
function calculateBiweeklyContestNumber(startDate) {
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
// Start date: December 23, 2023
const contestStartDate = new Date(startDate);
// Current date
const currentDate = new Date();
// Calculate the number of days between start date and current date
const daysPassed = Math.round(Math.abs((contestStartDate.getTime() - currentDate.getTime()) / (oneDay)));
// Calculate the number of biweekly intervals passed
const biweeklyIntervalsPassed = Math.floor(daysPassed / 14);
// Initial contest number
const initialContestNumber = 120;
// Calculate the current contest number
const currentContestNumber = initialContestNumber + biweeklyIntervalsPassed;
return currentContestNumber;
}
const currentDayTimeElement = document.querySelector('.currentDayTime');
if (currentDayTimeElement) {
const currentTime = getCurrentIndianTime();
currentDayTimeElement.textContent = `${currentTime.day}, ${currentTime.time}`;
}
const weeklySundayElement = document.querySelector('.weeklySunday');
if (weeklySundayElement) {
const currentTime = getCurrentIndianTime();
const isSunday = currentTime.day === 'Sunday';
const isTime = currentTime.time >= '08:00' && currentTime.time <= '09:35';
if (isSunday && isTime) {
const contestNumber = calculateWeeklyContestNumber('2023-12-24');
contestStatus = `Weekly Contest ${contestNumber} is Running`;
contestURL = `https://lc-live-ranking-api.vercel.app/?contest=weekly-contest-${contestNumber}`;
weeklySundayElement.textContent = `Loading top 5000 ranks . . .`;
} else {
weeklySundayElement.textContent = '';
}
}
const biweeklySaturdayEvenElement = document.querySelector('.biweeklySaturdayEven');
if (biweeklySaturdayEvenElement) {
const currentTime = getCurrentIndianTime();
const isSaturday = currentTime.day === 'Saturday';
const isTime = currentTime.time >= '20:00' && currentTime.time <= '21:35';
if (isSaturday && isTime) {
const contestNumber = calculateBiweeklyContestNumber('2023-12-23');
contestStatus = `Biweekly Contest ${contestNumber} is Running`;
contestURL = `https://lc-live-ranking-api.vercel.app/?contest=biweekly-contest-${contestNumber}`;
biweeklySaturdayEvenElement.textContent = `Loading top 5000 ranks . . .`;
} else {
biweeklySaturdayEvenElement.textContent = '';
}
}
const contestStatusElement = document.querySelector('.currContestStatus');
if (contestStatusElement) {
contestStatusElement.textContent = contestStatus;
}
console.log("Contest URL:", contestURL);
return contestURL;
}
document.addEventListener('DOMContentLoaded', async function () {
await generateDefaultLeaderboard();
document.querySelector('.addBtn').addEventListener('click', async function () {
const friendUsername = document.querySelector('input').value.trim();
if (friendUsername !== '') {
const data = await getLocalStorage('friends');
const friends = data.friends || [];
friends.push(friendUsername);
await setLocalStorage({ 'friends': friends });
await generateDefaultLeaderboard();
updateLeaderboard();
document.querySelector('input').value = '';
}
});
await fetchData();
await updateLeaderboard();
});
await generateDefaultLeaderboard();
document.querySelector('.addBtn').addEventListener('click', async function () {
const friendUsername = document.querySelector('input').value.trim();
if (friendUsername !== '') {
const data = await getLocalStorage('friends');
const friends = data.friends || [];
friends.push(friendUsername);
await setLocalStorage({ 'friends': friends });
await generateDefaultLeaderboard();
updateLeaderboard();
document.querySelector('input').value = '';
}
});
await fetchData();
await updateLeaderboard();
});