This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhack.js
127 lines (100 loc) · 2.93 KB
/
hack.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
const PolitHack = (function() {
const COLOR = {
HAS_MISTAKES: 'feffa3',
CORRECT: 'a3c8ff',
SERIES: '9bf7bc',
WRONG: 'ffa3a3'
};
function bubble(text, color = COLOR.CORRECT) {
return `<span style="border-radius:3px;margin-right:5px;background:#${color};padding:3px 7px;line-height:2em;">${text}</span>`;
}
function labelHasMistakes(target, text) {
target.innerHTML += bubble(text, COLOR.HAS_MISTAKES);
}
function labelCorrect(target, text) {
target.innerHTML += bubble(text, COLOR.CORRECT);
}
function labelSeries(target, text) {
target.innerHTML += bubble(text, COLOR.SERIES);
}
function labelWrong(target, text) {
target.innerHTML += bubble(text, COLOR.WRONG);
}
function labelVariant(target, n) {
target.innerHTML += `<p>Вариант ${n}</p>`
}
const QUESTION_MASK = /[^а-яА-ЯёЁ0-9a-zA-Z]/g;
function areEqualQuestions(question1, question2) {
return question2.replace(QUESTION_MASK, '').toLowerCase()
.startsWith(question1.replace(QUESTION_MASK, '').toLowerCase())
}
function getTaskBlocks() {
return document.getElementsByClassName('que');
}
function textOf(element) {
let text = '';
for (let each of element.childNodes) {
if (each.nodeName === '#text')
text += each.textContent.trim();
if (
each.tagName === 'P' ||
each.tagName === 'A' ||
each.tagName === 'EM' ||
each.tagName === 'STRONG'
) text += textOf(each);
if (each.tagName === 'DIV' && !each.classList.contains('ablock'))
text += textOf(each);
}
return text + ' ';
}
function shorten(text) {
return text.replace(/\s+/g, ' ').trim();
}
function formulationOf(taskBlock) {
return shorten(textOf(taskBlock.querySelector('.formulation')));
}
function headerOf(taskBlock) {
return taskBlock.querySelector('.qtext') || taskBlock.querySelector('.formulation');
}
function isAnswerAvailable(task) {
return task.answer.length !== 0
}
function labelAll(tasks) {
for (let each of getTaskBlocks()) {
const question = formulationOf(each);
const header = headerOf(each);
let found = false;
for (let task of tasks) {
if (areEqualQuestions(task.question, question)) {
if (isAnswerAvailable(task)) {
for (let item of task.answer)
labelCorrect(header, item);
} else {
for (let n = 1; n <= task.variants.length; n++) {
const answers = task.variants[n - 1];
if (answers.length > 0) {
const correctness = answers[answers.length - 1];
let correctCount = '';
if (correctness.startsWith('Верных')) {
correctCount = ' (' + correctness + ')';
answers.pop();
}
labelVariant(header, n + correctCount);
for (let item of task.variants[n - 1])
labelHasMistakes(header, item);
}
}
}
found = true;
break;
}
}
if (!found) {
labelWrong(header, 'No info (');
}
}
}
return {
labelAll: labelAll
};
})();