-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
993 lines (869 loc) · 33.2 KB
/
Copy pathindex.html
File metadata and controls
993 lines (869 loc) · 33.2 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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FloatyJS Host Demo</title>
<script type="module" src="./floaty-main.js" data-theme-color="#111827" data-button-text="AI"
data-tooltip-text="点击这里打开 AI 浮窗哦~" data-position="bottom-right"
data-close-on-outside-click="true" data-target-link="./iframe.html" data-window-height="800px"
data-window-width="1000px" data-window-title="FloatyJS Main"
data-page-info="这是 FloatyJS 的宿主页演示台,页面同时包含交互沙箱与 README 文档区域。" defer></script>
<script src="https://cdn.jsdelivr.net/npm/markdown-it@14.1.0/dist/markdown-it.min.js"></script>
<style>
:root {
--bg: #f8fafc;
--surface: rgba(255, 255, 255, 0.9);
--surface-strong: #ffffff;
--border: rgba(15, 23, 42, 0.08);
--text: #0f172a;
--muted: #475569;
--brand: #0f172a;
--brand-2: #2563eb;
--success: #166534;
--danger: #b91c1c;
--shadow: 0 24px 50px rgba(15, 23, 42, 0.12);
--radius: 24px;
}
* {
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
body {
margin: 0;
min-height: 100vh;
color: var(--text);
font-family: "Segoe UI", Arial, sans-serif;
background:
radial-gradient(circle at top left, #dbeafe 0, transparent 28%),
radial-gradient(circle at top right, #e9d5ff 0, transparent 24%),
linear-gradient(180deg, #f8fbff 0%, #f8fafc 100%);
}
.page {
width: min(1520px, calc(100% - 40px));
margin: 0 auto;
padding: 28px 0 48px;
}
.hero {
display: grid;
grid-template-columns: 1.15fr 0.85fr;
gap: 18px;
margin-bottom: 18px;
}
.panel {
border: 1px solid var(--border);
border-radius: var(--radius);
background: var(--surface);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
.hero-main {
padding: 28px;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-radius: 999px;
background: rgba(37, 99, 235, 0.08);
color: var(--brand-2);
font-weight: 700;
font-size: 12px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
h1 {
margin: 18px 0 12px;
font-size: clamp(34px, 4vw, 54px);
line-height: 1;
}
.hero p {
margin: 0;
max-width: 64ch;
color: var(--muted);
line-height: 1.7;
font-size: 16px;
}
.hero-points {
margin-top: 18px;
display: grid;
gap: 10px;
}
.point {
padding: 12px 14px;
border-radius: 16px;
background: rgba(15, 23, 42, 0.04);
color: var(--muted);
line-height: 1.55;
font-size: 14px;
}
.hero-side {
padding: 22px;
display: grid;
gap: 14px;
align-content: start;
}
.status-pill {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-radius: 999px;
background: rgba(22, 101, 52, 0.12);
color: var(--success);
font-size: 13px;
font-weight: 700;
}
.status-pill[data-status="waiting"] {
background: rgba(37, 99, 235, 0.12);
color: var(--brand-2);
}
.status-pill[data-status="error"] {
background: rgba(185, 28, 28, 0.12);
color: var(--danger);
}
.status-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
}
.metric {
padding: 14px;
border-radius: 18px;
background: rgba(15, 23, 42, 0.04);
}
.metric-label {
display: block;
margin-bottom: 8px;
color: var(--muted);
font-size: 12px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.metric-value {
font-size: 18px;
font-weight: 700;
word-break: break-word;
}
.grid {
display: grid;
grid-template-columns: 360px minmax(0, 1fr) 360px;
gap: 18px;
margin-bottom: 18px;
}
.controls,
.results,
.sandbox {
padding: 22px;
}
.section-title {
margin: 0 0 14px;
font-size: 18px;
}
.subtle {
margin: 0 0 16px;
color: var(--muted);
line-height: 1.6;
font-size: 14px;
}
.field {
display: grid;
gap: 8px;
margin-bottom: 14px;
}
.field label {
color: var(--muted);
font-size: 13px;
font-weight: 700;
}
.range-wrap {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: center;
}
input[type="range"] {
width: 100%;
}
.depth-badge {
min-width: 62px;
padding: 7px 10px;
border-radius: 999px;
background: rgba(15, 23, 42, 0.08);
text-align: center;
font-weight: 700;
}
.check-row {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 12px;
border-radius: 14px;
background: rgba(15, 23, 42, 0.04);
margin-bottom: 10px;
}
.button-grid {
display: grid;
gap: 10px;
margin-top: 16px;
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
button,
input,
textarea,
select {
font: inherit;
}
button {
border: none;
border-radius: 16px;
padding: 12px 14px;
background: var(--brand);
color: #fff;
font-size: 14px;
font-weight: 700;
cursor: pointer;
transition: transform 0.12s ease, filter 0.2s ease;
}
button:hover {
filter: brightness(1.08);
}
button:active {
transform: scale(0.98);
}
button.secondary {
background: #e2e8f0;
color: var(--text);
}
button.ghost {
background: rgba(15, 23, 42, 0.06);
color: var(--text);
}
.results-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 14px;
}
.result-meta {
display: flex;
flex-wrap: wrap;
gap: 10px;
color: var(--muted);
font-size: 13px;
}
.code-card {
min-height: 380px;
border-radius: 20px;
background: #0f172a;
color: #e2e8f0;
overflow: hidden;
}
pre {
margin: 0;
padding: 18px;
overflow: auto;
white-space: pre-wrap;
word-break: break-word;
font-size: 13px;
line-height: 1.6;
}
.preview-shell {
margin-top: 16px;
display: grid;
gap: 10px;
}
.preview-frame {
min-height: 220px;
border-radius: 20px;
overflow: hidden;
border: 1px dashed rgba(15, 23, 42, 0.14);
background:
linear-gradient(135deg, rgba(15, 23, 42, 0.06), rgba(15, 23, 42, 0.02));
display: flex;
align-items: center;
justify-content: center;
}
.preview-frame img {
width: 100%;
height: auto;
display: block;
}
.preview-placeholder {
padding: 20px;
text-align: center;
color: var(--muted);
font-size: 14px;
line-height: 1.6;
}
.sandbox-grid {
display: grid;
gap: 16px;
}
.sandbox-card {
padding: 18px;
border-radius: 20px;
background: rgba(15, 23, 42, 0.04);
}
.sandbox-card h3 {
margin: 0 0 10px;
font-size: 16px;
}
.sandbox-card p {
margin: 0 0 14px;
color: var(--muted);
line-height: 1.6;
font-size: 14px;
}
.sandbox-form {
display: grid;
gap: 12px;
}
.sandbox-form input,
.sandbox-form textarea,
.sandbox-form select {
width: 100%;
border: 1px solid rgba(15, 23, 42, 0.12);
border-radius: 14px;
padding: 12px 14px;
background: #fff;
color: var(--text);
}
.hover-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
}
.hover-item {
padding: 14px;
border-radius: 18px;
background: linear-gradient(135deg, #ffffff, #eef2ff);
border: 1px solid rgba(15, 23, 42, 0.08);
transition: transform 0.12s ease, box-shadow 0.2s ease;
cursor: pointer;
}
.hover-item:hover {
transform: translateY(-2px);
box-shadow: 0 14px 24px rgba(15, 23, 42, 0.08);
}
.tokens {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.token {
padding: 8px 12px;
border-radius: 999px;
background: rgba(37, 99, 235, 0.08);
color: var(--brand-2);
font-size: 13px;
font-weight: 700;
cursor: pointer;
}
.event-log {
display: grid;
gap: 10px;
max-height: 320px;
overflow: auto;
}
.event-item {
padding: 12px 14px;
border-radius: 16px;
background: rgba(15, 23, 42, 0.04);
}
.event-item strong {
display: block;
margin-bottom: 6px;
font-size: 13px;
}
.event-item span {
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.docs-panel {
padding: 28px;
}
.docs-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 20px;
}
.docs-header p {
margin: 0;
color: var(--muted);
font-size: 14px;
}
.markdown-body {
color: var(--text);
line-height: 1.75;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
color: var(--text);
line-height: 1.3;
}
.markdown-body pre {
background: #0f172a;
color: #e2e8f0;
border-radius: 18px;
}
.markdown-body code {
padding: 2px 6px;
border-radius: 999px;
background: rgba(15, 23, 42, 0.08);
font-family: Consolas, monospace;
font-size: 12px;
}
.markdown-body pre code {
background: transparent;
padding: 0;
}
@media (max-width: 1240px) {
.hero,
.grid {
grid-template-columns: 1fr;
}
.page {
width: min(100%, calc(100% - 28px));
}
}
</style>
</head>
<body>
<div class="page">
<section class="hero">
<div class="panel hero-main">
<div class="eyebrow">FloatyJS Host Demo</div>
<h1>宿主页演示台</h1>
<p>
这个页面既是 FloatyJS 的宿主页示例,也是一个交互沙箱。你可以在右下角打开从页浮窗,
同时在这里主动调用 <code>FloatyMain</code> 的上下文采集和截图能力,还能继续在页面下方查看 README 文档。
</p>
<div class="hero-points">
<div class="point">用左侧控制台直接调用主端 API,验证主端本身的行为。</div>
<div class="point">在右侧沙箱里聚焦、输入、悬停不同组件,再去从页或主端抓上下文。</div>
<div class="point">页面底部保留 README 渲染,首页仍然兼具文档入口作用。</div>
</div>
</div>
<div class="panel hero-side">
<div class="status-pill" id="hostStatus" data-status="waiting">等待 FloatyMain 初始化</div>
<div class="status-grid">
<div class="metric">
<span class="metric-label">Main Version</span>
<span class="metric-value" id="mainVersion">-</span>
</div>
<div class="metric">
<span class="metric-label">Window State</span>
<span class="metric-value" id="windowState">-</span>
</div>
<div class="metric">
<span class="metric-label">Position</span>
<span class="metric-value" id="mainPosition">-</span>
</div>
<div class="metric">
<span class="metric-label">Pointer Tracked</span>
<span class="metric-value" id="pointerTracked">-</span>
</div>
</div>
</div>
</section>
<section class="grid">
<aside class="panel controls">
<h2 class="section-title">主端控制台</h2>
<p class="subtle">
这些按钮直接调用 <code>window.FloatyMain</code>,让你在宿主页就能验证浮窗控制、上下文采集和截图接口。
</p>
<div class="field">
<label for="depthRange">DOM 深度</label>
<div class="range-wrap">
<input id="depthRange" type="range" min="1" max="12" step="1" value="5">
<div class="depth-badge" id="depthValue">5 层</div>
</div>
</div>
<label class="check-row">
<input id="includePointer" type="checkbox" checked>
<span>截图时叠加模拟鼠标指针</span>
</label>
<label class="check-row">
<input id="fullPage" type="checkbox">
<span>截取整个页面</span>
</label>
<div class="button-grid">
<button id="openBtn">打开从页浮窗</button>
<button class="secondary" id="closeBtn">关闭从页浮窗</button>
<button class="secondary" id="toggleBtn">切换浮窗</button>
<button class="secondary" id="syncBtn">向从页同步页面文本</button>
<button class="secondary" id="focusBtn">获取主页面焦点上下文</button>
<button class="secondary" id="pointerBtn">获取主页面鼠标上下文</button>
<button class="secondary" id="interactionBtn">获取组合上下文</button>
<button class="secondary" id="shotBtn">获取主页面截图</button>
<button class="secondary" id="snapshotBtn">获取 AI Snapshot</button>
</div>
</aside>
<main class="panel results">
<div class="results-header">
<div>
<h2 class="section-title">主端结果面板</h2>
<div class="result-meta">
<span id="resultTitle">等待调用</span>
<span id="resultTime">-</span>
</div>
</div>
<div class="button-row">
<button class="ghost" id="copyJsonBtn">复制 JSON</button>
<button class="ghost" id="clearBtn">清空</button>
</div>
</div>
<div class="code-card">
<pre id="output">{
"message": "点击左侧按钮开始演示主端能力"
}</pre>
</div>
<div class="preview-shell">
<h3 class="section-title">宿主页截图预览</h3>
<div class="preview-frame">
<div class="preview-placeholder" id="previewPlaceholder">
这里会显示主页面截图预览。
<br>
你可以先在沙箱区里制造 hover、focus 和滚动,再回来截图。
</div>
<img id="preview" alt="Host screenshot preview" hidden>
</div>
</div>
</main>
<aside class="panel sandbox">
<h2 class="section-title">交互沙箱</h2>
<p class="subtle">
这里专门放了一些适合观察 focus / hover / text / screenshot 的组件,方便配合从页和主页能力一起验证。
</p>
<div class="sandbox-grid">
<div class="sandbox-card">
<h3>表单焦点区</h3>
<p>先点击输入框或选择器,再去获取焦点上下文。</p>
<div class="sandbox-form">
<input type="text" value="这是一个可聚焦的输入框" placeholder="在这里输入一些内容">
<textarea rows="4">这里是多行输入区域,适合测试焦点元素、value 与祖先链。</textarea>
<select>
<option>普通选项 A</option>
<option>普通选项 B</option>
<option>普通选项 C</option>
</select>
</div>
</div>
<div class="sandbox-card">
<h3>悬停卡片区</h3>
<p>把鼠标停在任一卡片上,再抓取鼠标上下文或 AI Snapshot。</p>
<div class="hover-grid">
<div class="hover-item">
<strong>Pricing Card</strong>
<div>Hover 我可以观察卡片层级</div>
</div>
<div class="hover-item">
<strong>Feature Card</strong>
<div>Hover 我可以观察文本和 rect</div>
</div>
<div class="hover-item">
<strong>Status Card</strong>
<div>我适合配合 screenshot 做验证</div>
</div>
<div class="hover-item">
<strong>CTA Card</strong>
<div>Hover 后再回到从页点按钮</div>
</div>
</div>
</div>
<div class="sandbox-card">
<h3>语义 Token 区</h3>
<p>这里放了很多小型可 hover 的元素,适合测试浅层与深层祖先链区别。</p>
<div class="tokens">
<span class="token">AI Agent</span>
<span class="token">Context</span>
<span class="token">Screenshot</span>
<span class="token">DOM Chain</span>
<span class="token">Hover</span>
<span class="token">Focus</span>
<span class="token">Prompting</span>
<span class="token">Automation</span>
</div>
</div>
<div class="sandbox-card">
<h3>事件日志</h3>
<div class="event-log" id="eventLog">
<div class="event-item">
<strong>Ready</strong>
<span>等待主端动作日志写入。</span>
</div>
</div>
</div>
</div>
</aside>
</section>
<section class="panel docs-panel" id="docs">
<div class="docs-header">
<div>
<h2 class="section-title" style="margin-bottom: 6px;">README 文档</h2>
<p>这里继续保留 <code>README.md</code> 的在线渲染结果。</p>
</div>
<div class="button-row">
<button class="ghost" id="scrollTopBtn">回到顶部</button>
</div>
</div>
<div id="content" class="markdown-body"></div>
</section>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const output = document.getElementById("output");
const preview = document.getElementById("preview");
const previewPlaceholder = document.getElementById("previewPlaceholder");
const eventLog = document.getElementById("eventLog");
const hostStatus = document.getElementById("hostStatus");
const mainVersion = document.getElementById("mainVersion");
const windowState = document.getElementById("windowState");
const mainPosition = document.getElementById("mainPosition");
const pointerTracked = document.getElementById("pointerTracked");
const depthRange = document.getElementById("depthRange");
const depthValue = document.getElementById("depthValue");
const includePointer = document.getElementById("includePointer");
const fullPage = document.getElementById("fullPage");
const resultTitle = document.getElementById("resultTitle");
const resultTime = document.getElementById("resultTime");
const state = {
lastJson: null,
};
function setStatus(status, text) {
hostStatus.dataset.status = status;
hostStatus.textContent = text;
}
function nowLabel() {
return new Date().toLocaleTimeString();
}
function currentDepth() {
return Number(depthRange.value);
}
function screenshotOptions() {
return {
includePointer: includePointer.checked,
fullPage: fullPage.checked,
};
}
function updateDepthLabel() {
depthValue.textContent = `${currentDepth()} 层`;
}
function appendEvent(title, detail) {
const item = document.createElement("div");
item.className = "event-item";
item.innerHTML = `
<strong>${title}</strong>
<span>${detail}</span>
`;
eventLog.prepend(item);
}
function renderJson(title, data, options = {}) {
state.lastJson = data;
resultTitle.textContent = title;
resultTime.textContent = `更新于 ${nowLabel()}`;
const rendered = options.compactDataUrl
? JSON.stringify(
{
...data,
dataUrl: data?.dataUrl
? `data-url(${data.dataUrl.length} chars)`
: data?.dataUrl,
screenshot: data?.screenshot?.dataUrl
? {
...data.screenshot,
dataUrl: `data-url(${data.screenshot.dataUrl.length} chars)`,
}
: data?.screenshot,
},
null,
2,
)
: JSON.stringify(data, null, 2);
output.textContent = rendered;
}
function renderScreenshot(data) {
if (!data || !data.dataUrl) {
preview.hidden = true;
previewPlaceholder.hidden = false;
return;
}
preview.src = data.dataUrl;
preview.hidden = false;
previewPlaceholder.hidden = true;
}
function refreshState() {
if (!window.FloatyMain) {
return;
}
const hostState = window.FloatyMain.getState();
mainVersion.textContent = window.FloatyMain.version || "-";
windowState.textContent = hostState.visible ? "Open" : "Closed";
mainPosition.textContent = hostState.position || "-";
pointerTracked.textContent = hostState.pointerTracked ? "Yes" : "No";
}
async function runAction(title, runner, options = {}) {
try {
setStatus("waiting", "执行中...");
const result = await runner();
renderJson(title, result, options);
if (result?.dataUrl) {
renderScreenshot(result);
} else if (result?.screenshot?.dataUrl) {
renderScreenshot(result.screenshot);
} else {
renderScreenshot(null);
}
appendEvent(title, `成功完成,时间 ${nowLabel()}`);
setStatus("connected", "FloatyMain 已就绪");
refreshState();
return result;
} catch (error) {
renderScreenshot(null);
renderJson("Error", {
message: error.message,
});
appendEvent(title, `失败: ${error.message}`);
setStatus("error", "执行失败");
throw error;
}
}
updateDepthLabel();
depthRange.addEventListener("input", updateDepthLabel);
if (!window.FloatyMain) {
setStatus("error", "FloatyMain 未初始化");
output.textContent = JSON.stringify({
message: "FloatyMain 没有成功挂到 window 上,请检查 floaty-main.js 是否加载成功。",
}, null, 2);
return;
}
setStatus("connected", "FloatyMain 已就绪");
refreshState();
appendEvent("Ready", "宿主页演示台初始化完成");
document.getElementById("openBtn").addEventListener("click", async () => {
await runAction("Open Floaty Window", async () => {
window.FloatyMain.open();
refreshState();
return window.FloatyMain.getState();
});
});
document.getElementById("closeBtn").addEventListener("click", async () => {
await runAction("Close Floaty Window", async () => {
window.FloatyMain.close();
refreshState();
return window.FloatyMain.getState();
});
});
document.getElementById("toggleBtn").addEventListener("click", async () => {
await runAction("Toggle Floaty Window", async () => {
window.FloatyMain.toggle();
refreshState();
return window.FloatyMain.getState();
});
});
document.getElementById("syncBtn").addEventListener("click", async () => {
await runAction("Sync Page Text To Slave", async () => {
window.FloatyMain.syncPageText();
return {
message: "已向从页发送 page:text 事件",
state: window.FloatyMain.getState(),
};
});
});
document.getElementById("focusBtn").addEventListener("click", async () => {
await runAction("Host Focus Context", () => window.FloatyMain.getFocusContext({
depth: currentDepth(),
}));
});
document.getElementById("pointerBtn").addEventListener("click", async () => {
await runAction("Host Pointer Context", () => window.FloatyMain.getPointerContext({
depth: currentDepth(),
}));
});
document.getElementById("interactionBtn").addEventListener("click", async () => {
await runAction(
"Host Interaction Context",
() => window.FloatyMain.getInteractionContext({
depth: currentDepth(),
}),
);
});
document.getElementById("shotBtn").addEventListener("click", async () => {
await runAction(
"Host Screenshot",
() => window.FloatyMain.captureScreenshot(screenshotOptions()),
{ compactDataUrl: true },
);
});
document.getElementById("snapshotBtn").addEventListener("click", async () => {
await runAction(
"Host AI Snapshot",
() => window.FloatyMain.getAISnapshot({
depth: currentDepth(),
includeScreenshot: true,
screenshot: screenshotOptions(),
}),
{ compactDataUrl: true },
);
});
document.getElementById("copyJsonBtn").addEventListener("click", async () => {
if (!state.lastJson) {
appendEvent("Copy JSON", "当前没有可复制的数据");
return;
}
await navigator.clipboard.writeText(JSON.stringify(state.lastJson, null, 2));
appendEvent("Copy JSON", "已复制当前 JSON 到剪贴板");
});
document.getElementById("clearBtn").addEventListener("click", () => {
state.lastJson = null;
resultTitle.textContent = "等待调用";
resultTime.textContent = "-";
output.textContent = JSON.stringify({
message: "结果已清空,继续点击左侧按钮重新演示。",
}, null, 2);
renderScreenshot(null);
appendEvent("Clear", "已清空结果面板");
});
document.getElementById("scrollTopBtn").addEventListener("click", () => {
window.scrollTo({ top: 0, behavior: "smooth" });
});
const md = window.markdownit({
html: true,
linkify: true,
typographer: true,
});
fetch("./README.md")
.then((response) => {
if (!response.ok) {
throw new Error(`无法获取 README.md 文件: ${response.statusText}`);
}
return response.text();
})
.then((markdown) => {
const result = md.render(markdown);
document.getElementById("content").innerHTML = result;
})
.catch((error) => {
console.error("获取或渲染 Markdown 时出错:", error);
document.getElementById("content").innerHTML =
`<p style="color: #b91c1c;">无法加载 README: ${error.message}</p>`;
});
});
</script>
</body>
</html>