@spectosh/browser 0.2.5 → 0.3.1
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/README.md +3 -1
- package/dist/point-4P-cvF8S.cjs +252 -0
- package/dist/point-B5SXBYaU.js +1425 -0
- package/dist/react.cjs +1 -1
- package/dist/react.js +35 -28
- package/dist/specto-browser.cjs +1 -1
- package/dist/specto-browser.js +1 -2
- package/dist/src-BDf8wYoH.cjs +138 -0
- package/dist/{src-D-1ogCCr.js → src-mrKmVGmE.js} +1461 -1194
- package/package.json +7 -5
- package/src/index.js +19 -3
- package/src/point/context.js +61 -0
- package/src/point/describe.js +244 -0
- package/src/point/entry.js +475 -0
- package/src/point/index.js +859 -0
- package/src/point/trigger.js +198 -0
- package/src/point/voice.js +131 -0
- package/src/react.js +9 -3
- package/src/recorder.js +115 -28
- package/src/scrub.js +28 -0
- package/types/index.d.ts +42 -1
- package/types/react.d.ts +3 -1
- package/dist/src-DyiwhKUe.cjs +0 -138
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spectosh/browser",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Specto
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Specto for bundler users — session replay (rrweb DOM capture, console/network/error/rage/dead-click signals, privacy-masked in the browser) and Point, the cursor mode people use to mark what is broken.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -26,6 +26,8 @@
|
|
|
26
26
|
"import": "./dist/react.js",
|
|
27
27
|
"require": "./dist/react.cjs"
|
|
28
28
|
},
|
|
29
|
+
"./point": "./src/point/index.js",
|
|
30
|
+
"./point-trigger": "./src/point/trigger.js",
|
|
29
31
|
"./recorder": "./src/recorder.js",
|
|
30
32
|
"./recorder-worker": "./src/recorder.worker.js",
|
|
31
33
|
"./destination": {
|
|
@@ -45,7 +47,7 @@
|
|
|
45
47
|
"test": "node --test \"test/*.test.mjs\" && tsc -p test/types"
|
|
46
48
|
},
|
|
47
49
|
"dependencies": {
|
|
48
|
-
"rrweb": "^2.1.
|
|
50
|
+
"rrweb": "^2.1.7"
|
|
49
51
|
},
|
|
50
52
|
"peerDependencies": {
|
|
51
53
|
"react": ">=17"
|
|
@@ -59,8 +61,8 @@
|
|
|
59
61
|
"@types/react": "^19.2.0",
|
|
60
62
|
"react": "^19.2.0",
|
|
61
63
|
"react-dom": "^19.2.0",
|
|
62
|
-
"typescript": "^
|
|
63
|
-
"vite": "^8.3.
|
|
64
|
+
"typescript": "^7.0.2",
|
|
65
|
+
"vite": "^8.3.4"
|
|
64
66
|
},
|
|
65
67
|
"keywords": [
|
|
66
68
|
"session-replay",
|
package/src/index.js
CHANGED
|
@@ -15,11 +15,19 @@
|
|
|
15
15
|
// Start it before your app renders (your entry file) — capture begins at
|
|
16
16
|
// once, so the first requests and errors of a page load are in the replay.
|
|
17
17
|
// React: @spectosh/browser/react.
|
|
18
|
+
//
|
|
19
|
+
// Point, the cursor mode people use to mark what is broken (Settings →
|
|
20
|
+
// Point), comes with it: its gesture opens it, openPoint() opens it from
|
|
21
|
+
// your own UI. Its code is a separate chunk, loaded on first use.
|
|
22
|
+
// startRecording({ …, point: false }) leaves it out.
|
|
18
23
|
import { startRecording as start, isOptedOut, hasConsent, scrubText, SDK_VERSION } from './recorder.js'
|
|
19
24
|
import { DEFAULT_HOST, parseKey } from './destination.js'
|
|
20
25
|
import RecorderWorker from './recorder.worker.js?worker&inline'
|
|
26
|
+
import { installPoint } from './point/trigger.js'
|
|
21
27
|
|
|
22
28
|
let lastOpts = null
|
|
29
|
+
let point = null
|
|
30
|
+
const loadPoint = () => import('./point/index.js')
|
|
23
31
|
|
|
24
32
|
export function startRecording(opts = {}) {
|
|
25
33
|
if (!opts.key) throw new Error('@spectosh/browser: opts.key is required — your pk_… key from the project\'s Install page')
|
|
@@ -27,7 +35,11 @@ export function startRecording(opts = {}) {
|
|
|
27
35
|
// a key that names no host belongs to the hosted service — which must
|
|
28
36
|
// confirm it before anything is sent (an EU or self-hosted key without its
|
|
29
37
|
// host records nothing rather than reaching the wrong place)
|
|
30
|
-
|
|
38
|
+
const rec = start({ createWorker: () => new RecorderWorker(), _fallback: DEFAULT_HOST, _guessed: true, ...opts })
|
|
39
|
+
if (opts.point !== false && rec && !rec.disabled && !point) {
|
|
40
|
+
try { point = installPoint(rec, loadPoint, { launcher: opts.launcher }) } catch { /* Point is an extra: the recording goes on */ }
|
|
41
|
+
}
|
|
42
|
+
return rec
|
|
31
43
|
}
|
|
32
44
|
|
|
33
45
|
const current = () => (typeof window !== 'undefined' ? window.__spectoRec : null)
|
|
@@ -35,7 +47,11 @@ export const identify = (id, traits) => current()?.identify(id, traits)
|
|
|
35
47
|
export const track = (name, props) => current()?.track(name, props)
|
|
36
48
|
export const captureError = (error, info) => current()?.captureError?.(error, info)
|
|
37
49
|
export const sessionUrl = () => current()?.sessionUrl?.() || null
|
|
38
|
-
export const stopRecording = () => current()?.stop()
|
|
50
|
+
export const stopRecording = () => { point?.stop(); point = null; current()?.stop() }
|
|
51
|
+
// open Point from your own UI ("Report a problem")
|
|
52
|
+
export const openPoint = (via) => point?.open(via) ?? null
|
|
53
|
+
// the "Report a problem" button on this page: { position, offset: [x, y], label, color }, or false
|
|
54
|
+
export const setLauncher = (o) => point?.launcher(o)
|
|
39
55
|
|
|
40
56
|
// Consent mode: record (and store anything) only after the visitor agreed.
|
|
41
57
|
export function grantConsent() {
|
|
@@ -55,4 +71,4 @@ export function optIn() {
|
|
|
55
71
|
}
|
|
56
72
|
|
|
57
73
|
export { isOptedOut, hasConsent, scrubText, parseKey, DEFAULT_HOST, SDK_VERSION }
|
|
58
|
-
export default { startRecording, identify, track, captureError, sessionUrl, stopRecording, grantConsent, revokeConsent, optOut, optIn }
|
|
74
|
+
export default { startRecording, identify, track, captureError, sessionUrl, stopRecording, openPoint, setLauncher, grantConsent, revokeConsent, optOut, optIn }
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The context a marker carries (VISION.md §4.5), picked from the recorder's
|
|
2
|
+
// short memory (recorder.js → _point.recent()): the requests around the last
|
|
3
|
+
// time the person clicked this element (or the last few seconds when they
|
|
4
|
+
// never did), the console errors and warnings since the page loaded, and the
|
|
5
|
+
// interactions just before. Pure: times in epoch ms, `contains` tells whether
|
|
6
|
+
// a clicked element is the marked one or inside it.
|
|
7
|
+
|
|
8
|
+
const AROUND_BEFORE = 1000
|
|
9
|
+
const AROUND_AFTER = 6000
|
|
10
|
+
const RECENT_MS = 10_000
|
|
11
|
+
|
|
12
|
+
// the last click on (or inside) the marked element, within the last minute
|
|
13
|
+
export function lastClickOn(clicks, contains, now = Date.now()) {
|
|
14
|
+
for (let i = clicks.length - 1; i >= 0; i--) {
|
|
15
|
+
const c = clicks[i]
|
|
16
|
+
if (now - c.at > 60_000) break
|
|
17
|
+
try { if (c.el && contains(c.el)) return c.at } catch { /* detached */ }
|
|
18
|
+
}
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function contextFor({ recent, contains, now = Date.now() }) {
|
|
23
|
+
const clickedAt = lastClickOn(recent.clicks || [], contains, now)
|
|
24
|
+
const [from, to] = clickedAt ? [clickedAt - AROUND_BEFORE, clickedAt + AROUND_AFTER] : [now - RECENT_MS, now]
|
|
25
|
+
const network = (recent.network || [])
|
|
26
|
+
.filter((n) => n.at >= from && n.at <= to)
|
|
27
|
+
.slice(-20)
|
|
28
|
+
.map((n) => ({ at: n.at, method: n.method, url: n.url, status: n.status, dur: n.dur, ...(n.error ? { error: true } : {}) }))
|
|
29
|
+
const console = (recent.console || [])
|
|
30
|
+
.filter((c) => c.level === 'error' || c.level === 'warn')
|
|
31
|
+
.slice(-15)
|
|
32
|
+
.map((c) => ({ at: c.at, level: c.level, text: c.text }))
|
|
33
|
+
// JS errors are events, not console lines: they belong here too
|
|
34
|
+
const errors = (recent.event || [])
|
|
35
|
+
.filter((e) => e.type === 'error')
|
|
36
|
+
.slice(-5)
|
|
37
|
+
.map((e) => ({ at: e.at, level: 'error', text: e.message, stack: e.stack }))
|
|
38
|
+
const events = (recent.event || [])
|
|
39
|
+
.filter((e) => ['click', 'nav', 'rage', 'dead', 'custom'].includes(e.type) && e.at <= now && now - e.at < 120_000)
|
|
40
|
+
.slice(-15)
|
|
41
|
+
.map((e) => ({ at: e.at, type: e.type, target: e.sel || e.target || null, path: e.path || null, name: e.name || null }))
|
|
42
|
+
return { network, console: [...console, ...errors].sort((a, b) => a.at - b.at).slice(-20), events, clickedAt }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// where the person is: browser, OS, device class, locale, preferences
|
|
46
|
+
export function environment() {
|
|
47
|
+
const ua = navigator.userAgent || ''
|
|
48
|
+
const w = window.innerWidth
|
|
49
|
+
const touch = (navigator.maxTouchPoints || 0) > 0
|
|
50
|
+
const mq = (q) => { try { return window.matchMedia(q).matches } catch { return false } }
|
|
51
|
+
return {
|
|
52
|
+
browser: ua.slice(0, 200),
|
|
53
|
+
os: /Windows/.test(ua) ? 'Windows' : /Mac OS X|Macintosh/.test(ua) ? (touch ? 'iPadOS' : 'macOS') : /Android/.test(ua) ? 'Android' : /iPhone|iPad|iOS/.test(ua) ? 'iOS' : /Linux/.test(ua) ? 'Linux' : null,
|
|
54
|
+
device: w < 640 ? 'phone' : w < 1024 && touch ? 'tablet' : 'desktop',
|
|
55
|
+
locale: navigator.language || null,
|
|
56
|
+
tz: (() => { try { return Intl.DateTimeFormat().resolvedOptions().timeZone } catch { return null } })(),
|
|
57
|
+
reducedMotion: mq('(prefers-reduced-motion: reduce)'),
|
|
58
|
+
colorScheme: mq('(prefers-color-scheme: dark)') ? 'dark' : 'light',
|
|
59
|
+
viewport: { w, h: window.innerHeight },
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// What Point knows about an element (VISION.md §4.5): ways to find it again
|
|
2
|
+
// (selectors), the component and file it comes from when the framework says,
|
|
3
|
+
// a readable name, where it sits (breadcrumb), its state, the styles that
|
|
4
|
+
// usually explain a bug, and a masked snapshot of its markup. Masking follows
|
|
5
|
+
// the recording's rules: inputs never give their value, `specto-mask` and
|
|
6
|
+
// strict privacy hide text (except where the site shows it: showsText),
|
|
7
|
+
// sensitive attributes are dropped.
|
|
8
|
+
import { scrubText, showsText } from '../scrub.js'
|
|
9
|
+
|
|
10
|
+
const TEST_ATTRS = ['data-testid', 'data-test', 'data-test-id', 'data-cy', 'data-qa']
|
|
11
|
+
const STYLE_KEYS = ['display', 'visibility', 'opacity', 'pointerEvents', 'zIndex', 'overflow', 'cursor', 'color', 'background']
|
|
12
|
+
const KEEP_ATTRS = new Set(['id', 'class', 'role', 'type', 'name', 'href', 'disabled', 'aria-disabled', 'aria-expanded', 'aria-busy', 'aria-hidden', 'aria-pressed', 'aria-selected', 'aria-checked', 'for', 'data-specto-src', ...TEST_ATTRS])
|
|
13
|
+
const VOID = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr'])
|
|
14
|
+
|
|
15
|
+
const esc = (s) => String(s).replace(/\\/g, '\\\\').replace(/"/g, '\\"')
|
|
16
|
+
const tagOf = (el) => String(el?.tagName || '').toLowerCase()
|
|
17
|
+
const attr = (el, k) => { try { return el.getAttribute(k) } catch { return null } }
|
|
18
|
+
|
|
19
|
+
// an id a build or library generated is no way to find the element again
|
|
20
|
+
export const stableId = (id) => Boolean(id) && id.length <= 64 && !/\d{4,}|^:|:r\w*:|^(ember|react|radix|headlessui|mui|rc|el)[-_:]?\d/i.test(id) && /^[A-Za-z][\w-]*$/.test(id)
|
|
21
|
+
const stableClass = (c) => c && !c.startsWith('specto-') && c.length <= 40 && !/\d{3,}|^(css|sc|jsx|emotion|tw)-|__[A-Za-z0-9]{5,}$|^[a-z0-9]{6,}$/i.test(c)
|
|
22
|
+
|
|
23
|
+
export function isMasked(el, strict, unmask = '') {
|
|
24
|
+
const t = tagOf(el)
|
|
25
|
+
if (t === 'input' || t === 'textarea' || t === 'select' || attr(el, 'contenteditable') === 'true') return true
|
|
26
|
+
if (strict) return !showsText(el, unmask)
|
|
27
|
+
for (let n = el; n && n.nodeType === 1; n = n.parentElement) {
|
|
28
|
+
if (n.classList?.contains('specto-mask') || n.classList?.contains('specto-block')) return true
|
|
29
|
+
}
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function testIdOf(el) {
|
|
34
|
+
for (const a of TEST_ATTRS) { const v = attr(el, a); if (v) return [a, v] }
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// one level of a CSS path: tag#id, tag[data-testid], tag.class, or with
|
|
39
|
+
// :nth-of-type when siblings share it
|
|
40
|
+
function step(el) {
|
|
41
|
+
const tag = tagOf(el)
|
|
42
|
+
const id = el.id
|
|
43
|
+
if (stableId(id)) return { sel: `${tag}#${id}`, anchor: true }
|
|
44
|
+
const tid = testIdOf(el)
|
|
45
|
+
if (tid) return { sel: `${tag}[${tid[0]}="${esc(tid[1])}"]`, anchor: true }
|
|
46
|
+
const cls = typeof el.className === 'string' ? el.className.trim().split(/\s+/).filter(stableClass).slice(0, 2) : []
|
|
47
|
+
let sel = cls.length ? `${tag}.${cls.join('.')}` : tag
|
|
48
|
+
const parent = el.parentElement
|
|
49
|
+
if (parent) {
|
|
50
|
+
const same = Array.from(parent.children || []).filter((c) => tagOf(c) === tag)
|
|
51
|
+
if (same.length > 1) sel += `:nth-of-type(${same.indexOf(el) + 1})`
|
|
52
|
+
}
|
|
53
|
+
return { sel, anchor: false }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function cssPath(el, max = 5) {
|
|
57
|
+
const parts = []
|
|
58
|
+
for (let n = el, i = 0; n && n.nodeType === 1 && i < max && tagOf(n) !== 'html' && tagOf(n) !== 'body'; n = n.parentElement, i++) {
|
|
59
|
+
const s = step(n)
|
|
60
|
+
parts.unshift(s.sel)
|
|
61
|
+
if (s.anchor) break
|
|
62
|
+
}
|
|
63
|
+
return parts.join(' > ') || tagOf(el) || 'body'
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// the accessible-ish name: what a person would call it
|
|
67
|
+
export function accessibleName(el, masked) {
|
|
68
|
+
const label = attr(el, 'aria-label') || attr(el, 'title') || attr(el, 'alt')
|
|
69
|
+
if (label && !masked) return scrubText(label).trim().slice(0, 80)
|
|
70
|
+
const labelledBy = attr(el, 'aria-labelledby')
|
|
71
|
+
if (labelledBy && !masked) {
|
|
72
|
+
try { const t = document.getElementById(labelledBy)?.textContent; if (t) return scrubText(t).trim().slice(0, 80) } catch { /* no document */ }
|
|
73
|
+
}
|
|
74
|
+
if (masked) return ''
|
|
75
|
+
const text = (el.innerText ?? el.textContent ?? '').replace(/\s+/g, ' ').trim()
|
|
76
|
+
return scrubText(text).slice(0, 80)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function roleOf(el) {
|
|
80
|
+
const r = attr(el, 'role')
|
|
81
|
+
if (r) return r
|
|
82
|
+
const t = tagOf(el)
|
|
83
|
+
if (t === 'a' && attr(el, 'href') != null) return 'link'
|
|
84
|
+
if (t === 'button' || (t === 'input' && ['submit', 'button', 'reset'].includes(attr(el, 'type')))) return 'button'
|
|
85
|
+
if (t === 'input') return attr(el, 'type') === 'checkbox' ? 'checkbox' : attr(el, 'type') === 'radio' ? 'radio' : 'textbox'
|
|
86
|
+
if (t === 'select') return 'combobox'
|
|
87
|
+
if (t === 'textarea') return 'textbox'
|
|
88
|
+
if (/^h[1-6]$/.test(t)) return 'heading'
|
|
89
|
+
if (t === 'img') return 'img'
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// several ways to find it, best first
|
|
94
|
+
export function selectorsOf(el, { masked } = {}) {
|
|
95
|
+
const out = []
|
|
96
|
+
const tid = testIdOf(el)
|
|
97
|
+
if (tid) out.push(`[${tid[0]}="${esc(tid[1])}"]`)
|
|
98
|
+
if (stableId(el.id)) out.push(`#${el.id}`)
|
|
99
|
+
const role = roleOf(el)
|
|
100
|
+
const name = accessibleName(el, masked)
|
|
101
|
+
if (role && name && name.length <= 40) out.push(`role=${role}[name="${esc(name)}"]`)
|
|
102
|
+
out.push(cssPath(el))
|
|
103
|
+
return [...new Set(out)].slice(0, 6)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// The component the element belongs to, and the file when the build says:
|
|
107
|
+
// React (fiber), Vue 3, Svelte, or the Specto build plugin's data-specto-src.
|
|
108
|
+
export function componentOf(el) {
|
|
109
|
+
let source = null
|
|
110
|
+
for (let n = el; n && n.nodeType === 1 && !source; n = n.parentElement) source = attr(n, 'data-specto-src')
|
|
111
|
+
// React: the fiber hangs off the DOM node under a randomised key
|
|
112
|
+
const fiberKey = el && Object.keys(el).find((k) => k.startsWith('__reactFiber$') || k.startsWith('__reactInternalInstance$'))
|
|
113
|
+
if (fiberKey) {
|
|
114
|
+
for (let f = el[fiberKey], i = 0; f && i < 60; f = f.return, i++) {
|
|
115
|
+
const type = f.type
|
|
116
|
+
if (!type || typeof type === 'string') continue
|
|
117
|
+
const name = type.displayName || type.name || type.render?.displayName || type.render?.name || type.type?.displayName || type.type?.name
|
|
118
|
+
if (name && /^[A-Z]/.test(name) && !/^(Fragment|Suspense|StrictMode|Profiler|Provider|Consumer|Context)/.test(name)) {
|
|
119
|
+
const ds = f._debugSource
|
|
120
|
+
if (!source && ds?.fileName) source = `${String(ds.fileName).replace(/^.*?\/(src|app|pages|components)\//, '$1/')}:${ds.lineNumber}`
|
|
121
|
+
return { component: name.slice(0, 120), source }
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// Vue 3
|
|
126
|
+
for (let n = el, i = 0; n && i < 20; n = n.parentElement, i++) {
|
|
127
|
+
const c = n.__vueParentComponent
|
|
128
|
+
if (c) {
|
|
129
|
+
const name = c.type?.name || c.type?.__name || (c.type?.__file && String(c.type.__file).split('/').pop().replace(/\.vue$/, ''))
|
|
130
|
+
if (name) return { component: name, source: source || (c.type?.__file ? String(c.type.__file).replace(/^.*?\/src\//, 'src/') : null) }
|
|
131
|
+
}
|
|
132
|
+
const svelte = n.__svelte_meta
|
|
133
|
+
if (svelte?.loc?.file) return { component: String(svelte.loc.file).split('/').pop().replace(/\.svelte$/, ''), source: source || `${svelte.loc.file}:${svelte.loc.line}` }
|
|
134
|
+
}
|
|
135
|
+
return { component: null, source }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// where it sits: the closest landmarks and headings, outermost first
|
|
139
|
+
export function breadcrumbOf(el) {
|
|
140
|
+
const out = []
|
|
141
|
+
for (let n = el.parentElement, i = 0; n && n.nodeType === 1 && i < 25 && out.length < 6; n = n.parentElement, i++) {
|
|
142
|
+
const t = tagOf(n)
|
|
143
|
+
if (t === 'body' || t === 'html') break
|
|
144
|
+
const role = attr(n, 'role')
|
|
145
|
+
const label = attr(n, 'aria-label')
|
|
146
|
+
if (['main', 'nav', 'header', 'footer', 'aside', 'form', 'dialog', 'section', 'table'].includes(t) || ['dialog', 'navigation', 'form', 'region', 'tabpanel', 'menu'].includes(role) || stableId(n.id) || testIdOf(n)) {
|
|
147
|
+
out.unshift(`${t}${stableId(n.id) ? `#${n.id}` : ''}${label ? `[${scrubText(label).slice(0, 30)}]` : ''}`)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return out
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function stateOf(el) {
|
|
154
|
+
const s = {}
|
|
155
|
+
const t = tagOf(el)
|
|
156
|
+
if (el.disabled === true || attr(el, 'aria-disabled') === 'true') s.disabled = true
|
|
157
|
+
else if (['button', 'input', 'select', 'textarea'].includes(t)) s.disabled = false
|
|
158
|
+
if (t === 'input' && ['checkbox', 'radio'].includes(attr(el, 'type'))) s.checked = Boolean(el.checked)
|
|
159
|
+
const exp = attr(el, 'aria-expanded')
|
|
160
|
+
if (exp != null) s.expanded = exp === 'true'
|
|
161
|
+
if (attr(el, 'aria-busy') === 'true') s.busy = true
|
|
162
|
+
return Object.keys(s).length ? s : undefined
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function stylesOf(el) {
|
|
166
|
+
try {
|
|
167
|
+
const cs = getComputedStyle(el)
|
|
168
|
+
const out = {}
|
|
169
|
+
for (const k of STYLE_KEYS) {
|
|
170
|
+
const v = k === 'background' ? cs.backgroundColor : cs[k]
|
|
171
|
+
if (v != null && v !== '') out[k] = String(v).slice(0, 80)
|
|
172
|
+
}
|
|
173
|
+
return out
|
|
174
|
+
} catch { return {} }
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// a compact, masked snapshot of the element's markup (agents read it)
|
|
178
|
+
export function snapshotOf(el, { strict = false, unmask = '', maxNodes = 60, maxDepth = 4, maxChars = 4000 } = {}) {
|
|
179
|
+
let nodes = 0
|
|
180
|
+
const walk = (n, depth, masked) => {
|
|
181
|
+
if (nodes++ > maxNodes) return '…'
|
|
182
|
+
if (n.nodeType === 3) {
|
|
183
|
+
const t = String(n.textContent || '').replace(/\s+/g, ' ')
|
|
184
|
+
if (!t.trim()) return t ? ' ' : ''
|
|
185
|
+
return masked ? t.replace(/\S/g, '•').slice(0, 60) : scrubText(t).slice(0, 120).replace(/</g, '<')
|
|
186
|
+
}
|
|
187
|
+
if (n.nodeType !== 1) return ''
|
|
188
|
+
const tag = tagOf(n)
|
|
189
|
+
if (['script', 'style', 'noscript', 'template', 'svg'].includes(tag)) return tag === 'svg' ? '<svg/>' : ''
|
|
190
|
+
// strict: each element by itself (a shown element may sit in a masked one)
|
|
191
|
+
const m = strict ? isMasked(n, true, unmask) || Boolean(n.closest?.('[contenteditable="true"]')) : masked || isMasked(n, false)
|
|
192
|
+
const attrs = []
|
|
193
|
+
for (const a of Array.from(n.attributes || [])) {
|
|
194
|
+
if (!KEEP_ATTRS.has(a.name)) continue
|
|
195
|
+
let v = a.value
|
|
196
|
+
if (a.name === 'href') { try { v = new URL(v, location.href).pathname } catch { v = '' } }
|
|
197
|
+
if (a.name === 'class') v = v.split(/\s+/).filter((c) => c && !c.startsWith('specto-')).slice(0, 4).join(' ')
|
|
198
|
+
attrs.push(v === '' ? a.name : `${a.name}="${scrubText(v).slice(0, 80).replace(/"/g, '"')}"`)
|
|
199
|
+
}
|
|
200
|
+
if (tag === 'input') attrs.push('value="[masked]"')
|
|
201
|
+
const open = `<${tag}${attrs.length ? ' ' + attrs.join(' ') : ''}>`
|
|
202
|
+
if (VOID.has(tag)) return open
|
|
203
|
+
if (depth >= maxDepth) return `${open}…</${tag}>`
|
|
204
|
+
let inner = ''
|
|
205
|
+
for (const c of Array.from(n.childNodes || [])) {
|
|
206
|
+
inner += walk(c, depth + 1, m)
|
|
207
|
+
if (inner.length > maxChars) break
|
|
208
|
+
}
|
|
209
|
+
return `${open}${inner}</${tag}>`
|
|
210
|
+
}
|
|
211
|
+
return walk(el, 0, isMasked(el, strict, unmask)).slice(0, maxChars)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// the chip next to the cursor: component › test id › selector
|
|
215
|
+
export function chipOf(el, d) {
|
|
216
|
+
const tid = testIdOf(el)
|
|
217
|
+
return [d.component, tid ? tid[1] : null, d.name || tagOf(el)].filter(Boolean).slice(0, 2).join(' › ') || tagOf(el)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// everything at once, for a marker
|
|
221
|
+
export function describe(el, { strict = false, unmask = '' } = {}) {
|
|
222
|
+
const masked = isMasked(el, strict, unmask)
|
|
223
|
+
const { component, source } = componentOf(el)
|
|
224
|
+
const name = accessibleName(el, masked)
|
|
225
|
+
const role = roleOf(el)
|
|
226
|
+
let rect = null
|
|
227
|
+
try { const r = el.getBoundingClientRect(); rect = { x: Math.round(r.left), y: Math.round(r.top), w: Math.round(r.width), h: Math.round(r.height) } } catch { /* detached */ }
|
|
228
|
+
const d = {
|
|
229
|
+
tag: tagOf(el),
|
|
230
|
+
role,
|
|
231
|
+
label: masked ? null : (attr(el, 'aria-label') ? scrubText(attr(el, 'aria-label')).slice(0, 120) : null),
|
|
232
|
+
name: name ? `${name}${role ? ` ${role}` : ''}` : (role || tagOf(el)),
|
|
233
|
+
component,
|
|
234
|
+
source,
|
|
235
|
+
selectors: selectorsOf(el, { masked }),
|
|
236
|
+
path: breadcrumbOf(el),
|
|
237
|
+
rect,
|
|
238
|
+
text: masked ? null : (name || null),
|
|
239
|
+
state: stateOf(el),
|
|
240
|
+
styles: stylesOf(el),
|
|
241
|
+
snapshot: snapshotOf(el, { strict, unmask }),
|
|
242
|
+
}
|
|
243
|
+
return d
|
|
244
|
+
}
|