This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (129 loc) · 3.83 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"time"
"github.com/zserge/lorca"
)
const (
minimumWaitTime = 3000
maximumWaitTime = 5000
)
type config struct {
Login string `json:"login"`
Password string `json:"password"`
}
func main() {
dat, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatalf("Cannot load config file: %s", err)
}
cfg := &config{}
err = json.Unmarshal(dat, cfg)
if err != nil {
log.Fatalf("Cannot unmarshal config file content into config struct: %s", err)
}
ui, err := lorca.New("https://instaling.pl/teacher.php?page=login", "", 800, 600)
if err != nil {
log.Fatal(err)
}
ui.Bind("closeWindow", func() {
ui.Close()
})
defer ui.Close()
go func() {
for !waitForElement(ui, `'form[action="./teacher.php?page=teacherActions"'`) {
}
time.Sleep(time.Duration(rand.Intn(maximumWaitTime-minimumWaitTime)+minimumWaitTime) * time.Millisecond)
ui.Eval(fmt.Sprintf(`
document.querySelector("#log_email").value = "%s";
document.querySelector("#log_password").value = "%s";
document.querySelector('input[type="submit"]').click();
`, cfg.Login, cfg.Password))
for !waitForElement(ui, `'#session_button'`) {
}
time.Sleep(time.Duration(rand.Intn(maximumWaitTime-minimumWaitTime)+minimumWaitTime) * time.Millisecond)
ui.Eval(`
document.querySelector('#session_button').click();
`)
for !waitForElement(ui, `'#start_session_button'`) && !waitForElement(ui, `'#continue_session_button'`) {
}
time.Sleep(time.Duration(rand.Intn(maximumWaitTime-minimumWaitTime)+minimumWaitTime) * time.Millisecond)
ui.Eval(getMainScript())
}()
<-ui.Done()
}
func waitForElement(ui lorca.UI, querySelector string) bool {
return ui.Eval(fmt.Sprintf("!!document.querySelector(%s)", querySelector)).Bool()
}
func getMainScript() string {
return `
const wait = (amount = 0) =>
new Promise((resolve) => setTimeout(resolve, amount));
const randomBreak = (min, max) => wait(Math.floor(Math.random() * max) + min);
const writeAnswerToInput = async (response) => {
const answerInput = document.querySelector("#answer");
const checkButton = document.querySelector("#check");
const knowNewButton = document.querySelector("#know_new");
const dontKnowNewButton = document.querySelector("#dont_know_new");
const skipButton = document.querySelector("#skip");
if (
knowNewButton &&
dontKnowNewButton &&
document.querySelector("#new_word_form").style.display !== "none"
) {
await randomBreak(300, 600);
if (Math.random() >= 0.5) {
knowNewButton.click();
} else {
dontKnowNewButton.click();
}
await randomBreak(300, 600);
skipButton.click();
return;
}
answerInput.focus();
let offset = 0;
await randomBreak(400, 750);
while (offset < response.word.length) {
answerInput.value += response.word[offset];
await randomBreak(100, 150);
offset++;
}
checkButton.click();
};
const clickNextWord = async () => {
const nextWordButton = document.querySelector("#nextword");
nextWordButton.click();
};
const oldXHROpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function (method, url) {
this.addEventListener("load", function () {
if (url.includes("generate_next_word.php")) {
const json = JSON.parse(this.responseText);
if (json.summary) {
window.closeWindow();
} else {
writeAnswerToInput(json);
}
} else if (url.includes("save_answer.php")) {
clickNextWord();
}
});
return oldXHROpen.apply(this, arguments);
};
if (
document.querySelector("#start_session_button") &&
document.querySelector("#start_session_page").style.display === "block"
) {
console.log("?");
document.querySelector("#start_session_button").click();
} else {
console.log("?");
document.querySelector("#continue_session_button").click();
}
`
}