-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
171 lines (130 loc) · 5.8 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
const solveButton = document.getElementById("solve-button")
const problemStatement = document.querySelector(".problem-statement");
const filterInput = document.getElementById("filter-rating");
const contestId = document.getElementById("contestID");
const problemID = document.getElementById("problemID");
const problemRating = document.getElementById("problem-rating");
console.log("Trying to get a new Problem...");
// global variable to store the problem URL.
var problemURL;
/**
* If the current date is different than the date stored in localStorage, then update localStorage with
* the current date and return true. Otherwise, return false
* @returns a boolean value.
*/
function hasOneDayPassed() {
var date = new Date().toLocaleDateString();
if (localStorage.previousDate == date)
return false;
localStorage.previousDate = date;
return true;
}
/**
* It fetches the data from the codeforces api and returns the problems.
* @returns An array of objects.
*/
async function fetchCodeforcesProblemData() {
// using codeforces api to fetch problems
const response = await fetch('https://codeforces.com/api/problemset.problems');
const data = await response.json();
return data.result.problems;
}
/**
* It takes a list of problems and returns a random problem from the list
* @param problemList - an array of objects, each object contains the following properties:
* @returns The selected problem is being returned.
*/
function getOneProblem(problemList) {
const index = Math.floor(Math.random() * problemList.length);
const selectedProblem = problemList[index];
if (selectedProblem !== {} && selectedProblem !== undefined) {
// using localStorage to store the selected problem
localStorage.selectedProblem = JSON.stringify(selectedProblem);
// codeforces link is created and stored
problemURL = `https://codeforces.com/problemset/problem/${selectedProblem.contestId}/${selectedProblem.index}`;
}
return selectedProblem;
}
/**
* It updates the HTML elements on the page with the data from the selected problem.
* @param selectedProblem - The problem object that is selected from the list of problems.
*/
function updateHTML(selectedProblem) {
if (selectedProblem !== {} && selectedProblem !== undefined) {
// Updating the problem statement and problem details on the page
problemStatement.innerText = selectedProblem.name;
contestId.innerText = selectedProblem.contestId;
problemID.innerText = selectedProblem.index;
problemRating.innerText = selectedProblem.rating;
problemURL = `https://codeforces.com/problemset/problem/${selectedProblem.contestId}/${selectedProblem.index}`;
}
}
async function updateProblemByRating() {
// fetching input from filter
const filterRating = Number(filterInput.value);
// obtaining problem for the day from the list of problems
const problemList = await fetchCodeforcesProblemData();
// filtering problems based on rating
const filterProblemsList = problemList.filter(item => item.rating === filterRating);
// selecting a problem the list
const selectedProblem = getOneProblem(filterProblemsList);
updateHTML(selectedProblem);
// clearing filter input
filterInput.value = null;
console.log(selectedProblem);
}
// Updates the visitor counter
async function updateVisitorCounter() {
try {
const isExistingVisitor = !!localStorage.getItem("isExistingVisitor");
if (isExistingVisitor) {
const visitsResponse = await fetch("https://api.countapi.xyz/get/codeforces-probem-of-the-day.netlify.app/992ab16e-4a82-4d30-a834-a62af433f862");
const visits = await visitsResponse.json();
document.querySelector('#visitors > span.number').innerText = visits.value;
} else {
const visitsResponse = await fetch("https://api.countapi.xyz/hit/codeforces-probem-of-the-day.netlify.app/992ab16e-4a82-4d30-a834-a62af433f862");
const visits = await visitsResponse.json();
document.querySelector('#visitors > span.number').innerText = visits.value;
localStorage.setItem("isExistingVisitor", uuid())
}
} catch (error) {
console.error("Something went wrong. Trying to fetch last known number of visits again...");
const visitsResponse = await fetch("https://api.countapi.xyz/get/codeforces-probem-of-the-day.netlify.app/992ab16e-4a82-4d30-a834-a62af433f862");
const visits = await visitsResponse.json();
document.querySelector('#visitors > span.number').innerHTML = visits.value;
}
}
// Snippet from https://stackoverflow.com/a/2117523/10165585
// Returns a UUID string
function uuid() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
async function updateProblem() {
// checking if old problem data is existing, if exists display it.
if (localStorage.selectedProblem) {
// parsing object data from localstorage
const selectedProblem = localStorage.selectedProblem == undefined ? {} : JSON.parse(localStorage.selectedProblem);
updateHTML(selectedProblem);
}
// checking one day condition, if this check passes the new problem is fetched.
if (!hasOneDayPassed()) return false;
// obtaining problem for the day from the list of problems
const problemList = await fetchCodeforcesProblemData();
// selecting a problem the list
const selectedProblem = getOneProblem(problemList);
updateHTML(selectedProblem);
// console the problem details on every new problem fetch
console.log(selectedProblem);
}
// on every page load, it tries to update the problem
window.addEventListener('DOMContentLoaded', updateProblem);
// On every page load, it tries to update the visitor counter
window.addEventListener('DOMContentLoaded', updateVisitorCounter);
// keep checking every minute, and trying to load a new problem
setInterval(updateProblem, 60000);
function enterIntoTheProblem() {
window.open(problemURL, "_blank");
}
solveButton.addEventListener("click", enterIntoTheProblem);