@full-self-browsing/phantom-stream 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +263 -0
- package/bin/phantom-stream.js +193 -0
- package/dist/types/adapters/bookmarklet.d.ts +22 -0
- package/dist/types/adapters/bookmarklet.d.ts.map +1 -0
- package/dist/types/adapters/browser-inject.d.ts +7 -0
- package/dist/types/adapters/browser-inject.d.ts.map +1 -0
- package/dist/types/adapters/extension.d.ts +43 -0
- package/dist/types/adapters/extension.d.ts.map +1 -0
- package/dist/types/adapters/playwright-inject.d.ts +2 -0
- package/dist/types/adapters/playwright-inject.d.ts.map +1 -0
- package/dist/types/adapters/playwright.d.ts +14 -0
- package/dist/types/adapters/playwright.d.ts.map +1 -0
- package/dist/types/capture/index.d.ts +241 -0
- package/dist/types/capture/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/protocol/constants.d.ts +35 -0
- package/dist/types/protocol/constants.d.ts.map +1 -0
- package/dist/types/protocol/envelope.d.ts +41 -0
- package/dist/types/protocol/envelope.d.ts.map +1 -0
- package/dist/types/protocol/index.d.ts +5 -0
- package/dist/types/protocol/index.d.ts.map +1 -0
- package/dist/types/protocol/messages.d.ts +520 -0
- package/dist/types/protocol/messages.d.ts.map +1 -0
- package/dist/types/protocol/remote-control.d.ts +70 -0
- package/dist/types/protocol/remote-control.d.ts.map +1 -0
- package/dist/types/relay/backends/ws.d.ts +39 -0
- package/dist/types/relay/backends/ws.d.ts.map +1 -0
- package/dist/types/relay/index.d.ts +4 -0
- package/dist/types/relay/index.d.ts.map +1 -0
- package/dist/types/relay/limits.d.ts +54 -0
- package/dist/types/relay/limits.d.ts.map +1 -0
- package/dist/types/relay/relay.d.ts +73 -0
- package/dist/types/relay/relay.d.ts.map +1 -0
- package/dist/types/renderer/diff.d.ts +62 -0
- package/dist/types/renderer/diff.d.ts.map +1 -0
- package/dist/types/renderer/index.d.ts +211 -0
- package/dist/types/renderer/index.d.ts.map +1 -0
- package/dist/types/renderer/overlays.d.ts +166 -0
- package/dist/types/renderer/overlays.d.ts.map +1 -0
- package/dist/types/renderer/sanitize.d.ts +73 -0
- package/dist/types/renderer/sanitize.d.ts.map +1 -0
- package/dist/types/renderer/snapshot.d.ts +41 -0
- package/dist/types/renderer/snapshot.d.ts.map +1 -0
- package/dist/types/transport/websocket.d.ts +44 -0
- package/dist/types/transport/websocket.d.ts.map +1 -0
- package/docs/ARCHITECTURE.md +273 -0
- package/docs/DESIGN-HISTORY.md +120 -0
- package/docs/QUICKSTARTS.md +289 -0
- package/docs/RELEASE.md +60 -0
- package/docs/SECURITY.md +156 -0
- package/docs/paper/OUTLINE.md +100 -0
- package/examples/bookmarklet-demo/demo.css +102 -0
- package/examples/bookmarklet-demo/server.js +297 -0
- package/examples/bookmarklet-demo/source.html +52 -0
- package/examples/bookmarklet-demo/viewer.html +48 -0
- package/examples/extension-mv3/demo.css +102 -0
- package/examples/extension-mv3/server.js +350 -0
- package/examples/extension-mv3/source.html +48 -0
- package/examples/extension-mv3/viewer.html +48 -0
- package/examples/loopback-mirror.html +369 -0
- package/examples/loopback-transport.js +65 -0
- package/examples/playwright-demo/demo.css +462 -0
- package/examples/playwright-demo/fixture.html +56 -0
- package/examples/playwright-demo/fixture.js +57 -0
- package/examples/playwright-demo/server.js +517 -0
- package/examples/playwright-demo/viewer.html +112 -0
- package/examples/playwright-demo/viewer.js +414 -0
- package/examples/serve.js +97 -0
- package/examples/two-tab-demo/demo.css +266 -0
- package/examples/two-tab-demo/server.js +233 -0
- package/examples/two-tab-demo/source.html +70 -0
- package/examples/two-tab-demo/source.js +213 -0
- package/examples/two-tab-demo/viewer.html +72 -0
- package/examples/two-tab-demo/viewer.js +121 -0
- package/package.json +107 -0
- package/src/adapters/bookmarklet.js +98 -0
- package/src/adapters/browser-inject.js +17 -0
- package/src/adapters/extension.js +322 -0
- package/src/adapters/playwright-inject.js +4401 -0
- package/src/adapters/playwright.js +540 -0
- package/src/capture/README.md +302 -0
- package/src/capture/index.js +4361 -0
- package/src/index.js +5 -0
- package/src/protocol/constants.js +49 -0
- package/src/protocol/envelope.js +75 -0
- package/src/protocol/index.js +4 -0
- package/src/protocol/messages.js +268 -0
- package/src/protocol/remote-control.js +300 -0
- package/src/relay/README.md +22 -0
- package/src/relay/backends/ws.js +186 -0
- package/src/relay/index.js +3 -0
- package/src/relay/limits.js +105 -0
- package/src/relay/relay.js +397 -0
- package/src/renderer/README.md +443 -0
- package/src/renderer/diff.js +389 -0
- package/src/renderer/index.js +1401 -0
- package/src/renderer/overlays.js +528 -0
- package/src/renderer/sanitize.js +366 -0
- package/src/renderer/snapshot.js +202 -0
- package/src/transport/websocket.js +553 -0
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
// PhantomStream renderer overlays: host-document overlay layer, kind-keyed
|
|
2
|
+
// registry, and the parity built-ins (action glow, progress pill, dialog card).
|
|
3
|
+
//
|
|
4
|
+
// Behavioral port of the FSB reference viewer's overlay/dialog handlers
|
|
5
|
+
// (reference/dashboard/dashboard.js:3374-3443, shipped as FSB milestone
|
|
6
|
+
// v0.9.9.1), restructured behind the Phase 2 registry seam (02-RESEARCH.md
|
|
7
|
+
// Pattern 5): every non-identity key of a STREAM.OVERLAY payload is an
|
|
8
|
+
// overlay kind dispatched through ONE Map -- glow and progress are
|
|
9
|
+
// pre-registered through that same Map with zero special-cased dispatch
|
|
10
|
+
// (D-09/D-10), proving the registerOverlay extension seam by construction.
|
|
11
|
+
//
|
|
12
|
+
// Intentional divergences from the reference (renderer divergence ledger):
|
|
13
|
+
// - Class names: the reference's dashboard-prefixed preview classes are
|
|
14
|
+
// renamed to ps-overlay-* (02-UI-SPEC.md contract).
|
|
15
|
+
// - Icons: the reference uses Font Awesome (an FSB dashboard asset); a
|
|
16
|
+
// zero-dependency framework cannot ship an icon font, so the dialog
|
|
17
|
+
// icons are equivalent inline SVGs (warning-triangle for alert,
|
|
18
|
+
// question-circle for confirm, keyboard for prompt) -- UI-SPEC-locked.
|
|
19
|
+
// - Accessibility additions: aria-hidden="true" on glow/progress
|
|
20
|
+
// (decorative), role="status" aria-live="polite" on the dialog card.
|
|
21
|
+
// The layer itself is NOT aria-hidden -- an aria-hidden ancestor would
|
|
22
|
+
// suppress the dialog's live region.
|
|
23
|
+
// - Glow reposition transitions are wrapped in
|
|
24
|
+
// @media (prefers-reduced-motion: no-preference) (additive).
|
|
25
|
+
//
|
|
26
|
+
// Overlays render in the HOST document, positioned above the sandboxed
|
|
27
|
+
// iframe; they are never injected into the mirror document (CONTEXT-locked).
|
|
28
|
+
// Overlay payloads originate on the capture side (attacker-influenced page
|
|
29
|
+
// context): the built-ins write all message/label text via textContent,
|
|
30
|
+
// NEVER innerHTML (threat T-02-04). Custom renderFns receive the raw payload
|
|
31
|
+
// value and own their escaping (T-02-05; see register()).
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Single injected-CSS string for the overlay built-ins. The host (or
|
|
35
|
+
* createViewer, plan 02-03) appends this as one <style> element -- no
|
|
36
|
+
* external stylesheet, per the zero-dependency constraint. Every value is
|
|
37
|
+
* the parity contract from 02-UI-SPEC.md (exact reference CSS for the
|
|
38
|
+
* dialog card; documented reconstructions for the glow rect and progress
|
|
39
|
+
* pill where the FSB stylesheet is not vendored).
|
|
40
|
+
*/
|
|
41
|
+
export var OVERLAY_CSS = [
|
|
42
|
+
'.ps-overlay-glow {',
|
|
43
|
+
' position: absolute;',
|
|
44
|
+
' border: 2px solid #f59e0b;',
|
|
45
|
+
' border-radius: 4px;',
|
|
46
|
+
' box-shadow: 0 0 12px rgba(245, 158, 11, 0.6);',
|
|
47
|
+
' background: transparent;',
|
|
48
|
+
' pointer-events: none;',
|
|
49
|
+
' display: none; /* shown only while glow.state === \'active\' */',
|
|
50
|
+
'}',
|
|
51
|
+
'@media (prefers-reduced-motion: no-preference) {',
|
|
52
|
+
' .ps-overlay-glow {',
|
|
53
|
+
' transition: top 100ms ease, left 100ms ease, width 100ms ease, height 100ms ease;',
|
|
54
|
+
' }',
|
|
55
|
+
'}',
|
|
56
|
+
'.ps-node-highlight {',
|
|
57
|
+
' position: absolute;',
|
|
58
|
+
' border: 2px solid #38bdf8;',
|
|
59
|
+
' border-radius: 4px;',
|
|
60
|
+
' box-shadow: 0 0 0 1px rgba(8, 47, 73, 0.55), 0 0 14px rgba(56, 189, 248, 0.45);',
|
|
61
|
+
' background: rgba(56, 189, 248, 0.12);',
|
|
62
|
+
' pointer-events: none;',
|
|
63
|
+
' display: none;',
|
|
64
|
+
'}',
|
|
65
|
+
'@media (prefers-reduced-motion: no-preference) {',
|
|
66
|
+
' .ps-node-highlight {',
|
|
67
|
+
' transition: top 80ms ease, left 80ms ease, width 80ms ease, height 80ms ease;',
|
|
68
|
+
' }',
|
|
69
|
+
'}',
|
|
70
|
+
'.ps-node-highlight-label {',
|
|
71
|
+
' position: absolute;',
|
|
72
|
+
' left: 0;',
|
|
73
|
+
' top: -24px;',
|
|
74
|
+
' max-width: 240px;',
|
|
75
|
+
' overflow: hidden;',
|
|
76
|
+
' text-overflow: ellipsis;',
|
|
77
|
+
' white-space: nowrap;',
|
|
78
|
+
' border-radius: 4px;',
|
|
79
|
+
' padding: 3px 7px;',
|
|
80
|
+
' background: rgba(8, 47, 73, 0.92);',
|
|
81
|
+
' color: #e0f2fe;',
|
|
82
|
+
' font: 600 12px/1.2 system-ui, sans-serif;',
|
|
83
|
+
'}',
|
|
84
|
+
'.ps-overlay-progress {',
|
|
85
|
+
' position: absolute;',
|
|
86
|
+
' bottom: 8px;',
|
|
87
|
+
' left: 8px;',
|
|
88
|
+
' background: rgba(0, 0, 0, 0.75);',
|
|
89
|
+
' backdrop-filter: blur(4px);',
|
|
90
|
+
' -webkit-backdrop-filter: blur(4px);',
|
|
91
|
+
' color: #e0e0e0;',
|
|
92
|
+
' font: 600 13px/1.2 system-ui, sans-serif;',
|
|
93
|
+
' padding: 4px 12px;',
|
|
94
|
+
' border-radius: 6px;',
|
|
95
|
+
' max-width: calc(100% - 16px);',
|
|
96
|
+
' white-space: nowrap;',
|
|
97
|
+
' overflow: hidden;',
|
|
98
|
+
' text-overflow: ellipsis;',
|
|
99
|
+
' pointer-events: none;',
|
|
100
|
+
' display: none;',
|
|
101
|
+
'}',
|
|
102
|
+
'.ps-overlay-dialog {',
|
|
103
|
+
' position: absolute; top: 0; left: 0; right: 0; bottom: 0;',
|
|
104
|
+
' background: rgba(0, 0, 0, 0.5);',
|
|
105
|
+
' display: flex; align-items: center; justify-content: center;',
|
|
106
|
+
' pointer-events: none;',
|
|
107
|
+
' /* display: flex when open, none when closed */',
|
|
108
|
+
'}',
|
|
109
|
+
'.ps-overlay-dialog-card {',
|
|
110
|
+
' background: #1e1e2e;',
|
|
111
|
+
' border: 1px solid #333;',
|
|
112
|
+
' border-radius: 12px;',
|
|
113
|
+
' padding: 24px;',
|
|
114
|
+
' max-width: 320px;',
|
|
115
|
+
' width: 80%;',
|
|
116
|
+
' text-align: center;',
|
|
117
|
+
' box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);',
|
|
118
|
+
'}',
|
|
119
|
+
'.ps-overlay-dialog-icon {',
|
|
120
|
+
' font-size: 28px; /* SVG icons: width/height 28px */',
|
|
121
|
+
' color: #f59e0b;',
|
|
122
|
+
' margin-bottom: 12px;',
|
|
123
|
+
'}',
|
|
124
|
+
'.ps-overlay-dialog-type {',
|
|
125
|
+
' font-size: 13px; font-weight: 600;',
|
|
126
|
+
' text-transform: uppercase; letter-spacing: 1px;',
|
|
127
|
+
' color: #888;',
|
|
128
|
+
' margin-bottom: 8px;',
|
|
129
|
+
'}',
|
|
130
|
+
'.ps-overlay-dialog-message {',
|
|
131
|
+
' font-size: 14px; color: #e0e0e0; line-height: 1.5;',
|
|
132
|
+
' word-break: break-word; max-height: 200px; overflow-y: auto;',
|
|
133
|
+
'}'
|
|
134
|
+
].join('\n');
|
|
135
|
+
|
|
136
|
+
// Inline SVG dialog icons (Font Awesome replacement, UI-SPEC-locked
|
|
137
|
+
// divergence). 28px boxes per the parity icon size; fill: currentColor so
|
|
138
|
+
// the .ps-overlay-dialog-icon color (#f59e0b) applies. These are the ONLY
|
|
139
|
+
// markup strings assigned via innerHTML -- all capture-influenced text goes
|
|
140
|
+
// through textContent (T-02-04).
|
|
141
|
+
var ICON_SVG = {
|
|
142
|
+
// alert: warning triangle
|
|
143
|
+
alert: '<svg width="28" height="28" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">'
|
|
144
|
+
+ '<path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z"/></svg>',
|
|
145
|
+
// confirm: question circle
|
|
146
|
+
confirm: '<svg width="28" height="28" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">'
|
|
147
|
+
+ '<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 17h-2v-2h2v2z'
|
|
148
|
+
+ 'm2.07-7.75l-.9.92C13.45 12.9 13 13.5 13 15h-2v-.5c0-1.1.45-2.1 1.17-2.83l1.24-1.26'
|
|
149
|
+
+ 'c.37-.36.59-.86.59-1.41 0-1.1-.9-2-2-2s-2 .9-2 2H8c0-2.21 1.79-4 4-4s4 1.79 4 4'
|
|
150
|
+
+ 'c0 .88-.36 1.68-.93 2.25z"/></svg>',
|
|
151
|
+
// prompt: keyboard
|
|
152
|
+
prompt: '<svg width="28" height="28" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">'
|
|
153
|
+
+ '<path d="M20 5H4c-1.1 0-1.99.9-1.99 2L2 17c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V7'
|
|
154
|
+
+ 'c0-1.1-.9-2-2-2zm-9 3h2v2h-2V8zm0 3h2v2h-2v-2zM8 8h2v2H8V8zm0 3h2v2H8v-2zm-1 2H5v-2h2v2z'
|
|
155
|
+
+ 'm0-3H5V8h2v2zm9 7H8v-2h8v2zm0-4h-2v-2h2v2zm0-3h-2V8h2v2zm3 3h-2v-2h2v2zm0-3h-2V8h2v2z"/></svg>'
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Map a rect from capture-page viewport coordinates into host-document
|
|
160
|
+
* pixel coordinates over the scaled, letterboxed iframe. Pure parity math
|
|
161
|
+
* (reference dashboard.js:3381-3384):
|
|
162
|
+
*
|
|
163
|
+
* top = offsetY + y * s
|
|
164
|
+
* left = offsetX + x * s
|
|
165
|
+
* width = w * s
|
|
166
|
+
* height = h * s
|
|
167
|
+
*
|
|
168
|
+
* @param {{x: number, y: number, w: number, h: number}} rect
|
|
169
|
+
* Rect in capture-page viewport coordinates.
|
|
170
|
+
* @param {{s: number, offsetX: number, offsetY: number}} scale
|
|
171
|
+
* Scale state from the viewer's scale-to-fit (s = scale factor,
|
|
172
|
+
* offsetX/offsetY = letterbox offsets in host px).
|
|
173
|
+
* @returns {{top: number, left: number, width: number, height: number}}
|
|
174
|
+
* Host-document pixel numbers (callers append 'px').
|
|
175
|
+
*/
|
|
176
|
+
export function mapRectToHost(rect, scale) {
|
|
177
|
+
return {
|
|
178
|
+
top: scale.offsetY + rect.y * scale.s,
|
|
179
|
+
left: scale.offsetX + rect.x * scale.s,
|
|
180
|
+
width: rect.w * scale.s,
|
|
181
|
+
height: rect.h * scale.s
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Map a host-document point over the scaled mirror back into captured-page
|
|
187
|
+
* viewport CSS pixels. Letterbox/out-of-bounds points are classified before
|
|
188
|
+
* rounding and clamping so hosts can reject non-dispatchable input.
|
|
189
|
+
*
|
|
190
|
+
* @param {{x: number, y: number}} point
|
|
191
|
+
* Host-stage point relative to the viewer root.
|
|
192
|
+
* @param {{s?: number, offsetX?: number, offsetY?: number, pageW?: number, pageH?: number}} scale
|
|
193
|
+
* Scale state from the viewer. Missing page dimensions are treated as 0.
|
|
194
|
+
* @returns {{inside: boolean, x: number|null, y: number|null}}
|
|
195
|
+
* Dispatchable viewport coordinates, or null coordinates when outside.
|
|
196
|
+
*/
|
|
197
|
+
export function mapHostPointToViewport(point, scale) {
|
|
198
|
+
var p = point || {};
|
|
199
|
+
var sc = scale || {};
|
|
200
|
+
var s = (typeof sc.s === 'number' && isFinite(sc.s) && sc.s > 0) ? sc.s : 1;
|
|
201
|
+
var offsetX = (typeof sc.offsetX === 'number' && isFinite(sc.offsetX)) ? sc.offsetX : 0;
|
|
202
|
+
var offsetY = (typeof sc.offsetY === 'number' && isFinite(sc.offsetY)) ? sc.offsetY : 0;
|
|
203
|
+
var pageW = (typeof sc.pageW === 'number' && isFinite(sc.pageW)) ? Math.max(0, sc.pageW) : 0;
|
|
204
|
+
var pageH = (typeof sc.pageH === 'number' && isFinite(sc.pageH)) ? Math.max(0, sc.pageH) : 0;
|
|
205
|
+
|
|
206
|
+
if (pageW <= 0 || pageH <= 0 ||
|
|
207
|
+
typeof p.x !== 'number' || !isFinite(p.x) ||
|
|
208
|
+
typeof p.y !== 'number' || !isFinite(p.y)) {
|
|
209
|
+
return { inside: false, x: null, y: null };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
var rawX = (p.x - offsetX) / s;
|
|
213
|
+
var rawY = (p.y - offsetY) / s;
|
|
214
|
+
if (rawX < 0 || rawY < 0 || rawX >= pageW || rawY >= pageH) {
|
|
215
|
+
return { inside: false, x: null, y: null };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
var maxX = Math.max(0, Math.floor(pageW) - 1);
|
|
219
|
+
var maxY = Math.max(0, Math.floor(pageH) - 1);
|
|
220
|
+
return {
|
|
221
|
+
inside: true,
|
|
222
|
+
x: Math.max(0, Math.min(maxX, Math.round(rawX))),
|
|
223
|
+
y: Math.max(0, Math.min(maxY, Math.round(rawY)))
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Per-message context handed to handleOverlayMessage by the viewer.
|
|
229
|
+
* @typedef {Object} OverlayContext
|
|
230
|
+
* @property {{s: number, offsetX: number, offsetY: number}} scale
|
|
231
|
+
* Current scale-to-fit state for coordinate mapping.
|
|
232
|
+
* @property {(nid: number|string) => ({top: number, left: number, width: number, height: number}|null)} resolveNidRect
|
|
233
|
+
* Resolve a captured node id to a host-document rect (the viewer reads
|
|
234
|
+
* the mirrored element's bounding rect and maps it). Returns null when
|
|
235
|
+
* the nid no longer resolves (stale anchor).
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Overlay system handle returned by createOverlays.
|
|
240
|
+
* @typedef {Object} OverlaysHandle
|
|
241
|
+
* @property {Element} layer
|
|
242
|
+
* The overlay layer element. The caller (createViewer) appends it into
|
|
243
|
+
* the viewer root as a sibling ABOVE the iframe; this module never
|
|
244
|
+
* touches the host DOM outside this subtree.
|
|
245
|
+
* @property {(kind: string, renderFn: (payload: *, anchorRect: ?Object, layer: Element) => void) => void} register
|
|
246
|
+
* Register a renderer for an overlay kind (registry write; overwrites
|
|
247
|
+
* silently). See register() JSDoc for the renderFn contract.
|
|
248
|
+
* @property {(payload: Object, ctx: OverlayContext) => void} handleOverlayMessage
|
|
249
|
+
* Dispatch one STREAM.OVERLAY payload through the registry.
|
|
250
|
+
* @property {(payload: Object) => void} handleDialogMessage
|
|
251
|
+
* STREAM.DIALOG handler (dialog card show/hide).
|
|
252
|
+
* @property {() => void} resetOverlays
|
|
253
|
+
* Hide all kinds (new-snapshot reset, dashboard.js:2762-2764 parity).
|
|
254
|
+
*/
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Create the overlay system: layer element, kind registry, and the three
|
|
258
|
+
* pre-registered parity built-ins (glow, progress, dialog card).
|
|
259
|
+
*
|
|
260
|
+
* @param {{document: Document, logger?: {info: Function, warn: Function, error: Function}}} opts
|
|
261
|
+
* document is required (factory-time validation is the one place this
|
|
262
|
+
* module may throw, matching the capture factory precedent D-07);
|
|
263
|
+
* logger defaults to a console-backed logger.
|
|
264
|
+
* @returns {OverlaysHandle}
|
|
265
|
+
*/
|
|
266
|
+
export function createOverlays(opts) {
|
|
267
|
+
var cfg = opts || {};
|
|
268
|
+
var doc = cfg.document;
|
|
269
|
+
if (!doc || typeof doc.createElement !== 'function') {
|
|
270
|
+
throw new Error('overlays-document-required');
|
|
271
|
+
}
|
|
272
|
+
var logger = cfg.logger || {
|
|
273
|
+
info: function () { console.info.apply(console, arguments); },
|
|
274
|
+
warn: function () { console.warn.apply(console, arguments); },
|
|
275
|
+
error: function () { console.error.apply(console, arguments); }
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// --- Layer: absolute, inset 0, clipped, input-transparent, above the
|
|
279
|
+
// iframe (iframe z 1, layer z 2 per 02-UI-SPEC layer structure). ---
|
|
280
|
+
var layer = doc.createElement('div');
|
|
281
|
+
layer.style.position = 'absolute';
|
|
282
|
+
layer.style.top = '0px';
|
|
283
|
+
layer.style.left = '0px';
|
|
284
|
+
layer.style.right = '0px';
|
|
285
|
+
layer.style.bottom = '0px';
|
|
286
|
+
layer.style.overflow = 'hidden';
|
|
287
|
+
layer.style.pointerEvents = 'none';
|
|
288
|
+
layer.style.zIndex = '2'; // iframe is z 1 inside the viewer root
|
|
289
|
+
|
|
290
|
+
// --- Built-in elements. Sub-layer order (02-UI-SPEC): glow 10 <
|
|
291
|
+
// progress 20 < dialog 30 (dialog topmost, reference parity). ---
|
|
292
|
+
var glowEl = doc.createElement('div');
|
|
293
|
+
glowEl.className = 'ps-overlay-glow';
|
|
294
|
+
glowEl.style.zIndex = '10';
|
|
295
|
+
glowEl.style.display = 'none';
|
|
296
|
+
glowEl.setAttribute('aria-hidden', 'true'); // decorative highlight
|
|
297
|
+
layer.appendChild(glowEl);
|
|
298
|
+
|
|
299
|
+
var progressEl = doc.createElement('div');
|
|
300
|
+
progressEl.className = 'ps-overlay-progress';
|
|
301
|
+
progressEl.style.zIndex = '20';
|
|
302
|
+
progressEl.style.display = 'none';
|
|
303
|
+
progressEl.setAttribute('aria-hidden', 'true'); // decorative status pill
|
|
304
|
+
layer.appendChild(progressEl);
|
|
305
|
+
|
|
306
|
+
var dialogEl = doc.createElement('div');
|
|
307
|
+
dialogEl.className = 'ps-overlay-dialog';
|
|
308
|
+
dialogEl.style.zIndex = '30';
|
|
309
|
+
dialogEl.style.display = 'none'; // 'flex' when open (parity show/hide)
|
|
310
|
+
var dialogCardEl = doc.createElement('div');
|
|
311
|
+
dialogCardEl.className = 'ps-overlay-dialog-card';
|
|
312
|
+
dialogCardEl.setAttribute('role', 'status');
|
|
313
|
+
dialogCardEl.setAttribute('aria-live', 'polite');
|
|
314
|
+
var dialogIconEl = doc.createElement('div');
|
|
315
|
+
dialogIconEl.className = 'ps-overlay-dialog-icon';
|
|
316
|
+
dialogIconEl.innerHTML = ICON_SVG.alert; // default icon (alert parity)
|
|
317
|
+
var dialogTypeEl = doc.createElement('div');
|
|
318
|
+
dialogTypeEl.className = 'ps-overlay-dialog-type';
|
|
319
|
+
var dialogMessageEl = doc.createElement('div');
|
|
320
|
+
dialogMessageEl.className = 'ps-overlay-dialog-message';
|
|
321
|
+
dialogCardEl.appendChild(dialogIconEl);
|
|
322
|
+
dialogCardEl.appendChild(dialogTypeEl);
|
|
323
|
+
dialogCardEl.appendChild(dialogMessageEl);
|
|
324
|
+
dialogEl.appendChild(dialogCardEl);
|
|
325
|
+
layer.appendChild(dialogEl);
|
|
326
|
+
|
|
327
|
+
// --- Registry: ONE Map for every kind, built-ins included (D-09/D-10:
|
|
328
|
+
// glow/progress dispatch through the exact same path custom kinds do). ---
|
|
329
|
+
var registry = new Map();
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Register a renderer for an overlay kind. Overwrites silently, so hosts
|
|
333
|
+
* can replace a built-in if they want different visuals.
|
|
334
|
+
*
|
|
335
|
+
* renderFn contract (T-02-05): renderFn(payload, anchorRect, layer)
|
|
336
|
+
* receives the RAW payload value from the wire -- capture-side data is
|
|
337
|
+
* attacker-influenced, so custom renderFns own their escaping (write text
|
|
338
|
+
* via textContent like the built-ins do, never innerHTML). anchorRect is
|
|
339
|
+
* a host-document {top, left, width, height} rect or null (see
|
|
340
|
+
* handleOverlayMessage for resolution rules). On new-snapshot reset the
|
|
341
|
+
* renderFn is invoked with (null, null, layer) so it can clear its DOM.
|
|
342
|
+
* Errors thrown by a renderFn are contained (routed to the logger) and
|
|
343
|
+
* never break the message loop (T-02-07).
|
|
344
|
+
*
|
|
345
|
+
* @param {string} kind
|
|
346
|
+
* @param {(payload: *, anchorRect: ?Object, layer: Element) => void} renderFn
|
|
347
|
+
*/
|
|
348
|
+
function register(kind, renderFn) {
|
|
349
|
+
registry.set(kind, renderFn);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Containment wrapper for registry dispatch -- copies the
|
|
354
|
+
* safeSkipElement shape from the capture core (src/capture/index.js:
|
|
355
|
+
* 277-284): a throwing renderFn is routed to the injected logger and the
|
|
356
|
+
* kind loop continues; the message dispatch never throws (T-02-07).
|
|
357
|
+
* @param {string} kind
|
|
358
|
+
* @param {Function} renderFn
|
|
359
|
+
* @param {*} value
|
|
360
|
+
* @param {?Object} anchorRect
|
|
361
|
+
*/
|
|
362
|
+
function safeRenderOverlay(kind, renderFn, value, anchorRect) {
|
|
363
|
+
try {
|
|
364
|
+
renderFn(value, anchorRect, layer);
|
|
365
|
+
} catch (err) {
|
|
366
|
+
logger.error('[Renderer] overlay renderFn failed', kind, err);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Resolve a payload value to a host-document anchor rect. Priority per
|
|
372
|
+
* 02-RESEARCH.md Pattern 5:
|
|
373
|
+
* 1. value.nid -> ctx.resolveNidRect(nid) (mirrored element);
|
|
374
|
+
* a nid that no longer resolves yields null
|
|
375
|
+
* (stale anchor -- no coordinate fallback).
|
|
376
|
+
* 2. numeric x/y/w/h -> mapRectToHost(value, ctx.scale) (glow parity
|
|
377
|
+
* path, capture-page viewport coords).
|
|
378
|
+
* 3. neither -> null (fixed-position overlays).
|
|
379
|
+
* @param {Object} value
|
|
380
|
+
* @param {OverlayContext} ctx
|
|
381
|
+
* @returns {?{top: number, left: number, width: number, height: number}}
|
|
382
|
+
*/
|
|
383
|
+
function resolveAnchorRect(value, ctx) {
|
|
384
|
+
if (!value || typeof value !== 'object' || !ctx) return null;
|
|
385
|
+
if (value.nid !== undefined && value.nid !== null
|
|
386
|
+
&& typeof ctx.resolveNidRect === 'function') {
|
|
387
|
+
return ctx.resolveNidRect(value.nid) || null;
|
|
388
|
+
}
|
|
389
|
+
if (typeof value.x === 'number' && typeof value.y === 'number'
|
|
390
|
+
&& typeof value.w === 'number' && typeof value.h === 'number'
|
|
391
|
+
&& ctx.scale) {
|
|
392
|
+
return mapRectToHost(value, ctx.scale);
|
|
393
|
+
}
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// --- Built-in renderers (registered through the same Map as custom
|
|
398
|
+
// kinds). Each treats a null payload as "hide", which doubles as the
|
|
399
|
+
// reset contract every renderFn follows. ---
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Action-glow rect (reference dashboard.js:3379-3387). Visible only
|
|
403
|
+
* while glow.state === 'active' AND an anchor rect resolved; positioned
|
|
404
|
+
* via the mapped anchor rect.
|
|
405
|
+
* @param {?Object} value
|
|
406
|
+
* @param {?Object} anchorRect
|
|
407
|
+
*/
|
|
408
|
+
function renderGlow(value, anchorRect) {
|
|
409
|
+
if (value && value.state === 'active' && anchorRect) {
|
|
410
|
+
glowEl.style.top = anchorRect.top + 'px';
|
|
411
|
+
glowEl.style.left = anchorRect.left + 'px';
|
|
412
|
+
glowEl.style.width = anchorRect.width + 'px';
|
|
413
|
+
glowEl.style.height = anchorRect.height + 'px';
|
|
414
|
+
glowEl.style.display = 'block';
|
|
415
|
+
} else {
|
|
416
|
+
glowEl.style.display = 'none';
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Progress pill (reference dashboard.js:3390-3402). Exact parity text:
|
|
422
|
+
* determinate -> 'Math.round(percent)% - phase'; otherwise
|
|
423
|
+
* '(label || phase || Working) - (phase || Working)'. textContent only.
|
|
424
|
+
* @param {?Object} value
|
|
425
|
+
*/
|
|
426
|
+
function renderProgress(value) {
|
|
427
|
+
if (value) {
|
|
428
|
+
var phaseText = value.phase || 'Working';
|
|
429
|
+
var progressText;
|
|
430
|
+
if (value.mode === 'determinate' && typeof value.percent === 'number') {
|
|
431
|
+
progressText = Math.round(value.percent) + '%';
|
|
432
|
+
} else {
|
|
433
|
+
progressText = value.label || phaseText || 'Working';
|
|
434
|
+
}
|
|
435
|
+
progressEl.textContent = progressText + ' - ' + phaseText;
|
|
436
|
+
progressEl.style.display = 'block';
|
|
437
|
+
} else {
|
|
438
|
+
progressEl.style.display = 'none';
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
register('glow', renderGlow);
|
|
443
|
+
register('progress', renderProgress);
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Dispatch one STREAM.OVERLAY payload. Every own key except the identity
|
|
447
|
+
* keys (streamSessionId / snapshotId) is an overlay kind:
|
|
448
|
+
* - registered kind -> safeRenderOverlay(renderFn, value, anchorRect)
|
|
449
|
+
* (null values dispatch as null -> built-ins
|
|
450
|
+
* hide, custom kinds clear)
|
|
451
|
+
* - unregistered -> logged and ignored, never thrown (D-12,
|
|
452
|
+
* forward-compatible).
|
|
453
|
+
* @param {Object} payload
|
|
454
|
+
* @param {OverlayContext} ctx
|
|
455
|
+
*/
|
|
456
|
+
function handleOverlayMessage(payload, ctx) {
|
|
457
|
+
if (!payload || typeof payload !== 'object') return;
|
|
458
|
+
var kinds = Object.keys(payload);
|
|
459
|
+
for (var i = 0; i < kinds.length; i++) {
|
|
460
|
+
var kind = kinds[i];
|
|
461
|
+
// Identity keys stamp stream identity on the wire; they are reserved
|
|
462
|
+
// and never dispatched as kinds.
|
|
463
|
+
if (kind === 'streamSessionId' || kind === 'snapshotId') continue;
|
|
464
|
+
var renderFn = registry.get(kind);
|
|
465
|
+
if (!renderFn) {
|
|
466
|
+
logger.warn('[Renderer] unknown overlay kind ignored', kind);
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
var value = (payload[kind] === undefined) ? null : payload[kind];
|
|
470
|
+
var anchorRect = value ? resolveAnchorRect(value, ctx) : null;
|
|
471
|
+
safeRenderOverlay(kind, renderFn, value, anchorRect);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Set the dialog icon by dialog type (inline-SVG divergence from the
|
|
477
|
+
* reference's icon-font classes; unknown types fall back to alert).
|
|
478
|
+
* @param {string} type
|
|
479
|
+
*/
|
|
480
|
+
function setDialogIcon(type) {
|
|
481
|
+
dialogIconEl.innerHTML = ICON_SVG[type] || ICON_SVG.alert;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* STREAM.DIALOG handler (reference dashboard.js:3405-3443). Parity
|
|
486
|
+
* fallback: the dialog object may BE the payload (payload.dialog ||
|
|
487
|
+
* payload, dashboard.js:3407). Open -> capitalized type label (default
|
|
488
|
+
* 'Alert'), message via textContent (NEVER innerHTML -- T-02-04), icon by
|
|
489
|
+
* type, backdrop display 'flex'. Closed -> display 'none'. Other states
|
|
490
|
+
* are ignored (parity).
|
|
491
|
+
* @param {Object} payload
|
|
492
|
+
*/
|
|
493
|
+
function handleDialogMessage(payload) {
|
|
494
|
+
var dialog = (payload && payload.dialog) || payload;
|
|
495
|
+
if (!dialog) return;
|
|
496
|
+
if (dialog.state === 'open') {
|
|
497
|
+
var type = dialog.type || 'alert';
|
|
498
|
+
dialogTypeEl.textContent = type.charAt(0).toUpperCase() + type.slice(1);
|
|
499
|
+
dialogMessageEl.textContent = dialog.message || '';
|
|
500
|
+
setDialogIcon(type);
|
|
501
|
+
dialogEl.style.display = 'flex';
|
|
502
|
+
} else if (dialog.state === 'closed') {
|
|
503
|
+
dialogEl.style.display = 'none';
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* New-snapshot reset (D-13, reference dashboard.js:2762-2764): hide every
|
|
509
|
+
* overlay kind. Every registered renderFn -- built-ins and custom alike --
|
|
510
|
+
* is invoked with (null, null, layer) inside the containment wrapper
|
|
511
|
+
* (built-ins hide on null; custom kinds clear their DOM), then the dialog
|
|
512
|
+
* card is hidden.
|
|
513
|
+
*/
|
|
514
|
+
function resetOverlays() {
|
|
515
|
+
registry.forEach(function (renderFn, kind) {
|
|
516
|
+
safeRenderOverlay(kind, renderFn, null, null);
|
|
517
|
+
});
|
|
518
|
+
dialogEl.style.display = 'none';
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
return {
|
|
522
|
+
layer: layer,
|
|
523
|
+
register: register,
|
|
524
|
+
handleOverlayMessage: handleOverlayMessage,
|
|
525
|
+
handleDialogMessage: handleDialogMessage,
|
|
526
|
+
resetOverlays: resetOverlays
|
|
527
|
+
};
|
|
528
|
+
}
|