chromewright 0.8.0

Browser automation MCP server via Chrome DevTools Protocol (CDP)
Documentation
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
// Hydrated DOM semantic capture for the tui-gated SemanticDocument path.
// Separate from extract_dom.js (ARIA actionability). Avoids layout/geometry
// queries (no getBoundingClientRect); uses cheap visibility checks so client
// filters (e.g. Holmes toggling Tailwind `.hidden` / display:none) drop from
// the markdown view.
JSON.stringify((function() {
    'use strict';

    var MAX_COMPONENTS = 10000;
    var MAX_DEPTH = 64;
    var MAX_STRING_CHARS = 4096;
    var MAX_SELECT_OPTIONS = 256;
    var MAX_TOTAL_TEXT_CHARS = 1000000;

    var componentCount = 0;
    var totalTextChars = 0;
    var truncated = false;

    function getDocumentView(doc) {
        return doc.defaultView || window;
    }

    function generateDocumentId(doc) {
        var view = getDocumentView(doc);
        if (view.crypto && typeof view.crypto.randomUUID === 'function') {
            return view.crypto.randomUUID();
        }
        return 'doc-' + Math.random().toString(36).slice(2, 12);
    }

    function ensureDocumentState(doc) {
        var view = getDocumentView(doc);
        if (!view.__browserUseDocumentState) {
            var state = {
                documentId: generateDocumentId(doc),
                revision: 1,
                frameTrackerListeners: []
            };
            var observer = new view.MutationObserver(function() {
                state.revision += 1;
            });
            observer.observe(doc, {
                subtree: true,
                childList: true,
                attributes: true,
                characterData: true
            });
            state.observer = observer;
            view.__browserUseDocumentState = state;
        }
        return view.__browserUseDocumentState;
    }

    function observeOpenShadowRoot(root) {
        if (!root) return;
        var state = ensureDocumentState(root.ownerDocument);
        if (!state.observer) return;
        if (!state.semanticShadowRoots) {
            state.semanticShadowRoots = new WeakSet();
        }
        if (state.semanticShadowRoots.has(root)) return;
        state.observer.observe(root, {
            subtree: true,
            childList: true,
            attributes: true,
            characterData: true
        });
        state.semanticShadowRoots.add(root);
    }

    function clipString(value) {
        if (value == null) {
            return null;
        }
        var text = String(value);
        if (text.length > MAX_STRING_CHARS) {
            truncated = true;
            text = text.slice(0, MAX_STRING_CHARS);
        }
        totalTextChars += text.length;
        if (totalTextChars > MAX_TOTAL_TEXT_CHARS) {
            truncated = true;
        }
        return text;
    }

    function visibleText(element) {
        if (!element) {
            return '';
        }
        var text = element.innerText || element.textContent || '';
        return text.replace(/\s+/g, ' ').trim();
    }

    function directText(element) {
        var parts = [];
        for (var child = element.firstChild; child; child = child.nextSibling) {
            if (child.nodeType === 3) {
                var value = String(child.nodeValue || '').replace(/\s+/g, ' ').trim();
                if (value) {
                    parts.push(value);
                }
            }
        }
        return parts.join(' ').trim();
    }

    function landmarkRole(tag, role) {
        var normalized = (role || '').toLowerCase();
        if (normalized === 'main' || normalized === 'complementary' || normalized === 'banner' ||
            normalized === 'navigation' || normalized === 'region' || normalized === 'contentinfo') {
            if (normalized === 'complementary') return 'aside';
            if (normalized === 'banner') return 'header';
            if (normalized === 'navigation') return 'nav';
            if (normalized === 'region') return 'section';
            if (normalized === 'contentinfo') return 'footer';
            return normalized;
        }
        switch (tag) {
            case 'MAIN': return 'main';
            case 'ASIDE': return 'aside';
            case 'HEADER': return 'header';
            case 'NAV': return 'nav';
            case 'SECTION': return 'section';
            case 'FOOTER': return 'footer';
            default: return null;
        }
    }

    function isLandmark(tag, role) {
        return landmarkRole(tag, role) != null;
    }

    function isHeading(tag) {
        return tag === 'H1' || tag === 'H2' || tag === 'H3' ||
            tag === 'H4' || tag === 'H5' || tag === 'H6';
    }

    function headingLevel(tag) {
        return Number(tag.charAt(1));
    }

    function isSemanticElement(element) {
        var tag = element.tagName;
        var role = element.getAttribute('role');
        if (isLandmark(tag, role)) return true;
        if (isHeading(tag)) return true;
        if (tag === 'P' || tag === 'BLOCKQUOTE' || tag === 'PRE' || tag === 'FIGCAPTION') return true;
        if (tag === 'OL' || tag === 'UL' || tag === 'LI') return true;
        // Named anchors and href links are both semantic (fragment targets).
        if (tag === 'A' && (element.hasAttribute('href') || element.getAttribute('name'))) return true;
        if (tag === 'IMG') return true;
        if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || tag === 'BUTTON') return true;
        if (tag === 'ARTICLE' || tag === 'FIGURE' || tag === 'FIELDSET' || tag === 'FORM' || tag === 'DETAILS') {
            return true;
        }
        // Id-bearing wrappers become groups so #id targets remain addressable.
        if (element.id) return true;
        if (role === 'group' || role === 'list' || role === 'listitem' || role === 'link' ||
            role === 'button' || role === 'img' || role === 'textbox' || role === 'combobox') {
            return true;
        }
        return false;
    }

    function isSkippable(element) {
        var tag = element.tagName;
        if (tag === 'SCRIPT' || tag === 'STYLE' || tag === 'NOSCRIPT' ||
            tag === 'TEMPLATE' || tag === 'SVG' || tag === 'HEAD' || tag === 'META' ||
            tag === 'LINK' || tag === 'BR' || tag === 'HR') {
            return true;
        }
        // Client-side filters (Holmes, Alpine, etc.) hide non-matches with
        // [hidden], aria-hidden, or display:none / visibility:hidden. Without
        // this, the TUI keeps showing the full unfiltered list after search.
        if (isEffectivelyHidden(element)) {
            return true;
        }
        return false;
    }

    // True when the element is not presented to the user (no geometry probes).
    function isEffectivelyHidden(element) {
        if (!element || element.nodeType !== 1) {
            return false;
        }
        // HTML hidden property / attribute
        if (element.hidden === true) {
            return true;
        }
        if (element.getAttribute('hidden') != null) {
            return true;
        }
        if (element.getAttribute('aria-hidden') === 'true') {
            return true;
        }
        // Inline styles (common for scripted hide without class)
        var inline = element.style;
        if (inline) {
            if (inline.display === 'none' || inline.visibility === 'hidden') {
                return true;
            }
        }
        // Computed style: Tailwind `.hidden` and similar utility classes.
        try {
            var view = getDocumentView(element.ownerDocument || document);
            if (view && typeof view.getComputedStyle === 'function') {
                var style = view.getComputedStyle(element);
                if (style) {
                    if (style.display === 'none' || style.visibility === 'hidden') {
                        return true;
                    }
                }
            }
        } catch (error) {
            // Cross-origin / detached — treat as not hidden.
        }
        return false;
    }

    function collectSelectOptions(select) {
        var options = [];
        var list = select.options || [];
        for (var i = 0; i < list.length && options.length < MAX_SELECT_OPTIONS; i += 1) {
            var option = list[i];
            options.push({
                value: clipString(option.value || '') || '',
                label: clipString(option.label || option.text || '') || null,
                selected: !!option.selected,
                disabled: !!option.disabled
            });
        }
        if (list.length > MAX_SELECT_OPTIONS) {
            truncated = true;
        }
        return options;
    }

    function controlLabel(element) {
        if (element.id) {
            try {
                var label = document.querySelector('label[for="' + CSS.escape(element.id) + '"]');
                if (label) {
                    return clipString(visibleText(label));
                }
            } catch (error) {
                // CSS.escape may be unavailable; fall through.
            }
        }
        var aria = element.getAttribute('aria-label');
        if (aria) {
            return clipString(aria);
        }
        var labelledBy = element.getAttribute('aria-labelledby');
        if (labelledBy) {
            var parts = [];
            var ids = labelledBy.split(/\s+/);
            for (var i = 0; i < ids.length; i += 1) {
                var ref = document.getElementById(ids[i]);
                if (ref) {
                    parts.push(visibleText(ref));
                }
            }
            if (parts.length) {
                return clipString(parts.join(' '));
            }
        }
        if (element.labels && element.labels.length) {
            return clipString(visibleText(element.labels[0]));
        }
        return null;
    }

    function normalizeTextChunk(value) {
        if (value == null) {
            return '';
        }
        return String(value).replace(/\s+/g, ' ').trim();
    }

    function makeTextFragment(text) {
        var clipped = clipString(normalizeTextChunk(text));
        if (!clipped) {
            return null;
        }
        if (componentCount >= MAX_COMPONENTS || totalTextChars > MAX_TOTAL_TEXT_CHARS) {
            truncated = true;
            return null;
        }
        componentCount += 1;
        return {
            kind: 'text',
            tag: null,
            id: null,
            text: clipped,
            children: []
        };
    }

    function isPlainTextFragment(node) {
        return !!(
            node &&
            node.kind === 'text' &&
            (!node.children || node.children.length === 0) &&
            !node.href &&
            !node.landmark &&
            node.heading_level == null
        );
    }

    // Collapse runs of plain text-only ordered content into one compact leaf string.
    // Mixed semantic content is left as an ordered child list without aggregate text.
    function summarizeOrderedContent(ordered) {
        if (!ordered.length) {
            return { mode: 'empty' };
        }

        var parts = [];
        for (var i = 0; i < ordered.length; i += 1) {
            if (!isPlainTextFragment(ordered[i])) {
                return { mode: 'mixed', children: ordered };
            }
            if (ordered[i].text) {
                parts.push(ordered[i].text);
            }
        }

        return {
            mode: 'leaf',
            text: parts.join(' ')
        };
    }

    // Document-order walk: text nodes become Text fragments; semantic elements
    // become components; generic wrappers are unwrapped while retaining direct text.
    function visitOrdered(element, depth) {
        var children = [];
        if (depth > MAX_DEPTH) {
            truncated = true;
            return children;
        }

        function pushNodes(nodes) {
            for (var i = 0; i < nodes.length; i += 1) {
                if (nodes[i]) {
                    children.push(nodes[i]);
                }
            }
        }

        function visitChildNode(child) {
            if (!child) {
                return;
            }
            if (child.nodeType === 3) {
                var fragment = makeTextFragment(child.nodeValue);
                if (fragment) {
                    children.push(fragment);
                }
                return;
            }
            if (child.nodeType !== 1) {
                return;
            }
            if (child.assignedSlot) {
                return;
            }
            if (isSkippable(child)) {
                return;
            }
            if (!isSemanticElement(child)) {
                pushNodes(visitOrdered(child, depth + 1));
                return;
            }
            pushNodes(visit(child, depth));
        }

        for (var child = element.firstChild; child; child = child.nextSibling) {
            visitChildNode(child);
        }

        if (element.shadowRoot) {
            observeOpenShadowRoot(element.shadowRoot);
            for (var shadowChild = element.shadowRoot.firstChild; shadowChild; shadowChild = shadowChild.nextSibling) {
                visitChildNode(shadowChild);
            }
        }

        if (element.tagName === 'SLOT') {
            var assigned = element.assignedNodes();
            for (var j = 0; j < assigned.length; j += 1) {
                visitChildNode(assigned[j]);
            }
        }

        return children;
    }

    function makeNode(kind, element, fields, children) {
        if (componentCount >= MAX_COMPONENTS || totalTextChars > MAX_TOTAL_TEXT_CHARS) {
            truncated = true;
            return null;
        }
        componentCount += 1;

        var selector = null;
        if (kind === 'link' || kind === 'input' || kind === 'textarea' ||
            kind === 'select' || kind === 'button') {
            selector = buildInteractionSelector(element);
            if (selector) {
                totalTextChars += selector.length;
                if (totalTextChars > MAX_TOTAL_TEXT_CHARS) {
                    truncated = true;
                    return null;
                }
            }
        }

        var node = {
            kind: kind,
            tag: element.tagName.toLowerCase(),
            id: element.id ? clipString(element.id) : null,
            selector: selector,
            children: children || []
        };

        if (fields) {
            for (var key in fields) {
                if (Object.prototype.hasOwnProperty.call(fields, key) && fields[key] != null) {
                    node[key] = fields[key];
                }
            }
        }

        return node;
    }

    // Mint an exact, capture-scoped locator without relying on text, classes,
    // or mutable form values. Each JSON segment resolves uniquely inside the
    // document or an open shadow root; the next segment enters that host's
    // shadow root. Closed roots remain intentionally inaccessible.
    function buildInteractionSelector(element) {
        if (!element) return null;

        var segments = [];
        var target = element;
        while (target) {
            var root = target.getRootNode();
            if (!root || !root.querySelectorAll) return null;
            var selector = selectorWithinRoot(target, root);
            if (!selector) return null;
            segments.push(selector);
            if (root === document) break;
            if (!root.host) return null;
            target = root.host;
        }
        segments.reverse();
        var encoded = JSON.stringify(segments);
        return encoded.length <= MAX_STRING_CHARS ? encoded : null;
    }

    function selectorWithinRoot(element, root) {
        var path = [];
        var current = element;
        while (current && current.nodeType === 1) {
            var parent = current.parentElement;
            var tag = current.tagName.toLowerCase();
            if (parent) {
                var index = Array.prototype.indexOf.call(parent.children, current) + 1;
                if (index <= 0) return null;
                tag += ':nth-child(' + index + ')';
            } else if (current.parentNode === root && root.children) {
                var rootIndex = Array.prototype.indexOf.call(root.children, current) + 1;
                if (rootIndex <= 0) return null;
                tag += ':nth-child(' + rootIndex + ')';
            }
            path.unshift(tag);
            if (!parent) break;
            current = parent;
        }

        var selector = path.join(' > ');
        if (!selector || selector.length > MAX_STRING_CHARS) return null;
        try {
            var matches = root.querySelectorAll(selector);
            return matches.length === 1 && matches[0] === element ? selector : null;
        } catch (_) {
            return null;
        }
    }

    // Textual containers: leaf when only text; ordered children without aggregate
    // text when nested semantic descendants are present.
    function makeTextualContainer(kind, element, depth, extraFields) {
        var ordered = visitOrdered(element, depth + 1);
        var summary = summarizeOrderedContent(ordered);
        var fields = extraFields || {};

        if (summary.mode === 'empty') {
            return null;
        }

        if (summary.mode === 'leaf') {
            // Drop provisional text-fragment nodes; only the compact container remains.
            for (var i = 0; i < ordered.length; i += 1) {
                componentCount -= 1;
                if (ordered[i].text) {
                    totalTextChars = Math.max(0, totalTextChars - ordered[i].text.length);
                }
            }
            // summary.text is already clipped via fragment construction.
            fields.text = summary.text;
            if (kind === 'heading' || kind === 'list_item') {
                fields.label = summary.text;
            }
            // Re-apply string budget for the retained container text once.
            totalTextChars += summary.text.length;
            return makeNode(kind, element, fields, []);
        }

        // Mixed: ordered children are authoritative; never keep aggregate innerText.
        return makeNode(kind, element, fields, summary.children);
    }

    function visit(element, depth) {
        if (!element || element.nodeType !== 1) {
            return [];
        }
        if (isSkippable(element)) {
            return [];
        }
        if (componentCount >= MAX_COMPONENTS || totalTextChars > MAX_TOTAL_TEXT_CHARS) {
            truncated = true;
            return [];
        }
        if (depth > MAX_DEPTH) {
            truncated = true;
            return [];
        }

        var tag = element.tagName;
        var role = element.getAttribute('role');

        // Generic layout wrappers: unwrap, keeping ordered text + semantic children.
        if (!isSemanticElement(element)) {
            return visitOrdered(element, depth + 1);
        }

        var children;
        var node = null;

        if (isLandmark(tag, role)) {
            children = visitOrdered(element, depth + 1);
            node = makeNode('landmark', element, {
                landmark: landmarkRole(tag, role),
                label: clipString(element.getAttribute('aria-label') || '')
            }, children);
        } else if (isHeading(tag)) {
            node = makeTextualContainer('heading', element, depth, {
                heading_level: headingLevel(tag)
            });
        } else if (tag === 'P' || tag === 'BLOCKQUOTE' || tag === 'PRE' || tag === 'FIGCAPTION') {
            node = makeTextualContainer('text', element, depth, {});
        } else if (tag === 'OL' || tag === 'UL' || role === 'list') {
            children = visitOrdered(element, depth + 1);
            node = makeNode('list', element, {
                ordered: tag === 'OL'
            }, children);
        } else if (tag === 'LI' || role === 'listitem') {
            node = makeTextualContainer('list_item', element, depth, {});
        } else if (tag === 'A' || role === 'link') {
            // Terminal leaf: aggregate label only; never fragment descendants.
            // Named anchors (name= without href) are still links for fragment targets.
            var linkText = clipString(visibleText(element));
            var hrefAttr = element.getAttribute('href');
            var nameAttr = element.getAttribute('name');
            node = makeNode('link', element, {
                href: hrefAttr != null ? clipString(hrefAttr) : '',
                name: nameAttr ? clipString(nameAttr) : null,
                text: linkText,
                label: linkText || (nameAttr ? clipString(nameAttr) : null)
            }, []);
        } else if (tag === 'IMG' || role === 'img') {
            node = makeNode('image', element, {
                src: clipString(element.getAttribute('src') || ''),
                alt: clipString(element.getAttribute('alt') || ''),
                label: clipString(element.getAttribute('alt') || element.getAttribute('aria-label') || '')
            }, []);
        } else if (tag === 'INPUT') {
            var inputType = (element.getAttribute('type') || 'text').toLowerCase();
            if (inputType === 'hidden') {
                return [];
            }
            node = makeNode('input', element, {
                input_type: clipString(inputType),
                name: clipString(element.getAttribute('name') || ''),
                value: clipString(element.value != null ? element.value : element.getAttribute('value') || ''),
                placeholder: clipString(element.getAttribute('placeholder') || ''),
                checked: element.checked ? true : null,
                disabled: element.disabled ? true : null,
                required: element.required ? true : null,
                readonly: element.readOnly ? true : null,
                label: controlLabel(element)
            }, []);
        } else if (tag === 'TEXTAREA' || role === 'textbox') {
            node = makeNode('textarea', element, {
                name: clipString(element.getAttribute('name') || ''),
                value: clipString(element.value != null ? element.value : ''),
                placeholder: clipString(element.getAttribute('placeholder') || ''),
                disabled: element.disabled ? true : null,
                required: element.required ? true : null,
                readonly: element.readOnly ? true : null,
                label: controlLabel(element)
            }, []);
        } else if (tag === 'SELECT' || role === 'combobox' || role === 'listbox') {
            node = makeNode('select', element, {
                name: clipString(element.getAttribute('name') || ''),
                value: clipString(element.value != null ? element.value : ''),
                multiple: element.multiple ? true : null,
                disabled: element.disabled ? true : null,
                required: element.required ? true : null,
                options: collectSelectOptions(element),
                label: controlLabel(element)
            }, []);
        } else if (tag === 'BUTTON' || role === 'button') {
            // Terminal leaf: aggregate label only.
            var buttonText = clipString(visibleText(element) || element.getAttribute('value') || '');
            node = makeNode('button', element, {
                button_type: clipString(element.getAttribute('type') || 'submit'),
                name: clipString(element.getAttribute('name') || ''),
                value: clipString(element.getAttribute('value') || ''),
                disabled: element.disabled ? true : null,
                text: buttonText,
                label: buttonText || controlLabel(element)
            }, []);
        } else {
            // Generic semantic group (article, figure, form, fieldset, details,
            // role=group, or id-bearing wrappers kept for fragment targets).
            children = visitOrdered(element, depth + 1);
            if (!children.length) {
                var groupText = clipString(visibleText(element));
                // Keep empty id-bearing nodes so #id can still resolve.
                if (!groupText && !element.id) {
                    return [];
                }
                node = makeNode('group', element, {
                    text: groupText,
                    label: clipString(element.getAttribute('aria-label') || element.id || '')
                }, []);
            } else {
                node = makeNode('group', element, {
                    label: clipString(element.getAttribute('aria-label') || '')
                }, children);
            }
        }

        if (!node) {
            return [];
        }
        return [node];
    }

    // Count ids across the full live DOM, including nodes omitted from the
    // semantic projection. An emitted node may only receive an author identity
    // when its id is unique at the browser interaction boundary as well.
    function markUniqueIds(nodes, root) {
        var counts = Object.create(null);

        if (root && root.querySelectorAll) {
            var allWithId = root.querySelectorAll('[id]');
            for (var i = 0; i < allWithId.length; i += 1) {
                var id = allWithId[i].id;
                if (id) counts[id] = (counts[id] || 0) + 1;
            }
        }

        function walk(list) {
            for (var i = 0; i < list.length; i += 1) {
                var node = list[i];
                if (node.id) {
                    counts[node.id] = (counts[node.id] || 0) + 1;
                }
                if (node.children && node.children.length) {
                    walk(node.children);
                }
            }
        }

        function apply(list) {
            for (var i = 0; i < list.length; i += 1) {
                var node = list[i];
                node.unique_id = !!(node.id && counts[node.id] === 1);
                if (node.children && node.children.length) {
                    apply(node.children);
                }
            }
        }

        // The query above is authoritative for a real DOM capture. Keep the
        // tree walk for synthetic/fixture roots that do not implement it.
        if (!root || !root.querySelectorAll) walk(nodes);
        apply(nodes);
    }

    try {
        var documentState = ensureDocumentState(document);
        var root = document.body || document.documentElement;
        var nodes = root ? visit(root, 0) : [];
        markUniqueIds(nodes, root);

        // Semantic capture deliberately models the top-level document only:
        // iframe content is neither walked nor exposed as semantic components.
        // Keep its revision in the same canonical main-frame namespace as the
        // browser metadata probe (`main:<revision>`), so publication can prove
        // that the represented DOM did not change after this capture. Frame
        // suffixes are intentionally excluded because they describe content
        // outside this semantic document.
        return {
            document: {
                document_id: documentState.documentId,
                revision: 'main:' + String(documentState.revision),
                url: document.location.href,
                title: document.title || '',
                ready_state: document.readyState,
                frames: []
            },
            nodes: nodes,
            truncated: truncated,
            error: null
        };
    } catch (error) {
        return {
            document: {
                document_id: '',
                revision: '',
                url: '',
                title: '',
                ready_state: '',
                frames: []
            },
            nodes: [],
            truncated: false,
            error: String(error && error.message ? error.message : error)
        };
    }
})());