Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
765 lines (669 loc) · 30 KB
/
Copy pathcontent.js
File metadata and controls
765 lines (669 loc) · 30 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
// content.js - Chrome Extension Content Script
// Collects page context and performs browser automation
(function () {
'use strict';
const HTTPS_RE = /^https?:\/\//;
const domElementRefs = new Map();
function safeSend(msg) {
try {
if (typeof chrome === 'undefined' || !chrome.runtime || typeof chrome.runtime.sendMessage !== 'function') return;
chrome.runtime.sendMessage(msg);
} catch (e) {}
}
// Intercept history API for SPA navigation (YouTube, etc.)
(function () {
let lastUrl = location.href;
const origPushState = history.pushState;
history.pushState = function () {
origPushState.apply(history, arguments);
if (location.href !== lastUrl) {
lastUrl = location.href;
safeSend({ type: 'context.refresh' });
}
};
const origReplaceState = history.replaceState;
history.replaceState = function () {
origReplaceState.apply(history, arguments);
if (location.href !== lastUrl) {
lastUrl = location.href;
safeSend({ type: 'context.refresh' });
}
};
window.addEventListener('popstate', function () {
if (location.href !== lastUrl) {
lastUrl = location.href;
safeSend({ type: 'context.refresh' });
}
});
// Fallback: check URL every 10s in case history API wasn't used
setInterval(function () {
if (location.href !== lastUrl) {
lastUrl = location.href;
safeSend({ type: 'context.refresh' });
}
}, 10000);
})();
// ─── Message Listener ───────────────────────────────────────────────────────────
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
try {
switch (message.type) {
case 'page.collect':
collectPageProbe(message.overrideUrl).then(sendResponse).catch((err) => sendResponse({ error: err.message, rawCapture: { metadata: pageMetadata() } }));
return true;
case 'page.dom.snapshot':
sendResponse(collectDomSnapshot());
return true;
case 'page.dom.tree':
collectDomTree().then(sendResponse).catch((err) => sendResponse({ error: err.message }));
return true;
case 'page.dom.perform':
performDomActions(message.steps).then(sendResponse).catch((err) => sendResponse({ ok: false, summary: err.message, results: [] }));
return true;
case 'page.navigate':
sendResponse(handleNavigation(message.command));
return true;
case 'page.links.collect':
sendResponse({ links: collectPageLinks() });
return true;
case 'page.inject':
injectBuildDomTree().then((result) => sendResponse(result)).catch((err) => sendResponse({ injected: false, error: err.message }));
return true;
case 'page.highlight':
highlightElements(message.elements).then(sendResponse);
return true;
case 'page.highlight.remove':
removeHighlights();
sendResponse({ ok: true });
return true;
case 'page.highlight.toggle':
toggleHighlights(message.visible);
sendResponse({ ok: true });
return true;
case 'page.highlight.setState':
setBadgeState(message.highlightIndex, message.state);
sendResponse({ ok: true });
return true;
}
} catch (e) {}
return false;
});
// ─── Page Metadata ─────────────────────────────────────────────────────────────
function pageMetadata(overrideUrl) {
const faviconEl = document.querySelector('link[rel="icon"]') || document.querySelector('link[rel="shortcut icon"]');
return {
url: overrideUrl || window.location.href,
title: document.title,
domain: window.location.hostname,
favicon: faviconEl?.href || '',
};
}
// ─── Page Probe ────────────────────────────────────────────────────────────────
async function collectPageProbe(overrideUrl) {
// Small delay to let SPA pages settle before reading DOM
await new Promise(r => setTimeout(r, 100));
const bodyText = collectBodyText();
const images = collectPageImages().slice(0, 8);
return {
rawCapture: {
metadata: pageMetadata(overrideUrl),
selectedText: window.getSelection?.()?.toString().trim() ?? '',
bodyText,
images,
privacyFlags: {
containsSensitiveFormData: !!document.querySelector('input[type="password"], input[autocomplete*="cc-"], input[autocomplete*="card"]'),
userConsentedToHistory: false,
},
},
features: {
textLength: bodyText.length,
imageCount: images.length,
hasCanvas: document.querySelectorAll('canvas').length > 0,
hasVideo: document.querySelectorAll('video').length > 0,
hasDenseInteractiveUi: document.querySelectorAll('button, input, [role="button"]').length > 15,
},
};
}
// ─── Body Text Collection ─────────────────────────────────────────────────────
const HIGH_VALUE_SELECTORS = ['article', '[role="article"]', 'main', '[role="main"]', '.post', '.article', '.content', '.entry', '.story'];
const VISIBLE_TEXT_SELECTORS = ['section', 'div', 'p', 'li', 'td', 'th', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
const SKIP_TAGS = new Set(['SCRIPT', 'STYLE', 'NOSCRIPT', 'IFRAME', 'NAV', 'FOOTER', 'HEADER', 'ASIDE']);
function collectBodyText() {
const MAX_LEN = 50000;
const candidates = [];
for (const sel of HIGH_VALUE_SELECTORS) {
for (const el of document.querySelectorAll(sel)) {
if (isVisible(el)) candidates.push({ el, priority: 2 });
}
}
for (const sel of VISIBLE_TEXT_SELECTORS) {
for (const el of document.querySelectorAll(sel)) {
if (isVisible(el) && el.textContent.trim().length > 50) candidates.push({ el, priority: 1 });
}
}
const seen = new Set();
const lines = [];
let length = 0;
candidates.sort((a, b) => b.priority - a.priority || a.el.sourceIndex - b.el.sourceIndex);
for (const { el } of candidates) {
const text = extractText(el).trim();
if (!text || text.length < 30) continue;
const normalized = text.toLowerCase().replace(/\s+/g, ' ').slice(0, 200);
if (seen.has(normalized)) continue;
seen.add(normalized);
lines.push(text);
length += text.length + 1;
if (length > MAX_LEN) break;
}
return lines.join('\n\n').slice(0, MAX_LEN);
}
function extractText(el) {
return Array.from(el.childNodes)
.filter((n) => n.nodeType === Node.TEXT_NODE || (n.nodeType === Node.ELEMENT_NODE && !SKIP_TAGS.has(n.tagName)))
.map((n) => (n.nodeType === Node.TEXT_NODE ? n.textContent : extractText(n)))
.join(' ').replace(/\s+/g, ' ').trim();
}
function isVisible(el) {
if (!(el instanceof HTMLElement)) return false;
const s = window.getComputedStyle(el);
return s.display !== 'none' && s.visibility !== 'hidden' && el.offsetWidth > 0 && el.offsetHeight > 0;
}
// ─── Page Images ──────────────────────────────────────────────────────────────
function collectPageImages() {
const results = [];
const seen = new Set();
for (const img of document.images) {
if (!img.src || img.src.startsWith('data:') || seen.has(img.src)) continue;
if (img.width < 64 || img.height < 64) continue;
seen.add(img.src);
const rect = img.getBoundingClientRect();
results.push({ url: img.currentSrc || img.src, alt: img.alt || '', width: img.naturalWidth, height: img.naturalHeight, visible: isVisible(img), viewportRect: { left: Math.round(rect.left), top: Math.round(rect.top), width: Math.round(rect.width), height: Math.round(rect.height) } });
}
return results;
}
// ─── Page Links ───────────────────────────────────────────────────────────────
function collectPageLinks() {
const results = [];
const seen = new Set();
for (const el of document.querySelectorAll('a[href]')) {
const href = el.href;
if (!href || !href.startsWith('http') || seen.has(href)) continue;
const text = el.textContent.trim().slice(0, 80);
if (!text) continue;
seen.add(href);
const rect = el.getBoundingClientRect();
results.push({
index: results.length + 1,
text,
href,
visible: isVisible(el),
viewportRect: { left: Math.round(rect.left), top: Math.round(rect.top), width: Math.round(rect.width), height: Math.round(rect.height) },
});
}
return results.slice(0, 30);
}
// ─── DOM Snapshot ─────────────────────────────────────────────────────────────
function collectDomSnapshot() {
domElementRefs.clear();
const selectors = ['button', 'a[href]', 'input:not([type="hidden"])', 'textarea', 'select', 'summary', '[role="button"]', '[role="link"]', '[role="menuitem"]', '[role="option"]', '[role="checkbox"]', '[role="radio"]', '[role="switch"]', '[role="tab"]', '[role="textbox"]', '[contenteditable="true"]'];
const elements = [];
for (const sel of selectors) {
for (const el of document.querySelectorAll(sel)) {
if (isVisible(el)) elements.push(el);
}
}
const described = elements.filter((el) => !domElementRefs.has(el)).slice(0, 60).map((el, i) => {
const ref = `el-${i + 1}`;
domElementRefs.set(ref, el);
return describeElement(ref, el);
});
return { metadata: pageMetadata(), elements: described, capabilities: { supportsDomAutomation: true } };
}
function describeElement(ref, el) {
const rect = el.getBoundingClientRect();
const isInput = el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement;
return { ref, tagName: el.tagName.toLowerCase(), role: el.getAttribute('role') || inferRole(el), label: getElementLabel(el), text: (el.innerText || el.textContent || '').trim().slice(0, 120), selector: buildSelector(el), href: el instanceof HTMLAnchorElement ? el.href : undefined, value: isInput && el.value ? el.value : undefined, placeholder: isInput && el.placeholder ? el.placeholder : undefined, type: isInput && el.type ? el.type : undefined, isTextEntryCandidate: isTextEntryCandidate(el), disabled: el.disabled || el.getAttribute('aria-disabled') === 'true', viewportRect: { left: Math.round(rect.left), top: Math.round(rect.top), width: Math.round(rect.width), height: Math.round(rect.height) } };
}
function inferRole(el) {
if (el instanceof HTMLButtonElement) return 'button';
if (el instanceof HTMLAnchorElement) return 'link';
if (el instanceof HTMLInputElement) return el.type === 'checkbox' ? 'checkbox' : el.type === 'radio' ? 'radio' : 'textbox';
if (el instanceof HTMLTextAreaElement) return 'textbox';
if (el instanceof HTMLSelectElement) return 'listbox';
return '';
}
function getElementLabel(el) {
return el.getAttribute('aria-label') || el.getAttribute('aria-labelledby') || el.getAttribute('id') || el.name || '';
}
function buildSelector(el) {
if (el.id) return `#${el.id}`;
const parts = [];
let cur = el;
while (cur && cur !== document.body && parts.length < 4) {
let sel = cur.tagName.toLowerCase();
if (cur.id) { sel += `#${cur.id}`; parts.unshift(sel); break; }
if (cur.className && typeof cur.className === 'string') {
const cls = cur.className.trim().split(/\s+/)[0];
if (cls) sel += `.${cls}`;
}
parts.unshift(sel);
cur = cur.parentElement;
}
return parts.join(' > ');
}
function isTextEntryCandidate(el) {
return (el instanceof HTMLInputElement && !['hidden', 'checkbox', 'radio', 'file', 'submit', 'button', 'image', 'reset'].includes(el.type)) || el instanceof HTMLTextAreaElement || el.getAttribute('contenteditable') === 'true';
}
// ─── DOM Actions ──────────────────────────────────────────────────────────────
async function performDomActions(steps) {
const results = [];
for (const step of steps.slice(0, 5)) {
results.push(await performDomAction(step));
}
return { ok: results.every((r) => r.ok), summary: results.map((r) => r.message).join('; '), results };
}
async function performDomAction(step) {
try {
const el = resolveTarget(step);
if (!el && step.action !== 'navigate' && step.action !== 'scroll') {
return { ok: false, message: `Element not found: ${step.ref || step.label || step.selector}` };
}
switch (step.action) {
case 'click':
if (!el) return { ok: false, message: 'No target element' };
el.scrollIntoView({ block: 'center', behavior: 'smooth' });
el.click();
return { ok: true, message: `Clicked: ${getElementLabel(el) || step.action}` };
case 'type':
if (!el) return { ok: false, message: 'No target element' };
el.focus();
const text = step.value || '';
if (el instanceof HTMLTextAreaElement || el instanceof HTMLInputElement) {
if (el.type === 'file' || el.type === 'hidden' || el.type === 'submit' || el.type === 'button' || el.type === 'image' || el.type === 'reset') {
return { ok: false, message: `Cannot type into ${el.type} input` };
}
el.value = text;
el.dispatchEvent(new InputEvent('input', { bubbles: true, inputType: 'insertText', data: text }));
} else if (el.getAttribute('contenteditable') === 'true') {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
selection.deleteFromDocument();
}
document.execCommand('insertText', false, text);
el.dispatchEvent(new InputEvent('input', { bubbles: true }));
} else {
el.innerText = text;
el.dispatchEvent(new InputEvent('input', { bubbles: true }));
}
return { ok: true, message: `Typed: ${step.value}` };
case 'scroll':
if (step.direction === 'top') window.scrollTo({ top: 0, behavior: 'smooth' });
else if (step.direction === 'bottom') window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });
else window.scrollBy({ top: (step.direction === 'up' ? -1 : 1) * window.innerHeight * 0.8, behavior: 'smooth' });
return { ok: true, message: `Scrolled ${step.direction || 'down'}` };
case 'navigate':
if (!step.url) return { ok: false, message: 'No URL provided' };
if (!HTTPS_RE.test(step.url)) return { ok: false, message: 'Only HTTP(S) navigation supported' };
window.location.href = step.url;
return { ok: true };
case 'select':
if (!el || !(el instanceof HTMLSelectElement)) return { ok: false, message: 'Not a select element' };
const option = Array.from(el.options).find((o) => o.value === step.value || o.label === step.label);
if (option) { el.value = option.value; el.dispatchEvent(new Event('change', { bubbles: true })); }
return { ok: !!option, message: option ? `Selected: ${option.label}` : 'Option not found' };
case 'hover':
if (!el) return { ok: false, message: 'No target element' };
el.scrollIntoView({ block: 'center', behavior: 'smooth' });
el.dispatchEvent(new MouseEvent('mouseenter', { bubbles: true, cancelable: true, view: window }));
return { ok: true, message: `Hovered: ${getElementLabel(el) || step.action}` };
case 'scroll_to':
if (!el) return { ok: false, message: 'No target element' };
el.scrollIntoView({ block: 'center', behavior: 'smooth' });
return { ok: true, message: `Scrolled to: ${getElementLabel(el) || step.action}` };
case 'drag':
if (!el) return { ok: false, message: 'No target element for drag source' };
const targetSelector = step.target;
let targetEl = null;
if (targetSelector) {
if (targetSelector.startsWith('/')) {
try {
const result = document.evaluate(targetSelector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
targetEl = result.singleNodeValue;
} catch (e) {}
} else {
targetEl = document.querySelector(targetSelector);
}
}
const dragStart = el.getBoundingClientRect();
const dragEnd = targetEl ? targetEl.getBoundingClientRect() : { left: dragStart.left + 100, top: dragStart.top + 100 };
const dragStartEvent = new DragEvent('dragstart', { bubbles: true, cancelable: true, dataTransfer: new DataTransfer() });
el.dispatchEvent(dragStartEvent);
const dropEvent = new DragEvent('drop', { bubbles: true, cancelable: true, dataTransfer: new DataTransfer(), clientX: dragEnd.left + dragEnd.width / 2, clientY: dragEnd.top + dragEnd.height / 2 });
if (targetEl) targetEl.dispatchEvent(dropEvent);
const dragEndEvent = new DragEvent('dragend', { bubbles: true, cancelable: true });
el.dispatchEvent(dragEndEvent);
return { ok: true, message: `Dragged ${getElementLabel(el)} to ${targetSelector || 'offset'}` };
case 'go_back':
if (window.history.length > 1) { window.history.back(); return { ok: true, message: 'Navigated back' }; }
return { ok: false, message: 'No back history available' };
case 'refresh':
window.location.reload();
return { ok: true, message: 'Page refreshed' };
default:
return { ok: false, message: `Unsupported action: ${step.action}` };
}
} catch (err) {
return { ok: false, message: err.message };
}
}
function resolveTarget(step) {
if (step.ref && domElementRefs.has(step.ref)) return domElementRefs.get(step.ref);
if (step.selector) {
if (step.selector.startsWith('/') || step.selector.includes('/*[')) {
try {
const result = document.evaluate(step.selector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
if (result.singleNodeValue) return result.singleNodeValue;
const simpleXPath = step.selector.includes('@id')
? `//*[@id="${step.selector.match(/@id="([^"]+)"/)?.[1]}"]`
: null;
if (simpleXPath) {
const simpleResult = document.evaluate(simpleXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
if (simpleResult.singleNodeValue) return simpleResult.singleNodeValue;
}
const draftResult = document.evaluate('//div[contains(@class,"DraftEditor-root")]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
if (draftResult.singleNodeValue) return draftResult.singleNodeValue;
const contenteditable = document.evaluate('//div[@contenteditable="true"][contains(@class,"notranslate")]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
if (contenteditable.singleNodeValue) return contenteditable.singleNodeValue;
return null;
} catch (e) {
return null;
}
}
const found = document.querySelector(step.selector);
if (found) return found;
if (step.selector.includes('nth-of-type')) {
const inputs = document.querySelectorAll('input:not([type="hidden"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="image"]):not([type="reset"])');
if (inputs.length > 0) return inputs[0];
const textareas = document.querySelectorAll('textarea');
if (textareas.length > 0) return textareas[0];
const editable = document.querySelector('[contenteditable="true"]');
if (editable) return editable;
}
return null;
}
if (step.label) {
const normalized = step.label.toLowerCase();
for (const [ref, el] of domElementRefs) {
if (getElementLabel(el).toLowerCase().includes(normalized) || (el.innerText || '').toLowerCase().includes(normalized)) return el;
}
}
return null;
}
// ─── Navigation ──────────────────────────────────────────────────────────────
function handleNavigation(command) {
if (command.kind === 'scroll') {
if (command.direction === 'top') window.scrollTo({ top: 0, behavior: 'smooth' });
else if (command.direction === 'bottom') window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });
else window.scrollBy({ top: (command.direction === 'up' ? -1 : 1) * window.innerHeight * 0.8, behavior: 'smooth' });
} else if (command.kind === 'navigate') {
if (!command.url) return { ok: false, error: 'No URL provided' };
if (!/^https?:\/\//.test(command.url)) return { ok: false, error: 'Only HTTP(S) URLs supported' };
window.location.href = command.url;
}
return { ok: true };
}
// ─── DOM Tree Collection (DRAGON) ───────────────────────────────────────────
async function injectBuildDomTree() {
if (window.buildDomTree) {
return { injected: true, alreadyExists: true };
}
try {
await chrome.scripting.executeScript({
target: { allFrames: true },
files: ['buildDomTree.js']
});
return { injected: true };
} catch (e) {
return { injected: false, error: e.message };
}
}
async function collectDomTree() {
await new Promise(r => setTimeout(r, 100));
if (!window.buildDomTree) {
await injectBuildDomTree();
}
if (!window.buildDomTree) {
return { error: 'buildDomTree not available', elements: collectDomSnapshot().elements };
}
const startTime = Date.now();
const result = window.buildDomTree({ startId: 0, startHighlightIndex: 0 });
const elements = [];
const selectorMap = new Map();
for (const [id, node] of Object.entries(result.map)) {
if (!node || node.type === 'TEXT_NODE') continue;
const element = {
ref: `el-${node.highlightIndex || id}`,
tagName: node.tagName,
xpath: node.xpath,
attributes: node.attributes || {},
text: node.attributes?.text || '',
href: node.attributes?.href,
src: node.attributes?.src,
alt: node.attributes?.alt,
title: node.attributes?.title,
placeholder: node.attributes?.placeholder,
type: node.attributes?.type,
role: node.attributes?.role,
value: node.attributes?.value,
class: node.attributes?.class,
id: node.attributes?.id,
name: node.attributes?.name,
isInteractive: node.isInteractive,
isTopElement: node.isTopElement,
isInViewport: node.isInViewport,
isVisible: node.isVisible,
highlightIndex: node.highlightIndex,
'data-testid': node.attributes?.['data-testid'],
'data-cy': node.attributes?.['data-cy'],
'data-test': node.attributes?.['data-test'],
'aria-label': node.attributes?.['aria-label'],
viewportRect: {
left: node.attributes?.viewport?.left || 0,
top: node.attributes?.viewport?.top || 0,
width: node.attributes?.viewport?.width || 0,
height: node.attributes?.viewport?.height || 0
}
};
if (node.highlightIndex != null) {
}
elements.push(element);
if (node.highlightIndex != null) {
selectorMap.set(node.highlightIndex, element);
}
}
return {
metadata: pageMetadata(),
elements,
selectorMap: Object.fromEntries(selectorMap),
stats: {
totalElements: Object.keys(result.map).length,
interactiveElements: result.highlightCount,
buildTimeMs: Date.now() - startTime
}
};
}
// ─── Element Highlighting ──────────────────────────────────────────────────────
function highlightElements(elements) {
removeHighlights();
if (!elements || elements.length === 0) {
return { ok: false, error: 'No elements to highlight' };
}
let highlightedCount = 0;
for (const el of elements) {
if (el.highlightIndex == null) continue;
let domEl = null;
const selectors = [];
if (el.href) selectors.push(`a[href="${el.href}"]`);
if (el.xpath) {
try {
const result = document.evaluate(el.xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
if (result.singleNodeValue) {
domEl = result.singleNodeValue;
}
} catch (e) {
}
}
if (!domEl && el.tagName === 'input' && el.type) {
domEl = document.querySelector(`input[type="${el.type}"]`);
}
if (!domEl && el.tagName === 'button') {
domEl = document.querySelector('button');
}
if (!domEl && el.tagName === 'a' && el.href) {
domEl = document.querySelector(`a[href="${el.href}"]`);
}
if (!domEl && el.tagName) {
domEl = document.querySelector(el.tagName);
}
if (!domEl) {
continue;
}
const rect = domEl.getBoundingClientRect();
if (rect.width < 2 || rect.height < 2) {
continue;
}
const badge = document.createElement('div');
badge.textContent = el.highlightIndex;
badge.dataset.openagentBadge = el.highlightIndex;
badge.className = 'openagent-badge';
badge.style.cssText = [
'position:fixed',
`left:${rect.left + rect.width / 2}px`,
`top:${rect.top - 8}px`,
'transform:translateX(-50%)',
'background:#7c6af7',
'color:white',
'border-radius:10px',
'min-width:18px',
'height:18px',
'display:none',
'align-items:center',
'justify-content:center',
'font-weight:600',
'font-size:11px',
'pointer-events:none',
'z-index:2147483647',
'box-shadow:0 2px 8px rgba(0,0,0,0.4)',
'padding:0 5px',
'transition:background 0.2s, transform 0.2s',
].join(';');
const highlight = document.createElement('div');
highlight.dataset.openagentHighlight = el.highlightIndex;
highlight.className = 'openagent-highlight';
highlight.style.cssText = [
'position:fixed',
`left:${rect.left}px`,
`top:${rect.top}px`,
`width:${rect.width}px`,
`height:${rect.height}px`,
'background:rgba(124,106,247,0.15)',
'border:2px solid #7c6af7',
'border-radius:3px',
'pointer-events:none',
'z-index:2147483646',
'box-sizing:border-box',
'display:none',
'transition:border-color 0.2s, background 0.2s',
].join(';');
(document.body || document.documentElement).appendChild(highlight);
(document.body || document.documentElement).appendChild(badge);
highlightedCount++;
}
return { ok: true, highlightedCount };
}
function removeHighlights() {
const existing = document.getElementById('openagent-highlight-container');
if (existing) existing.remove();
document.querySelectorAll('[data-openagent-highlight]').forEach(el => el.remove());
document.querySelectorAll('[data-openagent-badge]').forEach(el => el.remove());
}
function toggleHighlights(visible) {
const highlightDisplay = visible ? 'block' : 'none';
const badgeDisplay = visible ? 'flex' : 'none';
document.querySelectorAll('[data-openagent-highlight]').forEach(el => {
el.style.display = highlightDisplay;
});
document.querySelectorAll('[data-openagent-badge]').forEach(el => {
el.style.display = badgeDisplay;
});
}
function setBadgeState(highlightIndex, state) {
const badge = document.querySelector(`.openagent-badge[data-openagent-badge="${highlightIndex}"]`);
const highlight = document.querySelector(`.openagent-highlight[data-openagent-highlight="${highlightIndex}"]`);
if (!badge) return;
badge.classList.remove('openagent-badge-loading', 'openagent-badge-success', 'openagent-badge-error');
if (state === 'loading') {
badge.classList.add('openagent-badge-loading');
} else if (state === 'success') {
badge.classList.add('openagent-badge-success');
if (highlight) highlight.classList.add('openagent-highlight-success');
} else if (state === 'error') {
badge.classList.add('openagent-badge-error');
if (highlight) highlight.classList.add('openagent-highlight-error');
}
setTimeout(() => {
if (state === 'success' || state === 'error') {
badge.classList.remove('openagent-badge-loading', 'openagent-badge-success', 'openagent-badge-error');
badge.classList.add('openagent-badge');
if (highlight) {
highlight.classList.remove('openagent-highlight-success', 'openagent-highlight-error');
highlight.classList.add('openagent-highlight');
}
}
}, 2000);
}
function computePageStateHash() {
const elements = document.querySelectorAll('a[href], button, input, textarea, select, [role="button"], [role="link"]');
let hash = 0;
for (const el of elements) {
const rect = el.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0) {
const text = el.textContent?.trim().slice(0, 20) || '';
const href = el.href || '';
hash = (hash * 31 + text.length + href.length) >>> 0;
}
}
return hash;
}
let lastPageStateHash = null;
function verifyAction(action, selector) {
const currentHash = computePageStateHash();
const hashChanged = lastPageStateHash !== null && lastPageStateHash !== currentHash;
lastPageStateHash = currentHash;
if (action === 'click') {
let target = null;
if (selector.startsWith('/') || selector.includes('/*[')) {
try {
const result = document.evaluate(selector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
target = result.singleNodeValue;
} catch (e) {}
} else {
target = document.querySelector(selector);
}
if (!target) {
return { ok: false, reason: 'element_not_found', recovered: false };
}
const urlChanged = window.location.href !== lastUrl;
lastUrl = window.location.href;
return {
ok: true,
urlChanged,
hashChanged,
elementExists: !!target,
elementText: target.textContent?.trim().slice(0, 50) || ''
};
}
return { ok: true };
}
let lastUrl = window.location.href;
// End of content script IIFE
})();