-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpbinfo-get-unsolved.js
262 lines (214 loc) · 9.15 KB
/
pbinfo-get-unsolved.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
// restoring console.log: https://stackoverflow.com/questions/7089443/restoring-console-log
var i = document.createElement('iframe')
i.style.display = 'none'
document.body.appendChild(i)
window.console = i.contentWindow.console
console.clear()
// link prompt
let pageLink = prompt('Pune un link către categoria de probleme de unde vrei să obții problemele nerezolvate.\nMergi la o clasă, alege o categorie, și copiază link-ul aici.\n\nExemple:\n\nhttps://www.pbinfo.ro/probleme/categorii/5/algoritmi-elementari-cifrele-unui-numar\n\nhttps://www.pbinfo.ro/?pagina=probleme-lista&disciplina=0&clasa=-1&dificultate=0&folosesc_consola=-1&eticheta=52')
pageLink = pageLink.replaceAll(/(\?|&)start\d+/g, '')
// clearing site
document.head.innerHTML = ''
document.body.innerHTML = ''
const title = document.createElement('h2')
title.style.display = 'block'
title.innerHTML = '<span style="color: red";> pbinfo-get-unsolved.js</span>. <a href="https://github.com/ezluci/pbinfo-get-unsolved" target="_blank"><i>GitHub</i></a>'
document.body.appendChild(title)
const style = document.createElement('style')
style.innerHTML = `
a:hover {
cursor: pointer;
}
td {
border: 1px solid black;
}
`
document.head.appendChild(style)
// logging
const logDiv = document.createElement('div')
logDiv.id = 'log'
document.body.appendChild(logDiv)
function addLog(msg) {
const date = new Date()
const msgEl = document.createElement('span')
msgEl.innerHTML = '<b>[' + date.getHours().toString().padStart(2, '0') + ':' + date.getMinutes().toString().padStart(2, '0') + ':' + date.getSeconds().toString().padStart(2, '0') + '] - </b>' + msg
msgEl.style.display = 'block'
logDiv.appendChild(msgEl)
window.scroll(0, logDiv.scrollHeight)
}
// start script
const reqPageEl = document.createElement('div')
reqPageEl.style.display = 'none'
document.body.appendChild(reqPageEl)
addLog(`Link către categoria de probleme: <a href="${pageLink}"><i>${pageLink}</i></a>`)
const problems = []
let table = document.createElement('table')
const sorted = {cnt: 1, id: 0, score: 0, difficulty: 0, postedBy_name: 0, author: 0, source: 0}
table.style.width = '75%'
table.style.minWidth = '450px'
table.style.maxWidth = '1050px'
function quickSort(array, left, right, sortType) {
let index;
if (array.length > 1) {
index = partition(array, left, right, sortType); //index returned from partition
if (left < index - 1) { //more elements on the left side of the pivot
quickSort(array, left, index - 1, sortType);
}
if (index < right) { //more elements on the right side of the pivot
quickSort(array, index, right, sortType);
}
}
return array;
}
function partition(array, left, right, sortType) {
let pivot = array[Math.floor((right + left) / 2)][sortType],
i = left, //left pointer
j = right; //right pointer
while (i <= j) {
while (array[i][sortType] < pivot) {
i++;
}
while (array[j][sortType] > pivot) {
j--;
}
if (i <= j) {
[array[i], array[j]] = [array[j], array[i]]; //swap two elements
i++;
j--;
}
}
return i;
}
function sortTable(sortType) {
if (sorted[sortType] === 0) {
['cnt', 'id', 'score', 'difficulty', 'postedBy_name', 'author', 'source'].filter((val) => {return val !== sortType}).forEach(type => {
sorted[type] = 0;
});
sorted[sortType] = 1;
}
else {
sorted[sortType] *= -1;
}
quickSort(problems, 0, problems.length - 1, sortType);
if (sorted[sortType] === -1) {
problems.reverse();
}
updateTable();
}
function updateTable() {
function sortSymbol(sortType)
{
if (sorted[sortType] === 1)
return '▼'
if (sorted[sortType] === -1)
return '▲'
return '▶'
}
function numberToDifficulty(nr)
{
if (nr === 0)
return 'ușoară'
if (nr === 1)
return 'medie'
if (nr === 2)
return 'dificilă'
if (nr === 3)
return 'concurs'
}
function numberToDifficultyColor(nr)
{
if (nr === 0)
return '5cb85c'
if (nr === 1)
return 'f0ad4e'
if (nr === 2)
return '5bc0de'
if (nr === 3)
return 'd9534f'
}
table.innerHTML = `
<tr style="font-weight: bold;">
<td style="min-width: 5em; user-select: none;"><a onclick="sortTable('cnt')">Contor ${sortSymbol('cnt')}</a></td>
<td style="min-width: 10em; user-select: none;"><a onclick="sortTable('id')">Nume ${sortSymbol('id')}</a></td>
<td style="min-width: 5em; user-select: none;"><a onclick="sortTable('score')">Punctaj ${sortSymbol('score')}</a></td>
<td style="min-width: 6.5em; user-select: none;"><a onclick="sortTable('difficulty')">Dificultate ${sortSymbol('difficulty')}</a></td>
<td style="min-width: 13em; user-select: none;"><a onclick="sortTable('postedBy_name')">Postată de ${sortSymbol('postedBy_name')}</a></td>
<td style="min-width: 10em; user-select: none;"><a onclick="sortTable('author')">Autor ${sortSymbol('author')}</a></td>
<td style="min-width: 10em; user-select: none;"><a onclick="sortTable('source')">Sursa problemei ${sortSymbol('source')}</a></td>
</tr>
`
let i = 1
problems.forEach(problem => {
const row = document.createElement('tr')
row.innerHTML = `
<td>${problem.cnt}.</td>
<td><a href="${problem.link}" target="_blank">#${problem.id} - ${problem.name}</a></td>
<td>${problem.score}p</td>
<td><span style="color: white; background-color:#${numberToDifficultyColor(problem.difficulty)};">${numberToDifficulty(problem.difficulty)}</span></td>
<td><a target="_blank" href="${problem.postedBy_link}"><img style="vertical-align: middle; width: 1.1em;" src="${problem.postedBy_img}"> ${problem.postedBy_name}</a></td>
<td>${problem.author}</td>
<td>${problem.source}</td>
`
table.appendChild(row)
})
}
(async function Start(pag) {
const xhr = new XMLHttpRequest()
xhr.open('get', pageLink + (pageLink.includes('?') ? '&' : '?') + 'start=' + 10*(pag-1))
xhr.setRequestHeader('Content-Type', 'text/plain')
xhr.onload = () => {
reqPageEl.innerHTML = xhr.response
const pageProblems = reqPageEl.getElementsByClassName('col-lg-8 col-md-8')[0].getElementsByClassName('panel panel-info')
let solvedProbsPage = 0
let allProbsPage = 0
for (let i = 0; i < pageProblems.length; ++i) {
const cnt = problems.length + 1
const id = parseInt(pageProblems[i].children[0].children[0].children[0].innerText.trim().slice(1))
const name = pageProblems[i].children[0].children[0].children[1].innerText.trim()
const link = pageProblems[i].children[0].children[0].children[1].href.trim()
const difficulty =
(pageProblems[i].getElementsByClassName('list-unstyled list-inline')[0].innerText.includes('ușoară') ? 0 :
pageProblems[i].getElementsByClassName('list-unstyled list-inline')[0].innerText.includes('medie') ? 1 :
pageProblems[i].getElementsByClassName('list-unstyled list-inline')[0].innerText.includes('dificilă') ? 2 :
3)
let source = pageProblems[i].children[2].children[0].children[0]
const postedBy_DOM = pageProblems[i].getElementsByClassName('fas fa-upload')[0].parentNode.children[1].children[0]
const postedBy_link = postedBy_DOM.href
const postedBy_name = postedBy_DOM.innerText.trim()
let postedBy_img = postedBy_DOM.children[0].src
if (new URL(postedBy_img).hostname === 'www.gravatar.com')
postedBy_img = postedBy_img.replace(/&s=*/, '&s=128')
else if (new URL(postedBy_img).hostname === 'www.pbinfo.ro')
postedBy_img = postedBy_img.replace(/&gsize=*/, '&gsize=128')
else
console.error('pbinfo-get-unsolved.js\nUnknown link format for profile image')
let author = pageProblems[i].getElementsByClassName('far fa-edit')[0]?.parentNode.innerText.trim()
let score = parseInt(pageProblems[i].children[2].children[1].children[0].innerText.trim().slice(7).trim())
if (problems.findIndex((val => {return val.id === id})) !== -1)
continue
allProbsPage ++
if (source.tagName === 'P')
source = source.innerText.trim()
else
source = ''
if (!author)
author = ''
if (score === 100) {
solvedProbsPage ++
} else {
if (isNaN(score))
score = 0
problems.push({cnt, id, name, link, difficulty, score, postedBy_link, postedBy_name, postedBy_img, author, source})
}
}
if (pageProblems.length === 0) {
updateTable()
addLog(`<u>Am terminat de extras problemele.</u> Sunt ${problems.length} probleme nerezolvate. <a onclick="document.body.appendChild(table); window.scrollBy(0, 150)">Deschide tabelul cu probleme.</a>`)
return
} else {
addLog(`Pagina ${pag} are ${solvedProbsPage}/${allProbsPage} probleme rezolvate.`)
}
Start(pag+1)
}
xhr.send()
}(1))