-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost.html
More file actions
752 lines (702 loc) · 24.8 KB
/
Copy pathhost.html
File metadata and controls
752 lines (702 loc) · 24.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Castview</title>
<link rel="icon" type="image/png" href="/logo.png">
<style>
:root {
--bg: #0a0c0f;
--card: #12151a;
--card2: #171b21;
--border: #232830;
--text: #f0f2f5;
--muted: #838c9b;
--accent: #f453c1;
--ok: #4ade80;
--accent-soft: rgba(225, 30, 166, 0.09);
--accent-border: rgba(225, 30, 166, 0.25);
--danger: #f87171;
--danger-soft: rgba(248, 113, 113, 0.08);
--danger-border: rgba(248, 113, 113, 0.3);
--mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}
* { box-sizing: border-box; margin: 0; }
body {
min-height: 100vh;
background: var(--bg);
color: var(--text);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
padding: 36px 24px 48px;
line-height: 1.6;
-webkit-font-smoothing: antialiased;
}
.wrap { max-width: 840px; margin: 0 auto; }
/* ---------- header ---------- */
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 34px;
}
.logo {
display: flex;
align-items: center;
gap: 10px;
font-size: 17px;
font-weight: 650;
letter-spacing: -0.01em;
}
.logo svg { color: var(--accent); }
.header-right { display: flex; align-items: center; gap: 18px; }
.live {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
color: var(--ok);
font-weight: 500;
}
.live .dot {
width: 8px; height: 8px; border-radius: 50%;
background: var(--ok);
box-shadow: 0 0 8px var(--ok);
}
.stop-btn {
display: inline-flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 500;
font-family: inherit;
color: var(--danger);
background: var(--danger-soft);
border: 1px solid var(--danger-border);
border-radius: 9px;
padding: 8px 16px;
cursor: pointer;
transition: background .15s;
}
.stop-btn:hover { background: rgba(248, 113, 113, 0.15); }
.shortcut-btn {
display: inline-flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 500;
font-family: inherit;
color: var(--text);
background: var(--card);
border: 1px solid var(--border);
border-radius: 9px;
padding: 8px 16px;
cursor: pointer;
transition: border-color .15s, background .15s;
}
.shortcut-btn:hover { border-color: var(--accent-border); background: var(--card2); }
.shortcut-btn.done { color: var(--accent); border-color: var(--accent-border); cursor: default; }
/* ---------- headline ---------- */
.headline { margin-bottom: 28px; }
.headline h1 {
font-size: clamp(22px, 3.6vw, 28px);
font-weight: 700;
letter-spacing: -0.02em;
line-height: 1.2;
margin-bottom: 6px;
}
.headline p { font-size: 14.5px; color: var(--muted); }
/* ---------- pin ---------- */
.pin-bar {
display: none;
align-items: center;
gap: 18px;
background: var(--card);
border: 1px solid var(--border);
border-radius: 14px;
padding: 16px 22px;
margin-bottom: 18px;
}
.pin-bar .pin {
font-family: var(--mono);
font-size: 30px;
font-weight: 600;
letter-spacing: 10px;
color: var(--accent);
flex-shrink: 0;
padding-right: 4px;
}
.pin-bar .divider { width: 1px; height: 38px; background: var(--border); flex-shrink: 0; }
.pin-bar .txt { font-size: 13px; color: var(--muted); line-height: 1.55; }
.pin-bar .txt b { color: var(--text); font-weight: 600; display: block; font-size: 13.5px; margin-bottom: 1px; }
.warn {
display: none;
background: rgba(251, 191, 36, .07);
border: 1px solid rgba(251, 191, 36, .25);
border-radius: 14px;
padding: 14px 22px;
margin-bottom: 18px;
font-size: 13px;
color: #fbbf24;
line-height: 1.6;
}
/* ---------- cards ---------- */
.cards {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 18px;
}
@media (max-width: 680px) { .cards { grid-template-columns: 1fr; } }
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: 16px;
overflow: hidden;
display: flex;
flex-direction: column;
}
.card-head {
display: flex;
align-items: center;
gap: 12px;
padding: 20px 24px;
border-bottom: 1px solid var(--border);
}
.card-head .icon {
width: 38px; height: 38px;
border-radius: 10px;
background: var(--accent-soft);
border: 1px solid var(--accent-border);
color: var(--accent);
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.card-head h2 { font-size: 15.5px; font-weight: 650; letter-spacing: -0.01em; }
.card-head p { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.card-body { padding: 26px 24px; flex: 1; }
.qr-wrap { display: flex; flex-direction: column; align-items: center; }
.qr-card {
background: #fff;
border-radius: 14px;
padding: 13px;
width: 190px;
margin-bottom: 14px;
animation: pop .3s ease;
}
@keyframes pop { from { opacity: 0; transform: scale(.97); } }
.qr-card svg { display: block; width: 100%; height: auto; }
.url {
font-family: var(--mono);
font-size: 12.5px;
color: var(--accent);
word-break: break-all;
text-align: center;
margin-bottom: 18px;
}
.hint {
display: flex;
gap: 10px;
align-items: flex-start;
font-size: 13px;
color: var(--muted);
line-height: 1.65;
background: var(--card2);
border: 1px solid var(--border);
border-radius: 10px;
padding: 12px 14px;
width: 100%;
}
.hint svg { flex-shrink: 0; margin-top: 2px; color: var(--accent); }
.hint b { color: var(--text); font-weight: 600; }
.device-chip {
display: none;
align-items: center;
gap: 7px;
padding: 5px 12px;
border-radius: 999px;
background: rgba(74, 222, 128, 0.1);
border: 1px solid rgba(74, 222, 128, 0.3);
color: var(--ok);
font-size: 12px;
font-weight: 600;
margin-left: auto;
flex-shrink: 0;
max-width: 45%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.device-chip.show { display: inline-flex; }
.device-chip .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--ok); flex-shrink: 0; }
.steps { display: flex; flex-direction: column; gap: 14px; }
.step { display: flex; gap: 13px; align-items: flex-start; }
.step .n {
width: 24px; height: 24px;
border-radius: 50%;
background: var(--card2);
border: 1px solid var(--border);
color: var(--text);
font-size: 12px;
font-weight: 600;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
margin-top: 2px;
}
.step p { font-size: 13.5px; color: var(--muted); line-height: 1.65; }
.step p b { color: var(--text); font-weight: 600; }
.waiting {
display: flex;
align-items: center;
gap: 10px;
margin-top: 20px;
padding: 12px 14px;
border-radius: 10px;
background: var(--card2);
border: 1px dashed var(--border);
font-size: 12.5px;
color: var(--muted);
}
.waiting .pulse {
width: 8px; height: 8px; border-radius: 50%;
background: var(--accent);
animation: pulse 1.4s ease infinite;
flex-shrink: 0;
}
@keyframes pulse { 50% { opacity: .2; } }
.scan-btn {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 8px 14px;
border-radius: 8px;
background: var(--card2);
border: 1px solid var(--border);
color: var(--text);
font-size: 12.5px;
font-weight: 600;
font-family: inherit;
cursor: pointer;
}
.scan-btn:hover { border-color: var(--accent); }
.scan-btn:disabled { opacity: .6; cursor: default; }
.scan-btn.spin svg { animation: rotate .8s linear infinite; }
@keyframes rotate { to { transform: rotate(360deg); } }
.none { font-size: 13.5px; color: var(--muted); line-height: 1.7; }
footer {
text-align: center;
font-size: 12.5px;
color: var(--muted);
margin-top: 30px;
}
footer .links {
display: flex;
justify-content: center;
align-items: center;
gap: 18px;
margin-top: 12px;
}
footer .links a {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--muted);
text-decoration: none;
}
footer .links a:hover { color: var(--text); }
footer .links svg { flex-shrink: 0; }
/* ---------- low latency bar ---------- */
.ll-bar {
display: none;
align-items: center;
gap: 18px;
background: var(--card);
border: 1px solid var(--border);
border-radius: 14px;
padding: 16px 22px;
margin-bottom: 18px;
}
.ll-bar.show { display: flex; }
.ll-bar .txt { font-size: 13px; color: var(--muted); line-height: 1.55; flex: 1; }
.ll-bar .txt b { color: var(--text); font-weight: 600; display: block; font-size: 13.5px; margin-bottom: 1px; }
.ll-bar .txt b .on-dot {
display: none;
width: 7px; height: 7px;
border-radius: 50%;
background: var(--ok);
margin-right: 7px;
vertical-align: 1px;
}
.ll-bar.active .txt b .on-dot { display: inline-block; }
.ll-btn {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 9px 16px;
border-radius: 8px;
background: var(--accent-soft);
border: 1px solid var(--accent-border);
color: var(--accent);
font-size: 12.5px;
font-weight: 600;
font-family: inherit;
cursor: pointer;
flex-shrink: 0;
}
.ll-bar.active .ll-btn {
background: var(--card2);
border-color: var(--border);
color: var(--muted);
}
/* ---------- stopped overlay ---------- */
#stopped {
position: fixed;
inset: 0;
background: var(--bg);
display: none;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 14px;
z-index: 50;
text-align: center;
padding: 24px;
}
#stopped.show { display: flex; }
#stopped .big-icon {
width: 64px; height: 64px;
border-radius: 50%;
background: var(--card);
border: 1px solid var(--border);
display: flex;
align-items: center;
justify-content: center;
color: var(--muted);
margin-bottom: 8px;
}
#stopped h2 { font-size: 21px; font-weight: 700; letter-spacing: -0.01em; }
#stopped p { font-size: 14px; color: var(--muted); max-width: 380px; line-height: 1.7; }
#stopped code { font-family: var(--mono); font-size: 13px; color: var(--accent); }
</style>
</head>
<body>
<div class="wrap">
<header>
<div class="logo">
<img src="/logo.png" alt="" width="26" height="26" style="border-radius:7px; display:block;">
Castview
</div>
<div class="header-right">
<div class="live"><span class="dot"></span>Live</div>
<button class="shortcut-btn" id="shortcut" style="display:none" title="Put a Castview launcher on your Desktop so you can start sharing with a double-click">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 3v12M12 15l-4-4M12 15l4-4"/>
<path d="M4 19h16"/>
</svg>
<span id="shortcut-lbl">Desktop shortcut</span>
</button>
<button class="stop-btn" id="stop">
<svg width="13" height="13" viewBox="0 0 24 24" fill="currentColor">
<rect x="5" y="5" width="14" height="14" rx="2"/>
</svg>
Stop sharing
</button>
</div>
</header>
<div class="headline">
<h1>This screen is being shared</h1>
<p>Connect another device using either option below.</p>
</div>
<div class="pin-bar" id="pin-bar">
<div class="pin" id="pin-value"></div>
<div class="divider"></div>
<div class="txt">
<b>PIN for this session</b>
Anyone connecting will be asked to type this PIN in their browser. A new PIN is created each time Castview starts.
</div>
</div>
<div class="warn" id="ffmpeg-banner">
Running in basic mode, so the stream will look choppy. Install <b>ffmpeg</b> (ffmpeg.org) and restart Castview for smooth streaming.
</div>
<div class="ll-bar" id="ll-bar">
<div class="txt">
<b><span class="on-dot"></span>Low latency mode</b>
<span id="ll-status">Stream at 30fps with well under half the delay. Uses this tab to capture, so keep it open while it runs.</span>
</div>
<button class="ll-btn" id="ll-btn">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M13 2L3 14h7l-1 8 10-12h-7l1-8z"/>
</svg>
<span id="ll-btn-lbl">Turn on</span>
</button>
</div>
<div class="cards">
<div class="card">
<div class="card-head">
<div class="icon">
<svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round">
<path d="M5 12.5a10 10 0 0 1 14 0M8.5 15.8a5.5 5.5 0 0 1 7 0M12 19h.01"/>
</svg>
</div>
<div>
<h2>WiFi</h2>
<p>Both devices on the same network</p>
</div>
</div>
<div class="card-body" id="wifi-body"><div class="none">Looking for WiFi…</div></div>
</div>
<div class="card">
<div class="card-head">
<div class="icon">
<svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 3v11M12 3l-2.5 2.5M12 3l2.5 2.5"/>
<circle cx="12" cy="17" r="2.2"/>
<path d="M4 17h5.8M14.2 17H20"/>
</svg>
</div>
<div>
<h2>USB cable</h2>
<p>No WiFi needed</p>
</div>
<div class="device-chip" id="usb-chip"><span class="dot"></span><span id="usb-chip-name"></span></div>
</div>
<div class="card-body" id="usb-body"></div>
</div>
</div>
<footer>
People on your network can see this screen while sharing is live.
<div class="links">
<a href="https://github.com/ajithonmain/Castview" target="_blank" rel="noopener">
<svg width="13" height="13" viewBox="0 0 24 24" fill="currentColor">
<path d="M12 .5C5.65.5.5 5.65.5 12c0 5.08 3.29 9.39 7.86 10.91.58.11.79-.25.79-.55 0-.27-.01-1.17-.02-2.12-3.2.7-3.87-1.36-3.87-1.36-.52-1.33-1.28-1.68-1.28-1.68-1.04-.71.08-.7.08-.7 1.15.08 1.76 1.19 1.76 1.19 1.03 1.75 2.69 1.25 3.34.95.1-.74.4-1.25.72-1.53-2.55-.29-5.23-1.28-5.23-5.68 0-1.26.45-2.28 1.19-3.09-.12-.29-.52-1.46.11-3.05 0 0 .97-.31 3.17 1.18a11 11 0 0 1 5.78 0c2.2-1.49 3.17-1.18 3.17-1.18.63 1.59.23 2.76.11 3.05.74.81 1.19 1.83 1.19 3.09 0 4.41-2.69 5.38-5.25 5.66.41.35.77 1.05.77 2.12 0 1.53-.01 2.76-.01 3.14 0 .3.2.67.8.55A11.51 11.51 0 0 0 23.5 12C23.5 5.65 18.35.5 12 .5z"/>
</svg>
Star on GitHub
</a>
<a href="https://cstview.vercel.app" target="_blank" rel="noopener">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/>
</svg>
Website
</a>
<a href="https://ajithmjose.com" target="_blank" rel="noopener">Built by Ajith M Jose</a>
</div>
</footer>
</div>
<div id="stopped">
<div class="big-icon">
<svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">
<rect x="2" y="4" width="20" height="13" rx="2"/>
<path d="M8 21h8M12 17v4M2 4l20 13"/>
</svg>
</div>
<h2>Sharing stopped</h2>
<p>Your screen is no longer visible to anyone. To share again, run <code>npx castview</code> in the terminal. You can close this tab.</p>
</div>
<script>
const wifiBody = document.getElementById('wifi-body');
const usbBody = document.getElementById('usb-body');
let lastKey = '';
let stopped = false;
const CAMERA_HINT = `
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
<path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/><circle cx="12" cy="13" r="4"/>
</svg>
<span>Open the <b>camera</b> on the other device and point it at this code, or type the address into its browser.</span>
`;
function usbSteps(pending) {
const status = pending.length
? `<b>${pending[0].label}</b> is plugged in but has no network address yet. Turn USB tethering off and back on, then scan again`
: 'Waiting for a device. A QR code will appear here on its own';
return `
<div class="steps">
<div class="step"><span class="n">1</span><p>Connect your phone or tablet to this computer with a <b>USB cable</b></p></div>
<div class="step"><span class="n">2</span><p>On the phone, turn on <b>USB tethering</b>. Search "tethering" in its Settings</p></div>
</div>
<div class="waiting"><span class="pulse"></span><span>${status}</span></div>
<button class="scan-btn" id="scan-now">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M21 12a9 9 0 1 1-2.64-6.36M21 3v6h-6"/>
</svg>
Scan now
</button>
`;
}
function qrBlock(i) {
return `
<div class="qr-wrap">
<div class="qr-card">${i.qrSvg}</div>
<div class="url">${i.url}</div>
<div class="hint">${CAMERA_HINT}</div>
</div>
`;
}
async function refresh(fresh) {
if (stopped) return;
try {
const info = await (await fetch(fresh ? '/api/info?fresh=1' : '/api/info')).json();
if (info.pin) {
document.getElementById('pin-value').textContent = info.pin;
document.getElementById('pin-bar').style.display = 'flex';
}
document.getElementById('ffmpeg-banner').style.display = info.ffmpeg ? 'none' : 'block';
// Offer the shortcut button only while the launcher is missing.
if (info.shortcutExists === false && !shortcutBtn.classList.contains('done')) {
shortcutBtn.style.display = 'inline-flex';
} else if (info.shortcutExists) {
shortcutBtn.style.display = 'none';
shortcutBtn.classList.remove('done');
document.getElementById('shortcut-lbl').textContent = 'Desktop shortcut';
}
const pending = info.pending || [];
const key = info.interfaces.map(i => i.kind + i.address).join('|')
+ '~' + pending.map(p => p.name).join('|');
if (key === lastKey) return;
lastKey = key;
const wifi = info.interfaces.filter(i => i.kind === 'wifi');
const usb = info.interfaces.filter(i => i.kind === 'usb');
const wired = info.interfaces.filter(i => i.kind === 'ethernet');
// Wired networks behave like WiFi for the viewer; show them there.
const wifiLike = [...wifi, ...wired];
wifiBody.innerHTML = wifiLike.length
? wifiLike.map(qrBlock).join('<div style="height:24px"></div>')
: '<div class="none">This computer has no WiFi connection. Use the USB option instead.</div>';
usbBody.innerHTML = usb.length
? usb.map(qrBlock).join('<div style="height:24px"></div>')
: usbSteps(pending);
// Connected device name lives in the card header, not above the QR,
// so both cards keep the same height.
document.getElementById('usb-chip').classList.toggle('show', usb.length > 0);
document.getElementById('usb-chip-name').textContent = usb.length ? usb[0].label : '';
} catch {
// server briefly unreachable; keep last known state and retry
}
}
// "Scan now" lives inside re-rendered HTML, so listen on the container.
usbBody.addEventListener('click', async (e) => {
const btn = e.target.closest('#scan-now');
if (!btn || btn.disabled) return;
btn.disabled = true;
btn.classList.add('spin');
lastKey = ''; // force a re-render even if nothing changed
await refresh(true);
const after = document.getElementById('scan-now');
if (after) {
after.classList.remove('spin');
after.disabled = false;
}
});
const shortcutBtn = document.getElementById('shortcut');
shortcutBtn.addEventListener('click', async () => {
if (shortcutBtn.classList.contains('done')) return;
try {
const r = await fetch('/api/shortcut', { method: 'POST' });
if (!(await r.json()).ok) throw new Error();
shortcutBtn.classList.add('done');
document.getElementById('shortcut-lbl').textContent = 'Added to Desktop';
} catch {
document.getElementById('shortcut-lbl').textContent = 'Could not add';
setTimeout(() => { document.getElementById('shortcut-lbl').textContent = 'Desktop shortcut'; }, 2500);
}
});
document.getElementById('stop').addEventListener('click', async () => {
if (!confirm('Stop sharing your screen?')) return;
stopped = true;
try { await fetch('/api/stop', { method: 'POST' }); } catch {}
document.getElementById('stopped').classList.add('show');
});
refresh();
setInterval(refresh, 3000);
// --- low latency mode: capture this screen in-tab, stream WebRTC to viewers ---
// Media goes peer-to-peer over the LAN; the Node server only relays the
// offer/answer/ICE signaling. Only works from this machine (localhost is a
// secure context, so getDisplayMedia is allowed even over plain http).
const llBar = document.getElementById('ll-bar');
const llBtn = document.getElementById('ll-btn');
const llBtnLbl = document.getElementById('ll-btn-lbl');
const llStatus = document.getElementById('ll-status');
const LL_IDLE_STATUS = llStatus.textContent;
let llStream = null;
let llWs = null;
const llPeers = new Map();
if (window.isSecureContext && navigator.mediaDevices && navigator.mediaDevices.getDisplayMedia) {
llBar.classList.add('show');
}
function llViewerCount() {
let n = 0;
for (const pc of llPeers.values()) {
if (pc.connectionState === 'connected') n++;
}
return n;
}
function llUpdateStatus() {
if (!llStream) return;
const n = llViewerCount();
llStatus.textContent = n
? `Active. ${n} viewer${n === 1 ? '' : 's'} watching the low-latency stream.`
: 'Active. Waiting for viewers; they switch over automatically.';
}
async function llCreatePeer(id) {
const old = llPeers.get(id);
if (old) old.close();
const pc = new RTCPeerConnection();
llPeers.set(id, pc);
for (const track of llStream.getTracks()) pc.addTrack(track, llStream);
pc.onicecandidate = (e) => {
if (e.candidate && llWs) llWs.send(JSON.stringify({ type: 'webrtc-ice', to: id, candidate: e.candidate }));
};
pc.onconnectionstatechange = () => {
if (['failed', 'closed', 'disconnected'].includes(pc.connectionState) && llPeers.get(id) === pc) {
llPeers.delete(id);
pc.close();
}
llUpdateStatus();
};
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
llWs.send(JSON.stringify({ type: 'webrtc-offer', to: id, sdp: pc.localDescription }));
}
async function llEnable() {
try {
llStream = await navigator.mediaDevices.getDisplayMedia({
video: { frameRate: 30, displaySurface: 'monitor' },
audio: false,
});
} catch {
return; // user dismissed the picker
}
const track = llStream.getVideoTracks()[0];
track.contentHint = 'detail'; // keep text readable over motion smoothness
track.onended = llDisable; // browser's own "Stop sharing" button
llWs = new WebSocket(`ws://${location.host}/?role=host`);
llWs.onmessage = async (e) => {
const msg = JSON.parse(e.data);
const pc = llPeers.get(msg.from);
if (msg.type === 'webrtc-request') await llCreatePeer(msg.from);
else if (msg.type === 'webrtc-answer' && pc) await pc.setRemoteDescription(msg.sdp).catch(() => {});
else if (msg.type === 'webrtc-ice' && pc) await pc.addIceCandidate(msg.candidate).catch(() => {});
else if (msg.type === 'viewer-gone' && pc) { pc.close(); llPeers.delete(msg.from); llUpdateStatus(); }
};
llWs.onclose = () => { if (llStream) llDisable(); };
llBar.classList.add('active');
llBtnLbl.textContent = 'Turn off';
llUpdateStatus();
}
function llDisable() {
if (llStream) for (const t of llStream.getTracks()) t.stop();
llStream = null;
for (const pc of llPeers.values()) pc.close();
llPeers.clear();
if (llWs) { llWs.onclose = null; llWs.close(); llWs = null; }
llBar.classList.remove('active');
llBtnLbl.textContent = 'Turn on';
llStatus.textContent = LL_IDLE_STATUS;
}
llBtn.addEventListener('click', () => (llStream ? llDisable() : llEnable()));
</script>
</body>
</html>