-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.js
158 lines (144 loc) · 5.92 KB
/
bot.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
//paste values obtained from answers.js here
let flashcards = [];
let terms = [];
let definitions = [];
for(let i = 0; i < flashcards.length; i+=2){
terms.push(flashcards[i]);
definitions.push(flashcards[i+1]);
}
function findMatch(t, arr){
let other;
let target = t.childNodes[0].textContent;
if(t.childNodes.length > 1){
for(let i = 1; i < t.childNodes.length; i++){
target += t.childNodes[i].textContent;
}
}
for(let i = 0; i < terms.length; i++){
if(terms[i] === target){
other = definitions[i];
break;
}
if(definitions[i] === target){
other = terms[i];
break;
}
}
for(let i = 1; i < arr.length; i++){
let ans = arr[i].childNodes[0].childNodes[0].textContent;
if(arr[i].childNodes[0].childNodes.length > 1){
for(let j = 1; j < arr[i].childNodes[0].childNodes.length; j++){
ans += arr[i].childNodes[0].childNodes[j].textContent;
}
}
if(ans === other){
return i;
}
}
return -1;
}
function eventFire(el, etype){
if(el.fireEvent) {
el.fireEvent('on' + etype);
} else {
let evObj = document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
}
}
function solve(delay){
console.log("SCRIPT INITIALIZED");
//600ms delay by default; varies by set, so modify to your needs; anything less and Quizlet might reject your score
setTimeout(function(){
let tiles = Array.from(document.getElementsByClassName("MatchModeQuestionGridTile-text"));
while(tiles.length > 0){
let idx = findMatch(tiles[0].childNodes[0], tiles);
let fm, sm = "";
if(tiles[0].childNodes[0].childNodes.length > 1) {
for(let i = 0; i < tiles[0].childNodes[0].childNodes.length; i++){
fm += tiles[0].childNodes[0].childNodes[i].textContent;
}
} else {
fm = tiles[0].childNodes[0].childNodes[0].textContent;
}
if(tiles[idx].childNodes[0].childNodes.length > 1) {
for(let i = 0; i < tiles[idx].childNodes[0].childNodes.length; i++){
sm += tiles[idx].childNodes[0].childNodes[i].textContent;
}
} else {
sm = tiles[idx].childNodes[0].childNodes[0].textContent;
}
console.log("MATCH FOUND: " + fm + " AND " + sm);
eventFire(tiles[0].childNodes[0].childNodes[0], "pointerdown");
eventFire(tiles[idx].childNodes[0].childNodes[0], "pointerdown");
tiles.splice(idx, 1);
tiles.splice(0, 1);
}
console.log("SCRIPT TERMINATED");
}, delay);
}
function inject(){
ui[2].disabled = true;
let opacity = 1.00;
let fadeOut = setInterval(function(){
if(opacity > 0){
opacity -= 0.01;
for(let i = 0; i < ui.length-1; i++){
ui[i].style.opacity = opacity;
}
} else {
clearInterval(fadeOut);
ui[0].textContent = "Script loaded.";
ui[1].textContent = "Set the rough amount of time it takes to run the script. By default it is 600ms, which should be sufficient for a 0.5s time without going under Quizlet's minimum. Tweak at your own risk."
ui[2].style.display = "none";
document.getElementsByClassName("UIButton--hero")[0].style.opacity = 0.00;
document.getElementsByClassName("UIButton--hero")[0].style.display = "inline-block";
ui[2] = document.getElementsByClassName("UIButton--hero")[0];
ui[3].textContent = "Inject script";
ui[4].style.opacity = 0.00;
ui[4].style.display = "inline-block";
ui[4].style.marginBottom = "30px";
ui[4].style.fontSize = "18px";
ui[4].defaultValue = 600;
ui[4].type = "number";
ui[4].step = 100;
ui[4].min = 0;
ui[5].style.marginTop = "20px";
ui[5].style.display = "inline-block";
let fadeIn = setInterval(function(){
if(opacity < 1.00){
opacity += 0.01;
for(let i = 0; i < ui.length; i++){
ui[i].style.opacity = opacity;
}
} else {
clearInterval(fadeIn);
oldButton.disabled = false;
ui[2].onclick = function(){
let delay = ui[4].value;
solve(delay);
};
}
}, 10);
}
}, 5);
}
let newButton = document.getElementsByClassName("UIButton--hero")[0].cloneNode(true);
newButton.style.marginLeft = "0";
document.getElementsByClassName("UIButton--hero")[0].style.display = "none";
let oldButton = document.getElementsByClassName("UIButton--hero")[0];
oldButton.disabled = true;
let input = document.createElement("input");
input.style.display = "none";
let name = document.createElement("p");
name.textContent = "Code by Hank Magan";
name.style.display = "none";
document.getElementsByClassName("UIButton--hero")[0].remove();
document.getElementsByClassName("MatchModeInstructionsModal")[0].appendChild(input);
document.getElementsByClassName("MatchModeInstructionsModal")[0].appendChild(oldButton);
document.getElementsByClassName("MatchModeInstructionsModal")[0].appendChild(newButton);
document.getElementsByClassName("MatchModeInstructionsModal")[0].appendChild(name);
let last = document.getElementsByClassName("UIHeading--three").length - 1;
//header, text content, button, button label, input, name
let ui = [document.getElementsByClassName("UIHeading--three")[last], document.getElementsByClassName("UIParagraph")[0], newButton, document.getElementsByClassName("UIButton-wrapper")[0], input, name];
newButton.onclick = inject;