forked from TheLazySquid/GimkitCheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.js
239 lines (210 loc) · 7.33 KB
/
general.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
(function(){
// migrate from the old save/load system
let kits = localStorage.getItem("gc_cheat_kits")
if(kits){
kits = JSON.parse(kits);
let answers = {}
for(let key of Object.keys(kits)){
answers = {...answers, ...kits[key]}
}
localStorage.setItem("gc_cheat_answers", JSON.stringify(answers))
localStorage.removeItem("gc_cheat_kits")
}
let answers = JSON.parse(localStorage.getItem('gc_cheat_answers') ?? "{}");
let color = prompt("Would you like to color in the answers? (Y/n)", "Y").toLowerCase() == "y";
const save = () => {
localStorage.setItem("gc_cheat_answers", JSON.stringify(answers));
}
const stripStyles = (str) => {
// remove all text after style=" and before "
return str.replace(/style=".*?"/g, "");
}
var listenedButtons = [];
let lastQuestion = null;
let lastAnswer = null;
let lastAnswerType = null;
var newAnswers = 0;
const selector = '.notranslate, img[alt="Answer Choice"], img[alt="Question"]';
let active = true;
let customStyles = new CSSStyleSheet();
customStyles.replaceSync(`
.answer-hover {
transition: filter 0.18s ease-in-out;
}
.answer-hover:hover {
filter: brightness(1.2);
}
`);
// add it to the document
document.adoptedStyleSheets.push(customStyles);
function pageChange() {
if(!active) return;
// the menu was probably opened
let items = Array.from(document.querySelectorAll(selector))
if(items.length > 0){
if(items.length == 1 && (document.querySelector("input") == null)){
// this is an incorrect answer
if(!lastQuestion) return;
newAnswers++;
if(!answers[lastQuestion]) answers[lastQuestion] = {};
if(lastAnswerType == "text"){
answers[lastQuestion].textAnswer = items[0].innerHTML;
}else{
answers[lastQuestion].correct = items[0].parentElement.innerHTML;
}
save();
console.log(`Total answers stored: ${Object.keys(answers).length}\nNew answers this session: ${newAnswers}`)
return;
}
lastQuestion = stripStyles(items[0].parentElement.innerHTML);
// remove the question text
if(items.length == 6) items = items.slice(2);
else items = items.slice(1);
// if the question was already answered, highlight the correct answer
if(lastQuestion in answers){
let answer = answers[lastQuestion];
if(answer.textAnswer){
if(items[0].parentElement.querySelector(".correct-answer") != null) return;
let answerText = answer.textAnswer;
let answerNode = document.createElement("div");
answerNode.innerHTML = `Correct answer: ${answerText}`;
answerNode.classList.add("correct-answer");
answerNode.style.fontSize = "16px";
items[0].parentElement.append(answerNode);
let input = document.querySelector("input");
input.value = answerText.slice(0, -1);
return;
}
// get the colors of the options
let colors = [];
for(let i = 0; i < items.length; i++) {
let item = items[i];
let parentAmount = 5;
if(item.nodeName == "IMG") parentAmount = 1;
let checkNode = item.nthparent(parentAmount);
let bgColor = getComputedStyle(checkNode).background;
colors.push(bgColor);
}
let correctSeen = false;
let correctExists = false;
for(let i = 0; i < items.length; i++){
let item = items[i];
if(item.nodeName == "IMG") parentAmount = 1;
if(stripStyles(item.parentElement.innerHTML) == stripStyles(answer.correct)){
correctExists = true;
break;
}
}
if(!correctExists) {
// remove the question from the answers
delete answers[lastQuestion];
return;
}
for(let i = 0; i < items.length && answer.correct; i++){
// color and move answers
let item = items[i]
let parentAmount = 3;
if(item.nodeName == 'IMG') parentAmount = 1;
if(stripStyles(item.parentElement.innerHTML) == stripStyles(answer.correct)){
// color in and move the correct answer to the bottom
if(color) item.nthparent(parentAmount).style.backgroundColor = "green";
else{
let changeItem = item.nthparent(5);
if(item.nodeName == "IMG") changeItem = item.parentElement;
// swap the item's background color to match the one it's supposed to be
changeItem.style.background = colors[3];
changeItem.classList.add("answer-hover");
}
let outer = item.nthparent(parentAmount*2);
let buttonParent = document.querySelectorAll(selector)[2]
if(buttonParent.nodeName == "IMG") buttonParent = buttonParent.nthparent(3);
else buttonParent = buttonParent.nthparent(7);
let buttons = Array.from(buttonParent.children)
if(buttons.indexOf(outer) != buttons.length-1){
buttonParent.append(outer);
}
correctSeen = true;
}else{
// color incorrect answers
if(color) item.nthparent(parentAmount).style.backgroundColor = "red";
else if(correctSeen){
let changeItem = item.nthparent(5);
if(item.nodeName == "IMG") changeItem = item.parentElement;
// swap the item's background color to match the one it's supposed to be
changeItem.style.background = colors[i-1];
changeItem.classList.add("answer-hover");
}
}
}
}
// check if the question is a text input
let input = document.querySelector(".sc-RpuvT");
if(input != null){
lastAnswerType = "text";
input.addEventListener("input", (e) => {
lastAnswer = e.target.value;
})
return;
}
lastAnswerType = "button";
for(let i = 0; i < items.length; i++){
let button = items[i].nthparent(6);
if(items[i].nodeName == "IMG") button = items[i].nthparent(2);
if(listenedButtons.indexOf(button) == -1){
listenedButtons.push(button);
}
button.addEventListener("click", function(){
lastAnswer = this.querySelector(selector).parentElement.innerHTML;
})
}
}else{
if(answers[lastQuestion]?.correct != undefined) return;
// figure out whether it was right or not
let greenBgExists = Array.from(document.querySelectorAll("div")).some(e => getComputedStyle(e).backgroundColor == "rgb(56, 142, 60)")
if(greenBgExists){
newAnswers++;
if(lastAnswerType == "text"){
// answer was text
if(!answers[lastQuestion]) answers[lastQuestion] = {};
answers[lastQuestion].textAnswer = lastAnswer;
}else{
// answer was a button
if(!answers[lastQuestion]) answers[lastQuestion] = {};
answers[lastQuestion].correct = stripStyles(lastAnswer);
}
console.log(`Total answers stored: ${Object.keys(answers).length}\nNew answers this session: ${newAnswers}`)
save();
}
}
}
let observer = new MutationObserver(pageChange);
observer.observe(document.body, {subtree: true, childList: true});
HTMLElement.prototype.nthparent = function(n) {
let parent = this;
while (n-- && parent) parent = parent.parentElement;
return parent;
}
// check if the questions are open when the script is loaded
pageChange();
// When shift is hit three times in quick succession, toggle active
let shiftCount = 0;
let shiftTimeout = null;
document.addEventListener("keydown", (e) => {
if(e.key == "Shift"){
shiftCount++;
if(shiftTimeout != null) clearTimeout(shiftTimeout);
shiftTimeout = setTimeout(() => {
shiftCount = 0;
}, 1000);
if(shiftCount == 3){
active = !active;
if(active) {
console.log("Active");
pageChange();
}
else console.log("Inactive");
shiftCount = 0;
}
}
})
})();