-
Notifications
You must be signed in to change notification settings - Fork 0
/
loginparser.cpp
219 lines (212 loc) · 7.73 KB
/
loginparser.cpp
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
#include "loginparser.h"
#define db qDebug() << this <<
LoginParser::LoginParser(QObject *parent) : QObject(parent)
{
}
void LoginParser::loadFiles()
{
const QString dir = QApplication::applicationDirPath().append("/Scripts/");
QDir directory(dir);
QStringList logindir = directory.entryList(QStringList() << "*.txt",QDir::Files);
foreach(auto file, logindir)
{
QFile f(dir + file);
bool bAbortFile = false;
if(f.open(QFile::ReadOnly))
{
login_s s;
QTextStream in(&f);
while (!in.atEnd())
{
QString line = in.readLine();
element_s El;
if (line == "Domain")
{
if(in.atEnd())
{
qWarning() << "ERROR: DOMAIN IS EMPTY";
bAbortFile = true;
break;
}
s.Element.insert("Domain", in.readLine().simplified());
}
if (line == "TextFieldByID")
{
if(in.atEnd())
{
qWarning() << "ERROR: TextField IS EMPTY OR MISSING";
bAbortFile = true;
break;
}
El.type = TextField;
El.elements << in.readLine().simplified();
if(in.atEnd())
{
qWarning() << "ERROR: TextField IS EMPTY OR MISSING";
bAbortFile = true;
break;
}
El.elements << in.readLine().simplified();
}
if (line == "TextFieldBySelector")
{
if(in.atEnd())
{
qWarning() << "ERROR: TextField IS EMPTY OR MISSING";
bAbortFile = true;
break;
}
El.type = TextFieldSel;
El.elements << in.readLine().simplified();
if(in.atEnd())
{
qWarning() << "ERROR: TextField IS EMPTY OR MISSING";
bAbortFile = true;
break;
}
El.elements << in.readLine().simplified();
}
if (line == "ButtonByID")
{
if(in.atEnd())
{
qWarning() << "ERROR: ButtonByID IS EMPTY";
bAbortFile = true;
break;
}
El.type = ButtonById;
El.elements << in.readLine().simplified();
}
if (line == "LocationOverride")
{
if(in.atEnd())
{
qWarning() << "ERROR: LocationOverride IS EMPTY";
bAbortFile = true;
break;
}
El.type = LocationOverride;
El.elements << in.readLine().simplified();
}
if (line == "ButtonBySelector")
{
if(in.atEnd())
{
qWarning() << "ERROR: ButtonBySelector IS EMPTY";
bAbortFile = true;
break;
}
El.type = ButtonBySelector;
El.elements << in.readLine().simplified();
}
if(!El.elements.isEmpty())
s.Elements << El;
}
f.close();
if(!bAbortFile)
Logins << s;
}
}
}
QString LoginParser::getLoginJavascript(QString url)
{
for(auto s: Logins)
{
if(s.Element.value("Domain") == url)
{
QString js;
qDebug() << this << s.TextField;
qDebug() << this << s.TextFieldSel;
qDebug() << this << s.ButtonById;
qDebug() << this << s.ButtonBySelector;
qDebug() << this << s.LocationOverride;
for(int i = 0; i < s.TextField.size(); ++i)
{
auto element = s.TextField.at(i++);
auto value = s.TextField.at(i);
js += "document.getElementById(\"" + element + "\").value=\""+value+"\";\n";
js += "document.getElementById(\"" + element + "\").dispatchEvent(new Event('change'));\n";
}
for(int i = 0; i < s.TextFieldSel.size(); ++i)
{
auto element = s.TextFieldSel.at(i++);
auto value = s.TextFieldSel.at(i);
js += "document.querySelectorAll('" + element + "')[0].value=\""+value+"\";\n";
js += "document.querySelectorAll('" + element + "')[0].dispatchEvent(new Event('change'));\n";
}
for(int i = 0; i < s.ButtonById.size(); ++i)
{
auto element = s.ButtonById.at(i);
js += "document.getElementById(\""+ element + "\").click();";
}
for(int i = 0; i < s.ButtonBySelector.size(); ++i)
{
auto element = s.ButtonBySelector.at(i);
js += "document.querySelectorAll('"+ element + "')[0].click();";
}
for(int i = 0; i < s.LocationOverride.size(); ++i)
{
auto element = s.LocationOverride.at(i);
js += "location.href=\""+ element + "\";";
}
return js;
}
}
return QString();
}
void LoginParser::runLogins(QString url, QWebEnginePage *page)
{
for(auto s: Logins)
{
if(s.Element.value("Domain") == url)
{
QString js;
for(int x = 0; x < s.Elements.size(); ++x)
{
element_s elements = s.Elements.at(x);
switch(elements.type)
{
case TextField:
{
auto element = elements.elements.at(0);
auto value = elements.elements.at(1);
js = "document.getElementById(\"" + element + "\").value=\""+value+"\";\n";
js += "document.getElementById(\"" + element + "\").dispatchEvent(new Event('change'));\n";
}
break;
case TextFieldSel:
{
auto element = elements.elements.at(0);
auto value = elements.elements.at(1);
js = "document.querySelectorAll('" + element + "')[0].value=\""+value+"\";\n";
js += "document.querySelectorAll('" + element + "')[0].dispatchEvent(new Event('change'));\n";
}
break;
case ButtonById:
{
auto element = elements.elements.at(0);
js = "document.getElementById(\""+ element + "\").click();";
}
break;
case ButtonBySelector:
{
auto element = elements.elements.at(0);
js = "document.querySelectorAll('"+ element + "')[0].click();";
}
break;
case LocationOverride:
{
auto element = elements.elements.at(0);
js = "location.href=\""+ element + "\";";
}
break;
default:
break;
}
// QThread::sleep(1);
db js;
page->runJavaScript(js);
}
}
}
}