-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
193 lines (167 loc) · 4.57 KB
/
index.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
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
const defaultPortals = [
'https://siasky.net/',
'https://sialoop.net/',
// 'https://skynet.luxor.tech/',
'https://skynet.tutemwesi.com/',
'https://siacdn.com/',
'https://vault.lightspeedhosting.com/',
'https://skydrain.net/'
]
const enabledPortals = {}
let running = 0;
let ms = 0;
let s = 0;
let m = 0;
let interval;
let timeout = 10;
let runid;
let finished = 0;
let racing = 0;
onClickRace = (evt) => {
const skylink = document.getElementById("skylink").value
if (!skylink.startsWith("sia://")) {
alert(`That is not a valid skylink, a skylink looks like this: sia://AABP6CorLeC6Upp-DrtZAlKFla_Ip2qC0PUI_qszy_RaRQ`)
return;
}
if (running) {
alert('already running')
return
}
running = true
runid = new Date().getTime()
document.getElementById("race").disabled = true
document.getElementById("reset").disabled = false
const skylinkStripped = skylink.slice("sia://")
console.log(`Racing ${skylinkStripped}`)
interval = setInterval(timer, timeout)
for (const [portal, enabled] of Object.entries(enabledPortals)) {
if (enabled) {
race(portal, skylinkStripped)
}
}
}
onClickReset = () => {
runid = 0
finished = 0;
racing = 0;
clearInterval(interval)
document.getElementById("skylink").value = ""
document.getElementById("timer").innerHTML = ""
document.getElementById("laps").innerHTML = ""
document.getElementById("race").disabled = false
document.getElementById("reset").disabled = true
running = false
}
onChangePortal = (cb) => {
console.log(cb.id)
if (enabledPortals[cb.id]) {
cb.checked = false
enabledPortals[cb.id] = false
} else {
cb.checked = true
enabledPortals[cb.id] = true
}
}
lap = (id, portal) => {
if (id != runid) {
return // old run
}
finished++
const lap = document.createElement('tr')
lap.className = "lap"
const pos = document.createElement('td')
pos.className = "lap-pos"
pos.innerHTML = `#${finished}`
lap.appendChild(pos)
const name = document.createElement('td')
name.className = "lap-name"
name.innerHTML = portal
lap.appendChild(name)
const ttfb = document.createElement('td')
ttfb.className = "lap-ttfb"
ttfb.innerHTML = time()
lap.appendChild(ttfb)
document.getElementById("laps").appendChild(lap)
if (finished == racing) {
clearInterval(interval)
}
}
fail = (id, portal) => {
if (id != runid) {
return // old run
}
finished++
console.log('FAIL', portal)
const lap = document.createElement('tr')
lap.className = "lap failed"
const pos = document.createElement('td')
pos.className = "lap-pos"
lap.appendChild(pos)
const name = document.createElement('td')
name.className = "lap-name"
name.innerHTML = portal
lap.appendChild(name)
const ttfb = document.createElement('td')
ttfb.className = "lap-ttfb"
ttfb.innerHTML = time()
lap.appendChild(ttfb)
document.getElementById("laps").appendChild(lap)
if (finished == racing) {
clearInterval(interval)
}
}
race = (portal, skylink) => {
const id = runid
const url = `${portal}${skylink}`
racing++
console.log(`Racing ${url}`)
fetch(url, {
withCredentials: true,
mode: 'no-cors'
})
.then((resp) => {
console.log(resp)
console.log(resp.status)
lap(id, portal)
})
.catch(() => {
fail(id, portal)
})
}
time = () => {
const mst = ms / timeout
const mins = m < 10 ? `0${m}` : m
const secs = s < 10 ? `0${s}` : s
const msec = mst < 10 ? `0${mst}` : mst
return `${mins}:${secs}:${msec}`
}
timer = () => {
document.getElementById("timer").innerHTML = time();
ms += timeout;
if (ms == 1000) {
s += 1;
ms = 0;
}
if (s == 60) {
m += 1;
s = 0;
}
}
initUI = () => {
let html = ''
for (const portal of defaultPortals) {
html += `
<input type="checkbox" id="${portal}" name="${portal}" value="${portal}" checked onchange="onChangePortal(this)">
<label for="${portal}">${portal}</label>
<br>
`
enabledPortals[portal] = true
}
document.getElementById("portals").innerHTML = html
}
window.addEventListener('DOMContentLoaded', (event) => {
document.getElementById("race").onclick = onClickRace
document.getElementById("reset").onclick = onClickReset
document.getElementById("reset").disabled = true
initUI()
})