-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiframe.html
More file actions
784 lines (683 loc) · 25.7 KB
/
Copy pathiframe.html
File metadata and controls
784 lines (683 loc) · 25.7 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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FloatyJS Slave Demo</title>
<style>
:root {
color-scheme: light;
--bg: #eef2ff;
--bg-accent: #dbeafe;
--panel: rgba(255, 255, 255, 0.88);
--panel-border: rgba(15, 23, 42, 0.08);
--text: #0f172a;
--muted: #475569;
--brand: #0f172a;
--brand-soft: #1d4ed8;
--success: #166534;
--danger: #b91c1c;
--shadow: 0 20px 40px rgba(15, 23, 42, 0.12);
--radius: 22px;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
font-family: "Segoe UI", Arial, sans-serif;
color: var(--text);
background:
radial-gradient(circle at top left, #bfdbfe 0, transparent 30%),
radial-gradient(circle at bottom right, #ddd6fe 0, transparent 28%),
linear-gradient(180deg, var(--bg) 0%, #f8fafc 100%);
}
.shell {
max-width: 1480px;
margin: 0 auto;
padding: 28px;
}
.hero {
display: grid;
grid-template-columns: 1.2fr 0.8fr;
gap: 18px;
margin-bottom: 18px;
}
.panel {
border: 1px solid var(--panel-border);
border-radius: var(--radius);
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: var(--shadow);
}
.hero-main {
padding: 28px;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.72);
color: var(--brand-soft);
font-size: 12px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.hero h1 {
margin: 18px 0 12px;
font-size: clamp(30px, 4vw, 48px);
line-height: 1;
}
.hero p {
margin: 0;
max-width: 62ch;
color: var(--muted);
font-size: 16px;
line-height: 1.6;
}
.hero-notes {
margin-top: 18px;
display: grid;
gap: 10px;
}
.note {
padding: 12px 14px;
border-radius: 16px;
background: rgba(15, 23, 42, 0.04);
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.hero-side {
padding: 22px;
display: grid;
gap: 14px;
align-content: start;
}
.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;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.metric .value {
font-size: 18px;
font-weight: 700;
word-break: break-word;
}
.status-pill {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-radius: 999px;
font-weight: 700;
font-size: 13px;
background: rgba(22, 101, 52, 0.12);
color: var(--success);
}
.status-pill[data-status="waiting"] {
background: rgba(29, 78, 216, 0.12);
color: #1d4ed8;
}
.status-pill[data-status="error"] {
background: rgba(185, 28, 28, 0.12);
color: var(--danger);
}
.grid {
display: grid;
grid-template-columns: 340px minmax(0, 1fr) 360px;
gap: 18px;
}
.controls,
.results,
.sidebar {
padding: 22px;
}
.section-title {
margin: 0 0 14px;
font-size: 18px;
}
.subtle {
margin: 0 0 16px;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.control-group {
display: grid;
gap: 12px;
margin-bottom: 18px;
}
.field {
display: grid;
gap: 8px;
}
.field label {
font-size: 13px;
font-weight: 700;
color: var(--muted);
}
.range-wrap {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
}
input[type="range"] {
width: 100%;
}
.depth-badge {
min-width: 56px;
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);
}
.button-grid {
display: grid;
gap: 10px;
}
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, opacity 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);
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.results-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 14px;
}
.result-meta {
display: flex;
flex-wrap: wrap;
gap: 10px;
color: var(--muted);
font-size: 13px;
}
.code-card {
min-height: 360px;
border-radius: 20px;
background: #0f172a;
color: #e2e8f0;
overflow: hidden;
}
pre {
margin: 0;
height: 100%;
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;
background:
linear-gradient(135deg, rgba(15, 23, 42, 0.06), rgba(15, 23, 42, 0.02));
border: 1px dashed rgba(15, 23, 42, 0.14);
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
.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;
}
.tips,
.event-log {
display: grid;
gap: 10px;
}
.tip-card,
.event-item {
padding: 12px 14px;
border-radius: 16px;
background: rgba(15, 23, 42, 0.04);
}
.tip-card strong,
.event-item strong {
display: block;
margin-bottom: 6px;
font-size: 13px;
}
.tip-card span,
.event-item span {
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.event-log {
max-height: 340px;
overflow: auto;
}
.event-empty {
color: var(--muted);
font-size: 14px;
}
.footer {
margin-top: 18px;
padding: 18px 22px;
}
.footer p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
code {
padding: 2px 6px;
border-radius: 999px;
background: rgba(15, 23, 42, 0.08);
font-family: Consolas, monospace;
font-size: 12px;
}
@media (max-width: 1200px) {
.grid {
grid-template-columns: 1fr;
}
.hero {
grid-template-columns: 1fr;
}
}
</style>
<script type="module" src="./floaty-slave.js"></script>
</head>
<body>
<div class="shell">
<section class="hero">
<div class="panel hero-main">
<div class="eyebrow">FloatyJS Slave Demo</div>
<h1>一个完整的主从能力演示页</h1>
<p>
这个页面运行在 iframe 里,通过 <code>window.FloatySlave</code> 主动向宿主页索取上下文。
你可以在宿主页里移动鼠标、切换焦点,然后回到这里点击按钮,观察 AI 可消费的数据结构。
</p>
<div class="hero-notes">
<div class="note">焦点上下文:返回宿主页当前焦点元素及其祖先链,默认向上 5 层。</div>
<div class="note">鼠标上下文:返回宿主页最近一次鼠标所在元素及其祖先链,适合 hover 类场景。</div>
<div class="note">截图:使用 <code>html2canvas</code> 截取宿主页,并可叠加一个模拟鼠标指针。</div>
</div>
</div>
<div class="panel hero-side">
<div class="status-pill" id="connectionStatus" data-status="waiting">等待连接</div>
<div class="status-grid">
<div class="metric">
<span class="label">Slave Version</span>
<span class="value" id="slaveVersion">-</span>
</div>
<div class="metric">
<span class="label">Parent Origin</span>
<span class="value" id="parentOrigin">-</span>
</div>
<div class="metric">
<span class="label">Last Action</span>
<span class="value" id="lastAction">None</span>
</div>
<div class="metric">
<span class="label">Events Logged</span>
<span class="value" id="eventCount">0</span>
</div>
</div>
</div>
</section>
<section class="grid">
<aside class="panel controls">
<h2 class="section-title">操作面板</h2>
<p class="subtle">
先在宿主页里悬停或聚焦目标元素,再回到这里点击按钮,查看收集到的数据。
</p>
<div class="control-group">
<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>
<div class="button-grid">
<button id="runAllBtn">一键获取 AI Snapshot</button>
<button class="secondary" id="pageTextBtn">获取页面纯文本</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>
</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" id="previewFrame">
<div class="preview-placeholder" id="previewPlaceholder">
这里会显示宿主页截图预览。
<br>
如果你启用了 <code>includePointer</code>,截图里还会叠加一个模拟鼠标指针。
</div>
<img id="preview" alt="Screenshot preview" hidden>
</div>
</div>
</main>
<aside class="panel sidebar">
<h2 class="section-title">事件与提示</h2>
<div class="tips">
<div class="tip-card">
<strong>如何观察焦点</strong>
<span>先在宿主页里点中一个输入框、按钮或文本块,再回来点“获取焦点上下文”。</span>
</div>
<div class="tip-card">
<strong>如何观察鼠标</strong>
<span>先在宿主页里把鼠标停留在目标元素上,再回来点“获取鼠标上下文”或“获取 AI Snapshot”。</span>
</div>
<div class="tip-card">
<strong>截图里的指针说明</strong>
<span>浏览器无法稳定捕获系统鼠标指针,所以这里叠加的是基于最近鼠标位置的模拟指针。</span>
</div>
</div>
<h3 class="section-title" style="margin-top: 18px;">事件日志</h3>
<div class="event-log" id="eventLog">
<div class="event-empty">还没有事件,等宿主页发来 <code>page:text</code> 或点击按钮后,这里会开始记录。</div>
</div>
</aside>
</section>
<section class="panel footer">
<p>
你也可以在 iframe 控制台里直接调用
<code>FloatySlave.getFocusContext()</code>、
<code>FloatySlave.getPointerContext()</code>、
<code>FloatySlave.captureScreenshot()</code>、
<code>FloatySlave.getAISnapshot()</code>
来验证这套接口。
</p>
</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 eventCount = document.getElementById("eventCount");
const connectionStatus = document.getElementById("connectionStatus");
const slaveVersion = document.getElementById("slaveVersion");
const parentOrigin = document.getElementById("parentOrigin");
const lastAction = document.getElementById("lastAction");
const resultTitle = document.getElementById("resultTitle");
const resultTime = document.getElementById("resultTime");
const depthRange = document.getElementById("depthRange");
const depthValue = document.getElementById("depthValue");
const includePointer = document.getElementById("includePointer");
const fullPage = document.getElementById("fullPage");
const state = {
eventCount: 0,
lastJson: null,
};
function currentDepth() {
return Number(depthRange.value);
}
function screenshotOptions() {
return {
includePointer: includePointer.checked,
fullPage: fullPage.checked,
};
}
function nowLabel() {
return new Date().toLocaleTimeString();
}
function appendEvent(title, detail) {
const empty = eventLog.querySelector(".event-empty");
if (empty) {
empty.remove();
}
state.eventCount += 1;
eventCount.textContent = String(state.eventCount);
const item = document.createElement("div");
item.className = "event-item";
item.innerHTML = `
<strong>${title}</strong>
<span>${detail}</span>
`;
eventLog.prepend(item);
}
function setConnection(status, text) {
connectionStatus.dataset.status = status;
connectionStatus.textContent = text;
}
function renderJson(title, data, options = {}) {
state.lastJson = data;
resultTitle.textContent = title;
resultTime.textContent = `更新于 ${nowLabel()}`;
lastAction.textContent = title;
const rendered = options.compactDataUrl
? JSON.stringify(
{
...data,
dataUrl: data.dataUrl
? `data-url(${data.dataUrl.length} chars)`
: data.dataUrl,
screenshot: data.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;
}
async function runAction(title, runner, options = {}) {
try {
setConnection("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()}`);
setConnection("connected", "已连接");
return result;
} catch (error) {
appendEvent(title, `失败: ${error.message}`);
setConnection("error", "操作失败");
renderJson("Error", {
message: error.message,
});
renderScreenshot(null);
throw error;
}
}
function updateDepthLabel() {
depthValue.textContent = `${currentDepth()} 层`;
}
depthRange.addEventListener("input", updateDepthLabel);
updateDepthLabel();
if (!window.FloatySlave) {
setConnection("error", "FloatySlave 未初始化");
output.textContent = JSON.stringify({
message: "FloatySlave 没有成功挂到 window 上,请检查 floaty-slave.js 是否加载成功。",
}, null, 2);
return;
}
setConnection("connected", "已连接");
slaveVersion.textContent = window.FloatySlave.version || "-";
parentOrigin.textContent = window.FloatySlave.parentOrigin || "-";
appendEvent("Ready", "FloatySlave 已完成初始化");
window.FloatySlave.on("page:text", (payload) => {
appendEvent(
"page:text",
`收到主页面文本事件,文本长度 ${payload?.text?.length || 0},pageInfo ${payload?.pageInfo ? "已提供" : "为空"}`,
);
});
document.getElementById("pageTextBtn").addEventListener("click", async () => {
await runAction("Page Text", () => window.FloatySlave.getPageText());
});
document.getElementById("focusBtn").addEventListener("click", async () => {
await runAction("Focus Context", () => window.FloatySlave.getFocusContext({
depth: currentDepth(),
}));
});
document.getElementById("pointerBtn").addEventListener("click", async () => {
await runAction("Pointer Context", () => window.FloatySlave.getPointerContext({
depth: currentDepth(),
}));
});
document.getElementById("interactionBtn").addEventListener("click", async () => {
await runAction(
"Interaction Context",
() => window.FloatySlave.getInteractionContext({
depth: currentDepth(),
}),
);
});
document.getElementById("shotBtn").addEventListener("click", async () => {
await runAction(
"Screenshot",
() => window.FloatySlave.captureScreenshot(screenshotOptions()),
{ compactDataUrl: true },
);
});
document.getElementById("runAllBtn").addEventListener("click", async () => {
await runAction(
"AI Snapshot",
() => window.FloatySlave.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", "已复制当前结果到剪贴板");
});
document.getElementById("clearBtn").addEventListener("click", () => {
state.lastJson = null;
resultTitle.textContent = "等待操作";
resultTime.textContent = "-";
lastAction.textContent = "None";
output.textContent = JSON.stringify({
message: "结果已清空,继续点击按钮重新演示。",
}, null, 2);
renderScreenshot(null);
appendEvent("Clear", "已清空结果面板");
});
});
</script>
</body>
</html>