Repository navigation
Expand file tree
/
Copy pathpulse.html
More file actions
929 lines (830 loc) · 26 KB
/
Copy pathpulse.html
File metadata and controls
929 lines (830 loc) · 26 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SPEED TEST — Diagnostic Report</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@500;700&family=JetBrains+Mono:wght@400;500;600;700&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<style>
:root{
--backdrop: #12181c;
--backdrop-2: #0d1215;
--paper: #f3ecdd;
--paper-edge: #e7ddc6;
--ink: #1b1f1a;
--ink-dim: #8a8272;
--rule: #c9c0a8;
--stamp-red: #b0402a;
--ink-teal: #2f5d50;
}
*{ box-sizing: border-box; }
html, body{
margin:0; padding:0;
min-height: 100vh;
background: var(--backdrop);
background-image:
radial-gradient(circle, rgba(255,255,255,0.035) 1px, transparent 1.4px);
background-size: 22px 22px;
color: var(--paper);
font-family: 'Space Grotesk', sans-serif;
}
.page{
max-width: 460px;
margin: 0 auto;
padding: 16px 20px 24px;
display:flex;
flex-direction: column;
align-items:center;
box-sizing: border-box;
}
.page-eyebrow{
font-family: 'JetBrains Mono', monospace;
font-size: 11px;
letter-spacing: 0.22em;
text-transform: uppercase;
color: #5b6670;
margin: 0 0 26px;
text-align:center;
}
/* ---------- Ticket card ---------- */
.ticket{
position: relative;
width: 100%;
background: var(--paper);
color: var(--ink);
box-shadow: 0 24px 50px -18px rgba(0,0,0,0.65);
border-radius: 3px;
}
.perf{
height: 16px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 16px;
}
.perf.top{ margin-bottom: -1px; }
.perf.bottom{ margin-top: -1px; }
.perf-dot{
flex: 0 0 auto;
width: 13px;
height: 13px;
border-radius: 50%;
background: var(--backdrop);
}
.ticket-body{
padding: 6px 30px 26px;
}
.masthead{
display:flex;
align-items: flex-end;
justify-content: space-between;
padding: 14px 0 12px;
}
.masthead .brand{
font-family: 'Space Grotesk', sans-serif;
font-weight: 700;
font-size: 17px;
letter-spacing: 0.01em;
line-height: 1;
}
.run-info{
display:flex;
flex-direction: column;
align-items: flex-end;
gap: 5px;
}
.mini-barcode{
display: flex;
align-items: stretch;
justify-content: flex-end;
gap: 1.5px;
height: 18px;
}
.mini-barcode span{
display: block;
height: 100%;
background: var(--ink);
opacity: 0.85;
}
.masthead .run-no{
font-family: 'JetBrains Mono', monospace;
font-size: 9.5px;
color: var(--ink-dim);
text-align:right;
}
.rule{
border: none;
border-top: 1.5px dashed var(--rule);
margin: 0;
}
.meta-row{
display:flex;
justify-content: space-between;
font-family: 'JetBrains Mono', monospace;
font-size: 10.5px;
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--ink-dim);
padding: 10px 0 18px;
}
/* ---------- Main reading ---------- */
.reading{
text-align:center;
padding: 8px 0 22px;
position: relative;
}
.reading .label{
font-family: 'JetBrains Mono', monospace;
font-size: 11px;
letter-spacing: 0.2em;
text-transform: uppercase;
color: var(--ink-dim);
margin: 12px 0 0;
}
.reading .number{
display:flex;
align-items: baseline;
justify-content:center;
gap: 3px;
}
.reading .number .value{
font-family: 'JetBrains Mono', monospace;
font-weight: 700;
font-size: 76px;
line-height: 1;
letter-spacing: -0.02em;
color: var(--stamp-red);
}
.reading .number .unit{
font-family: 'Space Grotesk', sans-serif;
font-weight: 700;
font-size: 22px;
color: var(--ink);
}
.stamp{
position: absolute;
top: -14px;
right: -6px;
width: 76px;
height: 76px;
border-radius: 50%;
border: 2px solid var(--ink-teal);
outline: 1px solid var(--ink-teal);
outline-offset: 4px;
display: none;
flex-direction: column;
align-items:center;
justify-content:center;
transform: rotate(-10deg);
color: var(--ink-teal);
font-family: 'JetBrains Mono', monospace;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-align:center;
line-height: 1.3;
}
.stamp.show{ display:flex; }
.stamp .stamp-emoji{
display: block;
font-size: 16px;
line-height: 1;
margin-bottom: 3px;
}
/* ---------- Detail table ---------- */
.details{
display:grid;
grid-template-columns: 1fr 1fr;
gap: 0 20px;
padding: 20px 0 6px;
}
.detail-block .heading{
font-family: 'Space Grotesk', sans-serif;
font-weight: 700;
font-size: 14px;
margin: 0 0 12px;
text-transform: uppercase;
letter-spacing: 0.03em;
}
.detail-block.latency .heading{ color: var(--ink-teal); }
.detail-block.upload .heading{ color: var(--stamp-red); }
.detail-line{
display:flex;
justify-content: space-between;
align-items: baseline;
padding: 5px 0;
font-family: 'JetBrains Mono', monospace;
}
.detail-line .k{
font-size: 10.5px;
text-transform: uppercase;
letter-spacing: 0.06em;
color: var(--ink-dim);
}
.detail-line .v{
font-size: 15px;
font-weight: 600;
color: var(--ink);
}
.detail-line .v .u{
font-size: 10px;
color: var(--ink-dim);
font-weight: 400;
margin-left: 3px;
}
/* ---------- Client / network / server info ---------- */
.client-info{
display:flex;
flex-direction: column;
gap: 9px;
padding: 18px 0 4px;
}
.info-line{
display:flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
font-family: 'JetBrains Mono', monospace;
}
.info-line .k{
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.08em;
color: var(--ink-dim);
white-space: nowrap;
padding-top: 1px;
}
.info-line .v{
font-size: 11px;
color: var(--ink);
text-align: right;
white-space: normal;
line-height: 1.45;
max-width: 68%;
transition: opacity .25s ease;
}
.info-line .v.loading{
opacity: 0.55;
font-style: italic;
}
/* ---------- Status line + button ---------- */
.status-line{
font-family: 'JetBrains Mono', monospace;
font-size: 12px;
color: #6a7580;
text-align:center;
min-height: 18px;
margin: 20px 0 14px;
}
.button-row{
display:flex;
align-items:center;
justify-content:center;
gap: 12px;
margin-top: 20px;
}
button#runBtn{
font-family: 'JetBrains Mono', monospace;
font-size: 12.5px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
color: var(--paper);
background: var(--stamp-red);
border: none;
padding: 14px 28px;
cursor: pointer;
border-radius: 12px;
box-shadow: 0 4px 0 #7a2c1c;
}
button#runBtn:disabled{
background: #4a4438;
color: #8a8272;
box-shadow: 0 4px 0 #33302a;
cursor: default;
}
button#runBtn:active:not(:disabled){
transform: translateY(2px);
box-shadow: 0 2px 0 #7a2c1c;
}
button#runBtn:focus-visible{
outline: 2px solid var(--paper);
outline-offset: 3px;
}
button#shareBtn{
font-family: 'JetBrains Mono', monospace;
font-size: 11.5px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
color: var(--ink-teal);
background: var(--paper);
border: 1.5px solid var(--ink-teal);
padding: 12px 22px;
cursor: pointer;
border-radius: 12px;
box-shadow: 0 4px 0 rgba(47,93,80,0.35);
display: none;
}
button#shareBtn.show{ display: inline-block; }
button#shareBtn:active:not(:disabled){
transform: translateY(2px);
box-shadow: 0 2px 0 rgba(47,93,80,0.35);
}
button#shareBtn:disabled{ opacity: 0.5; cursor: default; }
button#shareBtn:focus-visible{
outline: 2px solid var(--paper);
outline-offset: 3px;
}
.site-footer{
margin-top: 130px;
width: 100%;
box-sizing: border-box;
padding: 26px 20px;
margin-left: -20px;
margin-right: -20px;
width: calc(100% + 40px);
background: var(--backdrop-2);
display:flex;
flex-direction: column;
align-items:center;
}
.page-footer{
text-align: justify;
font-family: 'JetBrains Mono', monospace;
font-size: 10.5px;
color: #4a545e;
line-height: 1.7;
max-width: 340px;
}
.footer-links{
display:flex;
align-items:center;
gap: 8px;
margin-top: 16px;
font-family: 'JetBrains Mono', monospace;
font-size: 10.5px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.footer-links a{
color: #6a7580;
text-decoration: none;
}
.footer-links a:hover{ color: var(--paper); }
.footer-dot{ color: #3a4550; }
@media (max-width: 380px){
.reading .number .value{ font-size: 58px; }
.ticket-body{ padding: 6px 20px 22px; }
.details{ gap: 0 12px; }
}
</style>
</head>
<body>
<div class="page">
<script>
atOptions = {
'key' : 'f3119253d94c70f5000329faaa950bdc',
'format' : 'iframe',
'height' : 50,
'width' : 320,
'params' : {}
};
</script>
<script src="https://www.highperformanceformat.com/f3119253d94c70f5000329faaa950bdc/invoke.js"></script>
<div style="height:16px;"></div>
<div class="ticket">
<div class="perf top" id="perfTop"></div>
<div class="ticket-body">
<div class="masthead">
<span class="brand">SPEED TEST</span>
<div class="run-info">
<span class="run-no" id="runNo">REF · 00000000</span>
<div class="mini-barcode" id="miniBarcode"></div>
</div>
</div>
<hr class="rule">
<div class="meta-row">
<span id="metaDate">—</span>
<span>DIAGNOSTIC REPORT</span>
</div>
<div class="reading">
<div class="number">
<span class="value" id="mainValue">—</span>
<span class="unit">Mbps</span>
</div>
<div class="stamp" id="stamp"><span class="stamp-emoji">✅</span>VERIFIED<br>RESULT</div>
</div>
<hr class="rule">
<div class="details">
<div class="detail-block upload">
<p class="heading">Upload</p>
<div class="detail-line">
<span class="k">Speed</span>
<span class="v" id="upSpeed">—<span class="u">Mbps</span></span>
</div>
</div>
<div class="detail-block latency">
<p class="heading">Latency</p>
<div class="detail-line">
<span class="k">Ping</span>
<span class="v" id="pingValue">—<span class="u">ms</span></span>
</div>
</div>
</div>
<hr class="rule">
<div class="client-info">
<div class="info-line">
<span class="k">Client</span>
<span class="v" id="clientInfo">—</span>
</div>
<div class="info-line">
<span class="k">Network</span>
<span class="v" id="networkInfo">—</span>
</div>
<div class="info-line">
<span class="k">Server</span>
<span class="v" id="serverInfo">—</span>
</div>
<div class="info-line">
<span class="k">Duration</span>
<span class="v" id="durationInfo">—</span>
</div>
<div class="info-line">
<span class="k">Device</span>
<span class="v" id="deviceInfo">—</span>
</div>
</div>
</div>
<div class="perf bottom" id="perfBottom"></div>
</div>
<div class="button-row">
<button id="runBtn">Run test</button>
<button id="shareBtn">Share</button>
</div>
<footer class="site-footer">
<p class="page-footer">
Measured live against your network path. Wi-Fi, VPNs, and busy networks affect results.
Uses Cloudflare's public speed-test endpoints for download/upload sampling.
</p>
<nav class="footer-links">
<a href="#">About</a>
<span class="footer-dot">·</span>
<a href="#">Contact</a>
<span class="footer-dot">·</span>
<a href="#">Policy</a>
</nav>
</footer>
</div>
<script>
(function(){
const runBtn = document.getElementById('runBtn');
const shareBtn = document.getElementById('shareBtn');
const ticketEl = document.querySelector('.ticket');
const statusLine = document.getElementById('statusLine');
const mainValue = document.getElementById('mainValue');
const pingValue = document.getElementById('pingValue');
const upSpeed = document.getElementById('upSpeed');
const runNo = document.getElementById('runNo');
const metaDate = document.getElementById('metaDate');
const stamp = document.getElementById('stamp');
const clientInfo = document.getElementById('clientInfo');
const networkInfo = document.getElementById('networkInfo');
const serverInfo = document.getElementById('serverInfo');
const durationInfo = document.getElementById('durationInfo');
const deviceInfo = document.getElementById('deviceInfo');
// ---------- Perforation dots & barcode (real DOM nodes so screenshots render them) ----------
function renderDots(el, count = 16){
el.innerHTML = '';
for (let i = 0; i < count; i++){
const dot = document.createElement('span');
dot.className = 'perf-dot';
el.appendChild(dot);
}
}
renderDots(document.getElementById('perfTop'));
renderDots(document.getElementById('perfBottom'));
function renderBarcode(el, count = 26){
el.innerHTML = '';
const widths = [2, 1, 3, 1, 2, 4, 1, 2, 3, 1];
for (let i = 0; i < count; i++){
const bar = document.createElement('span');
bar.style.width = widths[i % widths.length] + 'px';
el.appendChild(bar);
}
}
renderBarcode(document.getElementById('miniBarcode'));
function stampMeta(){
const now = new Date();
const opts = { day: '2-digit', month: 'short', year: 'numeric' };
const dateStr = now.toLocaleDateString('en-GB', opts).toUpperCase();
const timeStr = now.toTimeString().slice(0,5);
metaDate.textContent = `${dateStr} · ${timeStr}`;
const rand8 = () => Math.floor(10000000 + Math.random()*90000000);
runNo.textContent = `REF · ${rand8()}`;
}
stampMeta();
// ---------- Device / browser signature ----------
function detectDevice(){
const ua = navigator.userAgent;
let browser = 'Unknown browser';
if (/Edg\//.test(ua)) browser = 'Edge';
else if (/OPR\//.test(ua)) browser = 'Opera';
else if (/Chrome\//.test(ua) && !/Chromium/.test(ua)) browser = 'Chrome';
else if (/Firefox\//.test(ua)) browser = 'Firefox';
else if (/Safari\//.test(ua) && !/Chrome/.test(ua)) browser = 'Safari';
let os = 'Unknown OS';
if (/Android/.test(ua)) os = 'Android';
else if (/iPhone|iPad|iPod/.test(ua)) os = 'iOS';
else if (/Windows/.test(ua)) os = 'Windows';
else if (/Mac OS X/.test(ua)) os = 'macOS';
else if (/Linux/.test(ua)) os = 'Linux';
return `${browser} · ${os}`;
}
const deviceSignature = detectDevice();
// ---------- Speed grade ----------
function gradeMeta(mbps){
if (mbps >= 40) return { label: 'EXCELLENT', emoji: '🚀', color: '#2f5d50' };
if (mbps >= 15) return { label: 'GOOD', emoji: '✅', color: '#2f5d50' };
if (mbps >= 3) return { label: 'FAIR', emoji: '⚠️', color: '#a97a2b' };
return { label: 'POOR', emoji: '🐢', color: '#b0402a' };
}
function getConnectionType(){
const conn = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
if (!conn) return null;
if (conn.type) return conn.type.replace(/_/g, ' ').toUpperCase();
if (conn.effectiveType) return conn.effectiveType.toUpperCase();
return null;
}
// ---------- Client IP / ISP / location lookup ----------
function fetchWithTimeout(url, ms = 5000){
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), ms);
return fetch(url, { signal: controller.signal }).finally(() => clearTimeout(id));
}
let clientText = null;
let networkText = null;
async function loadClientInfo(){
const providers = [
async () => {
const r = await fetchWithTimeout('https://ipwho.is/');
const d = await r.json();
if (d.success === false) throw new Error('provider failed');
return {
ip: d.ip,
city: d.city,
country: d.country_code,
isp: (d.connection && (d.connection.isp || d.connection.org)) || null
};
},
async () => {
const r = await fetchWithTimeout('https://ipapi.co/json/');
const d = await r.json();
if (d.error) throw new Error('provider failed');
return { ip: d.ip, city: d.city, country: d.country_code, isp: d.org };
}
];
for (const provider of providers){
try{
const data = await provider();
const cityCountry = [data.city, data.country].filter(Boolean).join(', ');
clientText = [cityCountry, data.ip].filter(Boolean).join(' · ') || 'unavailable';
const isp = data.isp || 'Unknown ISP';
const connType = getConnectionType();
networkText = connType ? `${isp} · ${connType}` : isp;
return;
}catch(e){
// try next provider
}
}
clientText = 'unavailable';
networkText = 'unavailable';
}
// client/network data is fetched quietly in the background; only shown once the step is reached
loadClientInfo();
function sleep(ms){
return new Promise(resolve => setTimeout(resolve, ms));
}
// shows "Loading…" for a beat, then swaps in the final value
async function loadStep(el, finalValue, loadingMs = 500){
el.textContent = 'Loading…';
el.classList.add('loading');
await sleep(loadingMs);
el.textContent = finalValue;
el.classList.remove('loading');
}
// ---------- Test server (Cloudflare colo) lookup ----------
const COLO_MAP = {
SIN: 'Singapore, SG', BOM: 'Mumbai, IN', MAA: 'Chennai, IN', CCU: 'Kolkata, IN',
DEL: 'Delhi, IN', DAC: 'Dhaka, BD', HKG: 'Hong Kong, HK', KUL: 'Kuala Lumpur, MY',
BKK: 'Bangkok, TH', NRT: 'Tokyo, JP', ICN: 'Seoul, KR', LHR: 'London, UK',
FRA: 'Frankfurt, DE', SEA: 'Seattle, US', LAX: 'Los Angeles, US', SJC: 'San Jose, US'
};
let capturedColo = null;
function coloLabel(code){
return COLO_MAP[code] ? `${COLO_MAP[code]} (${code})` : code;
}
const DOWN_URL = (bytes) => `https://speed.cloudflare.com/__down?bytes=${bytes}&t=${Date.now()}-${Math.random().toString(16).slice(2)}`;
const UP_URL = () => `https://speed.cloudflare.com/__up?t=${Date.now()}-${Math.random().toString(16).slice(2)}`;
function setVal(el, num, unit){
el.innerHTML = num.toFixed(unit === 'ms' ? 0 : 1) + '<span class="u">' + unit + '</span>';
}
// ---------- Live jitter while a value is being measured ----------
const activeJitters = new Set();
function randomBetween(min, max){
return min + Math.random() * (max - min);
}
function startJitterPlain(el, min, max, decimals){
const id = setInterval(() => {
el.textContent = randomBetween(min, max).toFixed(decimals);
}, 90);
activeJitters.add(id);
return id;
}
function startJitterUnit(el, min, max, decimals, unit){
const id = setInterval(() => {
el.innerHTML = randomBetween(min, max).toFixed(decimals) + '<span class="u">' + unit + '</span>';
}, 90);
activeJitters.add(id);
return id;
}
function stopJitter(id){
clearInterval(id);
activeJitters.delete(id);
}
function stopAllJitters(){
activeJitters.forEach(id => clearInterval(id));
activeJitters.clear();
}
async function measurePing(sampleCount = 6){
const times = [];
for(let i=0;i<sampleCount;i++){
const start = performance.now();
try{
const resp = await fetch(DOWN_URL(0), { cache: 'no-store' });
if (!capturedColo){
const ray = resp.headers.get('cf-ray');
if (ray && ray.includes('-')) capturedColo = ray.split('-').pop();
}
}catch(e){}
const dur = performance.now() - start;
if (i > 0) times.push(dur);
}
return times.reduce((a,b)=>a+b,0) / times.length;
}
async function measureDownload(){
const sizes = [1000000, 3000000, 8000000];
let totalBytes = 0, totalTime = 0;
for (const size of sizes){
const start = performance.now();
let resp;
try{ resp = await fetch(DOWN_URL(size), { cache: 'no-store' }); }catch(e){ continue; }
const reader = resp.body.getReader();
let received = 0;
while(true){
const { done, value } = await reader.read();
if (done) break;
received += value.length;
}
totalBytes += received;
totalTime += (performance.now() - start);
}
return (totalBytes * 8) / (totalTime/1000) / 1e6;
}
function measureUpload(){
return new Promise((resolve) => {
const size = 6000000;
const data = new Blob([new Uint8Array(size)]);
const xhr = new XMLHttpRequest();
const start = performance.now();
xhr.open('POST', UP_URL());
xhr.onload = () => {
const totalTime = (performance.now() - start) / 1000;
resolve((size * 8) / totalTime / 1e6);
};
xhr.onerror = () => resolve(null);
xhr.ontimeout = () => resolve(null);
xhr.timeout = 20000;
xhr.send(data);
});
}
async function runTest(){
runBtn.disabled = true;
shareBtn.classList.remove('show');
stamp.classList.remove('show');
stampMeta();
if (statusLine) statusLine.textContent = 'printing download…';
mainValue.textContent = '—';
pingValue.innerHTML = '—<span class="u">ms</span>';
upSpeed.innerHTML = '—<span class="u">Mbps</span>';
clientInfo.textContent = '—';
networkInfo.textContent = '—';
serverInfo.textContent = '—';
durationInfo.textContent = '—';
deviceInfo.textContent = '—';
const testStart = performance.now();
try{
const jDown = startJitterPlain(mainValue, 5, 120, 0);
const download = await measureDownload();
stopJitter(jDown);
mainValue.textContent = download.toFixed(0);
if (statusLine) statusLine.textContent = 'printing upload…';
const jUp = startJitterUnit(upSpeed, 1, 60, 1, 'Mbps');
const upload = await measureUpload();
stopJitter(jUp);
if (upload != null) setVal(upSpeed, upload, 'Mbps');
else upSpeed.innerHTML = 'n/a';
if (statusLine) statusLine.textContent = 'printing latency…';
const jPing = startJitterUnit(pingValue, 4, 95, 0, 'ms');
const ping = await measurePing();
stopJitter(jPing);
setVal(pingValue, ping, 'ms');
const seconds = (performance.now() - testStart) / 1000;
if (statusLine) statusLine.textContent = 'reading client info…';
await loadStep(clientInfo, clientText || 'unavailable');
if (statusLine) statusLine.textContent = 'reading network info…';
await loadStep(networkInfo, networkText || 'unavailable');
if (statusLine) statusLine.textContent = 'locating server…';
const serverText = capturedColo ? coloLabel(capturedColo) : 'Cloudflare edge (not exposed by browser)';
await loadStep(serverInfo, serverText);
if (statusLine) statusLine.textContent = 'calculating duration…';
await loadStep(durationInfo, seconds.toFixed(1) + 's');
if (statusLine) statusLine.textContent = 'reading device info…';
await loadStep(deviceInfo, deviceSignature);
if (statusLine) statusLine.textContent = 'report complete';
const g = gradeMeta(download);
stamp.style.borderColor = g.color;
stamp.style.color = g.color;
stamp.innerHTML = `<span class="stamp-emoji">${g.emoji}</span>${g.label}<br>RESULT`;
stamp.classList.add('show');
shareBtn.classList.add('show');
}catch(err){
if (statusLine) statusLine.textContent = 'something went wrong — try again';
console.error(err);
}finally{
stopAllJitters();
runBtn.disabled = false;
runBtn.textContent = 'Run again';
}
}
runBtn.addEventListener('click', runTest);
// ---------- Share result as an image ----------
async function shareResult(){
if (typeof html2canvas === 'undefined'){
alert('Image generator failed to load — check your connection and try again.');
return;
}
shareBtn.disabled = true;
const originalText = shareBtn.textContent;
shareBtn.textContent = 'Preparing…';
try{
const canvas = await html2canvas(ticketEl, {
backgroundColor: '#f3ecdd',
scale: 2,
useCORS: true
});
canvas.toBlob(async (blob) => {
if (!blob){
shareBtn.textContent = originalText;
shareBtn.disabled = false;
return;
}
const file = new File([blob], 'pulse-speed-test.png', { type: 'image/png' });
try{
if (navigator.canShare && navigator.canShare({ files: [file] })){
await navigator.share({
files: [file],
title: 'SPEED TEST — Connection Diagnostic Report',
text: 'My internet speed test result'
});
} else {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'pulse-speed-test.png';
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
}catch(shareErr){
// user cancelled share sheet or share failed silently — no action needed
}finally{
shareBtn.textContent = originalText;
shareBtn.disabled = false;
}
}, 'image/png');
}catch(err){
console.error(err);
shareBtn.textContent = originalText;
shareBtn.disabled = false;
}
}
shareBtn.addEventListener('click', shareResult);
})();
</script>
</body>
</html>