-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
150 lines (130 loc) · 5.72 KB
/
contentScript.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
(async ()=>{
const randomPickButtonExists = document.getElementsByClassName("random-pick-button")[0];
if(!randomPickButtonExists){
// Default UL list
const menuList = document.getElementsByClassName("menu-list main-menu-list")[0];
// Main container
const featureContainer = document.createElement("li");
featureContainer.className = "feature-container";
// Feature Button
const randomPickButton = document.createElement("img");
randomPickButton.src = chrome.runtime.getURL("assets/shuffle_button.png");
randomPickButton.className = "random-pick-button";
// Feature Menu
const featureMenu = document.createElement("form");
featureMenu.className = "feature-menu " + "invis";;
const featureTitle = document.createElement("h4");
featureTitle.textContent = "Pick a random problem";
// Features List
const featureListSelectorContainer = document.createElement("div");
// Rating
const featureListRating = document.createElement("div");
featureListRating.className = "feature "
const ratingLabel = document.createElement("div");
ratingLabel.textContent = "Rating range:";
const ratingRangeBox = document.createElement("div");
const ratingFrom = document.createElement("input");
ratingFrom.type = "number";
const ratingTo = document.createElement("input");
ratingTo.type = "number";
ratingFrom.className = "rating-input";
ratingTo.className = "rating-input";
ratingFrom.placeholder = "from";
ratingTo.placeholder = "to";
ratingRangeBox.appendChild(ratingFrom);
ratingRangeBox.append(" - ");
ratingRangeBox.appendChild(ratingTo);
featureListRating.appendChild(ratingLabel);
featureListRating.appendChild(ratingRangeBox);
featureListSelectorContainer.appendChild(featureListRating);
// Feature Controls
const featureControlButtonsContainer = document.createElement("div");
featureControlButtonsContainer.className = "control-button-container";
// Buttons
const goButton = document.createElement("button");
goButton.type = "submit";
goButton.textContent = "Go";
const resetButton = document.createElement("button");
resetButton.textContent = "Reset";
featureControlButtonsContainer.appendChild(resetButton);
featureControlButtonsContainer.appendChild(goButton);
// Appending things together
featureMenu.appendChild(featureTitle);
featureMenu.appendChild(featureListSelectorContainer);
featureMenu.appendChild(featureControlButtonsContainer);
featureContainer.appendChild(randomPickButton);
featureContainer.appendChild(featureMenu);
menuList.appendChild(featureContainer);
// ErrorBox
const errorBox = document.createElement("div");
errorBox.style.color = "red";
errorBox.style.marginTop = "5px";
errorBox.textContent = "No problems found for selected filters!"
// Loading Overlay
const loadingOverlay = document.createElement("div");
loadingOverlay.className = "loading-overlay";
const loadingIcon = document.createElement("img");
loadingIcon.style.width = "40px"
loadingIcon.src = chrome.runtime.getURL("assets/loading_icon.gif");
loadingOverlay.appendChild(loadingIcon);
// Binding listeners
// Main button
randomPickButton.addEventListener("click", () => {
featureMenu.classList.toggle("invis");
if(featureMenu.classList.contains("invis")){
document.activeElement.blur();
}else{
ratingFrom.focus();
}
});
// Reset Button
resetButton.addEventListener("click", () => {
if(featureMenu.contains(errorBox)){
featureMenu.removeChild(errorBox);
}
ratingFrom.value = "";
ratingTo.value = "";
});
// Go button
const pickRandom = async() => {
if(featureMenu.contains(errorBox)){
featureMenu.removeChild(errorBox);
}
featureMenu.appendChild(loadingOverlay);
let low = 0;
if(ratingFrom.value != ""){
low = ratingFrom.value;
}
let high = 3200;
if(ratingTo.value != ""){
high = ratingTo.value;
}
let data = await fetch("https://codeforces.com/api/problemset.problems");
data = await data.json();
let problems = data.result.problems;
let found = false;
while(problems.length != 0){
let problemNo = Math.floor(Math.random() * 10000000000) % problems.length;
let problem = problems[problemNo];
if(problem.rating && problem.rating >= low && problem.rating <= high){
window.open(`https://codeforces.com/problemset/problem/${problem.contestId}/${problem.index}`);
featureMenu.classList.toggle("invis");
found = true;
break;
}else{
problems.splice(problemNo, 1);
}
}
if(!found){
featureMenu.appendChild(errorBox);
}
featureMenu.removeChild(loadingOverlay);
return;
}
featureMenu.addEventListener("submit", (e) => {
e.preventDefault();
pickRandom();
});
return;
}
})();