-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
177 lines (158 loc) · 7.44 KB
/
background.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
const url = window.location.href;
var id;
var index;
if(url[23] == 'c'){
var data = url.slice(31, url.length);
var dataArray = data.split(/[\/\?]/);
id = dataArray[0];
index = dataArray[2];
}
else{
var data = url.slice(42, url.length);
var dataArray = data.split(/[\/\?]/);
id = dataArray[0];
index = dataArray[1];
}
const requestURL = `https://codeforces.com/api/contest.standings?contestId=${id}&from=1&count=1`;
fetchData();
async function fetchData(){
var rating = null;
var tags = null;
try{
const response = await fetch(requestURL);
const data = await response.json();
const problemsList = data.result.problems;
if(data.status == "OK"){
for(var i = 0; i < problemsList.length; i++){
if(problemsList[i].index == index){
rating = problemsList[i].rating;
tags = problemsList[i].tags;
break;
}
}
}
else{
rating = -1;
}
}
catch(e){
rating = -1;
}
var toInsert;
if(rating == null){
toInsert = `
<div class="roundbox sidebox" style="">
<div class="roundbox-lt"> </div>
<div class="roundbox-rt"> </div>
<div class="caption titled">→ CF GetRating
<div class="top-links"></div>
</div>
<div style="padding: 0.5em;">
<div style="margin-bottom: 5px; font-size:0.8em;color: red;">
Rating not available for this question.
</div>
`
tags.forEach((tag) => {
toInsert += `
<div class="roundbox hidden" style="margin:2px; padding:0 3px 2px 3px; background-color:#f0f0f0;float:left;">
<div class="roundbox-lt"> </div>
<div class="roundbox-rt"> </div>
<div class="roundbox-lb"> </div>
<div class="roundbox-rb"> </div>
<span class="tag-box" style="font-size:1.2rem;" title="Difficulty">
${tag}
</span>
</div>
`
});
toInsert += `
<div style="clear:both;text-align:right;font-size:1.1rem;">
</div>
</div>
<div style="text-align:center;">
<button onclick='
document.querySelectorAll(".hidden").forEach((item) => {
item.classList.remove("hidden");
});
'
style="margin-bottom:3px; width: 50%;">Show All Tags</button>
</div>
<div style="text-align:center;">
<a href="https://codeforces.com/contest/${id}/standings" target="_blank"><button style="margin-bottom:15px; margin-top: 3px; width: 50%;">Contest Standings</button></a>
</div>
</div>
`
}
else if(rating == -1){
toInsert = `
<div class="roundbox sidebox" style="">
<div class="roundbox-lt"> </div>
<div class="roundbox-rt"> </div>
<div class="caption titled">→ CF GetRating
<div class="top-links"></div>
</div>
<div>
<div style="margin:1em;font-size:0.8em;color: red;">
Codeforces API Error.
</div>
</div>
<div style="text-align:center;margin-bottom:15px">
<a href="https://codeforces.com/contest/${id}/standings" target="_blank"><button>Contest Standings</button></a>
</div>
</div>
`
}
else{
toInsert = `
<div class="roundbox sidebox" style="">
<div class="roundbox-lt"> </div>
<div class="roundbox-rt"> </div>
<div class="caption titled">→ CF GetRating
<div class="top-links"></div>
</div>
<div style="padding: 0.5em;">
<div class="roundbox" style="margin:2px; padding:0 3px 2px 3px; background-color:#f0f0f0;float:left;">
<div class="roundbox-lt"> </div>
<div class="roundbox-rt"> </div>
<div class="roundbox-lb"> </div>
<div class="roundbox-rb"> </div>
<span class="tag-box" style="font-size:1.2rem;" title="Difficulty">
*${rating}
</span>
</div>
`
tags.forEach((tag) => {
toInsert += `
<div class="roundbox hidden" style="margin:2px; padding:0 3px 2px 3px; background-color:#f0f0f0;float:left;">
<div class="roundbox-lt"> </div>
<div class="roundbox-rt"> </div>
<div class="roundbox-lb"> </div>
<div class="roundbox-rb"> </div>
<span class="tag-box" style="font-size:1.2rem;" title="Difficulty">
${tag}
</span>
</div>
`
});
toInsert += `
<div style="clear:both;text-align:right;font-size:1.1rem;">
</div>
</div>
<div style="text-align:center;">
<button onclick='
document.querySelectorAll(".hidden").forEach((item) => {
item.classList.remove("hidden");
});
'
style="margin-bottom:3px; width: 50%;">Show All Tags</button>
</div>
<div style="text-align:center;">
<a href="https://codeforces.com/contest/${id}/standings" target="_blank"><button style="margin-bottom:15px; margin-top: 3px; width: 50%;">Contest Standings</button></a>
</div>
</div>
`
}
const getRatingBox = document.createElement("div");
getRatingBox.innerHTML = toInsert;
document.querySelector("#sidebar").appendChild(getRatingBox);
}