This repository was archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
95 lines (78 loc) · 2.97 KB
/
Copy pathscript.js
File metadata and controls
95 lines (78 loc) · 2.97 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
(function () {
var randomField = document.getElementById('randomField')
var accountNameField = document.getElementById('accountNameField')
var passwordField = document.getElementById('passwordField')
var endpointField = document.getElementById('endpointField')
var disabledLabel = document.querySelectorAll('.disabledLabel')
var loginForm = document.getElementById('loginForm')
var statusContainer = document.querySelector('.status')
var responseContainer = document.querySelector('.response')
var requestedAtContainer = document.querySelector('.requestedAt')
function pad(value) {
if (value < 10) {
return '0' + value
}
return value
}
function getFormattedNow() {
var now = new Date()
return [now.getHours(), now.getMinutes(), now.getSeconds()].map(pad).join(':') + ' ' + [now.getFullYear(), now.getDay(), now.getMonth()].map(pad).join('-')
}
function randomData() {
// dirty hack xD
return Math.random().toString(36).slice(2, 10)
}
function appendResult(response) {
var status = '??? - unknown error';
var responseContent = 'please, check the browser console for more information.'
if (response.responseText) {
status = response.status + ' - ' + response.statusText
responseContent = ''
try {
var obj = JSON.parse(response.responseText)
responseContent += JSON.stringify(obj, null, 2)
} catch (e) {
responseContent += e + "\n"
responseContent += response.responseText
}
}
statusContainer.innerText = status
responseContainer.innerText = responseContent
requestedAtContainer.innerText = getFormattedNow()
}
function sendToServer(url, payload) {
var xhr = new XMLHttpRequest();
xhr.open('POST', url)
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
appendResult(xhr)
}
}
xhr.send(JSON.stringify(payload))
}
loginForm.onsubmit = function (e) {
e.preventDefault()
var payload = {}
payload.accountname = accountNameField.value
payload.password = passwordField.value
payload.type = 'login'
if (randomField.checked) {
payload.accountname = randomData()
payload.password = randomData()
}
var endpoint = endpointField.value
sendToServer(endpoint, payload)
}
randomField.onchange = function (e) {
accountNameField.disabled = e.target.checked
passwordField.disabled = e.target.checked
var disabledText = '';
if (e.target.checked) {
disabledText = '(disabled)'
}
disabledLabel.forEach(function (el) {
el.textContent = disabledText
})
}
})()