forked from MarketSquare/robotframework-browser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogin.tsx
More file actions
407 lines (383 loc) · 15.4 KB
/
login.tsx
File metadata and controls
407 lines (383 loc) · 15.4 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import React, { ChangeEvent, MouseEvent, useRef, useState } from 'react';
function goesInvisible(event: React.MouseEvent<HTMLButtonElement>) {
event.persist();
console.log('Going hidden in 1 second');
setTimeout(() => {
//@ts-expect-error TypeScript can't infer that the element has style
event.target.style.visibility = 'hidden';
console.log('Went hidden');
}, 500);
}
function popup() {
window.open('../static/popup.html', 'width=400,height=200,scrollbars=yes');
}
function ProgressBar() {
const [width, setWidth] = useState(() => 10);
const enabled = useRef(false);
function frame() {
if (enabled.current && width < 100) {
setWidth(width + 1);
console.log('width growing');
}
}
React.useEffect(() => {
const interval = setInterval(frame, 10);
return () => clearInterval(interval);
});
return (
<div
id="progress"
style={{
position: 'absolute',
top: '660px',
left: '0px',
width: '400px',
height: '30px',
backgroundColor: 'gray',
}}
>
<div
id="progress_bar"
onClick={() => {
enabled.current = true;
}}
style={{ width: width + '%', height: '100%', backgroundColor: 'green' }}
/>
<div style={{ textAlign: 'center', color: 'black' }}> {width}% </div>
</div>
);
}
async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function delayedRequest() {
await sleep(200);
console.log(await fetch('/api/get/json'));
}
async function delayedRequestBig() {
await sleep(250);
console.log(await fetch('/api/get/json/big'));
}
function fileUploaded(uploadResultElement: React.RefObject<HTMLElement>, event: ChangeEvent<HTMLInputElement>) {
const files = event.target.files;
let fileNames = '';
if (files) {
for (let i = 0; i < files.length; i++) {
if (i > 0) {
fileNames = `${fileNames},${files[i].name}`;
} else {
fileNames = files[i].name;
}
}
uploadResultElement.current.innerHTML = fileNames;
} else {
uploadResultElement.current.innerHTML = 'no uploaded file';
}
}
function testPrompt(promptResultElement: React.RefObject<HTMLElement>) {
const input = prompt('Enter a value');
if (input) promptResultElement.current.innerHTML = input;
else promptResultElement.current.innerHTML = 'prompt_not_filled';
}
export default function Site() {
const username = useRef('');
const password = useRef('');
const [submit, setSubmit] = useState(false);
const uploadResult = React.createRef<HTMLDivElement>();
const promptResult = React.createRef<HTMLDivElement>();
const networkPinger = React.createRef<HTMLDivElement>();
const mouseDelayDiv = React.createRef<HTMLDivElement>();
const clickCount = React.createRef<HTMLDivElement>();
const mouseButton = React.createRef<HTMLDivElement>();
const coordinatesDivX = React.createRef<HTMLDivElement>();
const coordinatesDivY = React.createRef<HTMLDivElement>();
const keypresses = React.createRef<HTMLDivElement>();
const altKey = React.createRef<HTMLDivElement>();
const shiftKey = React.createRef<HTMLDivElement>();
const ctrlKey = React.createRef<HTMLDivElement>();
const metaKey = React.createRef<HTMLDivElement>();
let mouseDelay: number;
let mouseDownTime: number;
let click_Count = 0;
let countKeyPress = 0;
function eventMouseDown(e: MouseEvent<HTMLButtonElement>) {
mouseDownTime = new Date().getTime();
mouseButton.current!.innerHTML = '';
const mouseButtons = ['left', 'middle', 'right'];
mouseButton.current!.innerHTML = mouseButtons[e.button];
console.log(
`Mouse button: ${mouseButtons[e.button]}, X: ${e.pageX}, Y: ${e.pageY}, Time: ${new Date().toISOString()}`,
);
altKey.current!.innerHTML = e.altKey.toString();
shiftKey.current!.innerHTML = e.shiftKey.toString();
ctrlKey.current!.innerHTML = e.ctrlKey.toString();
metaKey.current!.innerHTML = e.metaKey.toString();
coordinatesDivX.current!.innerHTML = e.pageX.toString();
coordinatesDivY.current!.innerHTML = e.pageY.toString();
if (e.altKey && e.shiftKey) {
throw new EvalError('You are not allowed to use this site');
}
}
function eventMouseUp() {
const mouseupTime = new Date().getTime();
mouseDelay = mouseupTime - mouseDownTime;
click_Count += 1;
console.error(`${click_Count.toString()}`);
console.warn(`${mouseDelay.toString()}`);
clickCount.current!.innerHTML = click_Count.toString();
mouseDelayDiv.current!.innerHTML = mouseDelay.toString();
}
const handleSubmit = () => {
setSubmit(true);
};
function usernameChange(event: ChangeEvent<HTMLInputElement>) {
username.current = event.target.value;
countKeyPress += 1;
keypresses.current!.innerHTML = countKeyPress.toString();
}
function passwordChange(event: ChangeEvent<HTMLInputElement>) {
password.current = event.target.value;
}
function Body() {
if (submit) return <PostSubmit />;
else return <PreSubmit />;
}
function networkPing() {
fetch('/api/get/json')
.then(() => {
networkPinger.current!.innerText = 'Online';
})
.catch(() => {
networkPinger.current!.innerText = 'no connection';
});
}
React.useEffect(() => {
const interval = setInterval(networkPing, 500);
return () => clearInterval(interval);
}, []);
function PreSubmit() {
document.title = 'Login Page';
const tableStyle = {
fontSize: 'small',
};
return (
<>
<h1 id="heading1">Login Page</h1>
<p>Please input your user name and password and click the login button.</p>
<form name="login_form" onSubmit={handleSubmit}>
<table>
<tbody>
<tr>
<td>
<label htmlFor="username_field">User Name:</label>
</td>
<td>
<input id="username_field" size={30} type="text" onChange={usernameChange} />
</td>
</tr>
<tr>
<td>
<label htmlFor="password_field">Password:</label>
</td>
<td>
<input id="password_field" size={30} type="password" onChange={passwordChange} />
</td>
</tr>
<tr>
<td></td>
<td>
<input id="login_button" type="submit" value="LOGIN" />
</td>
</tr>
</tbody>
</table>
</form>
<textarea id="textarea51" defaultValue="Some initial text" />
<button id="goes_hidden" onClick={goesInvisible}>
Visible
</button>
<button id="pops_up" onClick={popup}>
Pops up a window
</button>
<button id="delayed_request" onClick={() => void delayedRequest()}>
Fires a request in 200ms
</button>
<button id="delayed_request_big" onClick={() => void delayedRequestBig()}>
Fires a big request in 250ms
</button>
<button id="alerts" onClick={() => alert('Am an alert')}>
Pops up an alert
</button>
<div id="prompt_result" ref={promptResult} />
<button id="prompts" onClick={() => testPrompt(promptResult)}>
Pops up a prompt
</button>
<div id="network_pinger" ref={networkPinger}>
Online
</div>
<button id="ManyClass" className="pure-button another something kala">
Doesn't do anything
</button>
<button className="pure-button">Doesn't do anything</button>
<button className="pure-button">Doesn't do anything</button>
<button className="pure-button">Doesn't do anything</button>
<button className="pure-button">Doesn't do anything</button>
<button id="clickWithOptions" onMouseDown={eventMouseDown} onMouseUp={eventMouseUp}>
Click with Options
</button>
<button style={{ width: '0px', height: '0px', padding: '0px', border: '0px' }} id="no-size"></button>
<button hidden={true} id="hidden-btn">
hidden=true
</button>
<button id="hidden-visibility-btn" style={{ visibility: 'hidden' }}>
visibility hidden
</button>
<button id="hidden-display-btn" style={{ display: 'none' }}>
display none
</button>
<table style={tableStyle}>
<tbody>
<tr>
<td>
<label htmlFor="upload_result">Upload Result:</label>
</td>
<td>
<div id="upload_result" ref={uploadResult} />
</td>
</tr>
<tr>
<td>
<label htmlFor="mouse_delay_time">Mouse Delay:</label>
</td>
<td>
<div id="mouse_delay_time" ref={mouseDelayDiv} />
</td>
</tr>
<tr>
<td>
<label htmlFor="mouse_button">Mouse Button:</label>
</td>
<td>
<div id="mouse_button" ref={mouseButton} />
</td>
</tr>
<tr>
<td>
<label htmlFor="coordinatesX">Coordinates X:</label>
</td>
<td>
<div id="coordinatesX" ref={coordinatesDivX} />
</td>
</tr>
<tr>
<td>
<label htmlFor="coordinatesY">Coordinates Y:</label>
</td>
<td>
<div id="coordinatesY" ref={coordinatesDivY} />
</td>
</tr>
<tr>
<td>
<label htmlFor="click_count">Click Count:</label>
</td>
<td>
<div id="click_count" ref={clickCount} />
</td>
</tr>
<tr>
<td>
<label htmlFor="countKeyPress">Keypresses:</label>
</td>
<td>
<div id="countKeyPress" ref={keypresses} />
</td>
</tr>
<tr>
<td>
<label htmlFor="alt_key">Alt Key:</label>
</td>
<td>
<div id="alt_key" ref={altKey} />
</td>
</tr>
<tr>
<td>
<label htmlFor="ctrl_key">Ctrl Key:</label>
</td>
<td>
<div id="ctrl_key" ref={ctrlKey} />
</td>
</tr>
<tr>
<td>
<label htmlFor="shift_key">Shift Key:</label>
</td>
<td>
<div id="shift_key" ref={shiftKey} />
</td>
</tr>
<tr>
<td>
<label htmlFor="meta_key">Meta Key:</label>
</td>
<td>
<div id="meta_key" ref={metaKey} />
</td>
</tr>
</tbody>
</table>
<label id="pet-select-label">Choose a pet:</label>
<select name="pets" id="pet-select">
<option value="">--Please choose an option--</option>
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="hamster">Hamster</option>
<option value="parrot">Parrot</option>
<option value="spider">Spider</option>
<option value="goldfish">Goldfish</option>
</select>
<br></br>
<input
type="file"
id="file_chooser"
name="file_chooser"
onChange={(event) => fileUploaded(uploadResult, event)}
/>
<input
type="file"
id="multi_file_chooser"
name="multi_file_chooser"
multiple
onChange={(event) => fileUploaded(uploadResult, event)}
/>
<a id="file_download" href="index.js" download="test_download_file">
Download file{' '}
</a>
<ProgressBar />
</>
);
}
function PostSubmit() {
if (username.current == 'demo' && password.current == 'mode') {
document.title = 'Welcome Page';
return (
<>
<h1>Welcome Page</h1>
<p>
Login succeeded. Now you can <a href=".">logout</a>.
</p>
</>
);
} else {
document.title = 'Error Page';
return (
<>
<h1>Error Page</h1>
<p>Login failed. Invalid user name and/or password.</p>
</>
);
}
}
return <Body />;
}