forked from ComposioHQ/open-chatgpt-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.ts
More file actions
713 lines (624 loc) · 26.6 KB
/
content.ts
File metadata and controls
713 lines (624 loc) · 26.6 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
// Content script that runs on all pages to extract context and interact with the DOM
// Visual feedback for clicks
function highlightElement(element: Element, coordinates: { x: number; y: number }) {
const originalOutline = (element as HTMLElement).style.outline;
const originalBg = (element as HTMLElement).style.backgroundColor;
(element as HTMLElement).style.outline = '3px solid #007AFF';
(element as HTMLElement).style.backgroundColor = 'rgba(0, 122, 255, 0.1)';
// Create a visual click indicator at the coordinates
const indicator = document.createElement('div');
indicator.style.cssText = `
position: fixed;
left: ${coordinates.x}px;
top: ${coordinates.y}px;
width: 20px;
height: 20px;
margin-left: -10px;
margin-top: -10px;
border-radius: 50%;
background: rgba(0, 122, 255, 0.5);
border: 2px solid #007AFF;
pointer-events: none;
z-index: 999999;
animation: atlasClickPulse 0.6s ease-out;
`;
// Add animation keyframes if not already present
if (!document.getElementById('atlas-click-animation')) {
const style = document.createElement('style');
style.id = 'atlas-click-animation';
style.textContent = `
@keyframes atlasClickPulse {
0% { transform: scale(1); opacity: 1; }
100% { transform: scale(2.5); opacity: 0; }
}
`;
document.head.appendChild(style);
}
document.body.appendChild(indicator);
setTimeout(() => {
(element as HTMLElement).style.outline = originalOutline;
(element as HTMLElement).style.backgroundColor = originalBg;
indicator.remove();
}, 600);
}
interface PageContext {
url: string;
title: string;
textContent: string;
links: Array<{ text: string; href: string }>;
images: Array<{ alt: string; src: string }>;
forms: Array<{ id: string; action: string; inputs: Array<{ name: string; type: string }> }>;
metadata: {
description?: string;
keywords?: string;
author?: string;
};
viewport: {
width: number;
height: number;
scrollX: number;
scrollY: number;
devicePixelRatio: number;
};
}
// Extract comprehensive page context
function extractPageContext(): PageContext {
const links = Array.from(document.querySelectorAll('a')).slice(0, 50).map(a => ({
text: a.textContent?.trim() || '',
href: a.href
}));
const images = Array.from(document.querySelectorAll('img')).slice(0, 20).map(img => ({
alt: img.alt,
src: img.src
}));
const forms = Array.from(document.querySelectorAll('form')).map(form => ({
id: form.id,
action: form.action,
inputs: Array.from(form.querySelectorAll('input, textarea, select')).map(input => ({
name: (input as HTMLInputElement).name,
type: (input as HTMLInputElement).type || 'text'
}))
}));
const getMetaContent = (name: string): string | undefined => {
const meta = document.querySelector(`meta[name="${name}"], meta[property="${name}"]`);
return meta?.getAttribute('content') || undefined;
};
return {
url: window.location.href,
title: document.title,
textContent: document.body.innerText.slice(0, 10000), // Limit to 10k chars
links,
images,
forms,
metadata: {
description: getMetaContent('description') || getMetaContent('og:description'),
keywords: getMetaContent('keywords'),
author: getMetaContent('author')
},
viewport: {
width: window.innerWidth,
height: window.innerHeight,
scrollX: window.scrollX,
scrollY: window.scrollY,
devicePixelRatio: window.devicePixelRatio
}
};
}
// Execute actions on the page
function executePageAction(
action: string,
target?: string,
value?: string,
selector?: string,
coordinates?: { x: number; y: number },
direction?: string,
amount?: number,
key?: string,
keys?: string[],
destination?: { x: number; y: number }
): any {
try {
switch (action) {
case 'click':
// Support both selector and coordinate-based clicking
if (selector || target) {
const element = document.querySelector(selector || target!);
if (element) {
const rect = element.getBoundingClientRect();
const clickX = rect.left + rect.width / 2;
const clickY = rect.top + rect.height / 2;
['mousedown', 'mouseup', 'click'].forEach(eventType => {
const event = new MouseEvent(eventType, {
bubbles: true,
cancelable: true,
view: window,
clientX: clickX,
clientY: clickY
});
element.dispatchEvent(event);
});
// Visual feedback
highlightElement(element, coordinates || { x: clickX, y: clickY });
return { success: true, message: `Clicked element: ${selector || target}`, element: element.tagName };
}
return { success: false, message: `Element not found: ${selector || target}` };
} else if (coordinates) {
const element = document.elementFromPoint(coordinates.x, coordinates.y);
if (element) {
// Get element position for logging
const rect = element.getBoundingClientRect();
// Dispatch full mouse event sequence
['mousedown', 'mouseup', 'click'].forEach(eventType => {
const event = new MouseEvent(eventType, {
bubbles: true,
cancelable: true,
view: window,
clientX: coordinates.x,
clientY: coordinates.y
});
element.dispatchEvent(event);
});
// Visual feedback
highlightElement(element, coordinates);
return {
success: true,
message: `Clicked at (${coordinates.x}, ${coordinates.y})`,
element: element.tagName,
text: element.textContent?.slice(0, 50),
elementBounds: {
left: rect.left,
top: rect.top,
width: rect.width,
height: rect.height
}
};
}
console.error('❌ No element found at coordinates');
return { success: false, message: `No element found at coordinates (${coordinates.x}, ${coordinates.y})` };
}
return { success: false, message: 'Target selector or coordinates required for click action' };
case 'fill':
if (value) {
const textToType = value; // Capture value to preserve type narrowing
let element: HTMLElement | null = null;
// Try to find element by selector if provided
if (target && !target.includes(':focus')) {
element = document.querySelector(target) as HTMLElement;
}
// If no element found or selector was for focused elements, use the currently focused element
if (!element) {
element = document.activeElement as HTMLElement;
}
if (element && (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA' ||
element.getAttribute('contenteditable') === 'true')) {
// Click the element first to ensure it receives focus
const rect = element.getBoundingClientRect();
const clickX = rect.left + rect.width / 2;
const clickY = rect.top + rect.height / 2;
['mousedown', 'mouseup', 'click'].forEach(eventType => {
const event = new MouseEvent(eventType, {
bubbles: true,
cancelable: true,
view: window,
clientX: clickX,
clientY: clickY
});
element!.dispatchEvent(event);
});
// Explicitly focus the element
element.focus();
// Return a promise that resolves after a delay to ensure focus is established
return new Promise<any>((resolve) => {
setTimeout(() => {
// Verify element still has focus
const stillFocused = document.activeElement === element;
if (!stillFocused) {
element!.focus();
// Add another small delay after re-focus
setTimeout(() => {
proceedWithTyping();
}, 100);
} else {
proceedWithTyping();
}
function proceedWithTyping() {
// Clear existing value first
if (element!.tagName === 'INPUT' || element!.tagName === 'TEXTAREA') {
const inputElement = element as HTMLInputElement;
// Clear the value using multiple methods for compatibility
inputElement.value = '';
// Set the new value using native setter (works with React)
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
'value'
)?.set;
if (nativeInputValueSetter) {
nativeInputValueSetter.call(inputElement, textToType);
} else {
inputElement.value = textToType;
}
// Trigger all necessary events for React/Vue/Angular apps
inputElement.dispatchEvent(new Event('input', { bubbles: true }));
inputElement.dispatchEvent(new Event('change', { bubbles: true }));
inputElement.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
inputElement.dispatchEvent(new KeyboardEvent('keypress', { key: 'Enter', bubbles: true }));
inputElement.dispatchEvent(new KeyboardEvent('keyup', { key: 'Enter', bubbles: true }));
} else if (element!.getAttribute('contenteditable') === 'true') {
// For contenteditable elements, clear and set text
element!.textContent = textToType;
element!.dispatchEvent(new Event('input', { bubbles: true }));
element!.dispatchEvent(new Event('change', { bubbles: true }));
}
resolve({
success: true,
message: `Typed "${textToType}" into ${element!.tagName}`,
element: element!.tagName
});
}
}, 300); // 300ms delay after focus to ensure it's established
});
}
console.error('❌ Element not typeable:', element?.tagName, element);
return {
success: false,
message: element ? `Element ${element.tagName} is not typeable` : `No focused element found. Try clicking on the input field first.`
};
}
return { success: false, message: 'Value required for fill action' };
case 'scroll':
if (direction === 'top' || target === 'top') {
window.scrollTo({ top: 0, behavior: 'smooth' });
return { success: true, message: 'Scrolled to top' };
} else if (direction === 'bottom' || target === 'bottom') {
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
return { success: true, message: 'Scrolled to bottom' };
} else if (direction === 'up') {
window.scrollBy({ top: -(amount || 300), behavior: 'smooth' });
return { success: true, message: `Scrolled up by ${amount || 300}px` };
} else if (direction === 'down') {
window.scrollBy({ top: (amount || 300), behavior: 'smooth' });
return { success: true, message: `Scrolled down by ${amount || 300}px` };
} else if (selector || target) {
const element = document.querySelector(selector || target!);
if (element) {
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
return { success: true, message: `Scrolled to: ${selector || target}` };
}
return { success: false, message: `Element not found: ${selector || target}` };
}
return { success: true, message: 'Scrolled' };
case 'keyboard_type':
// Simulate actual keyboard typing character by character into the focused element
// This mimics the Python playwright keyboard.type() behavior
if (value) {
const textToType = value;
const focusedEl = document.activeElement;
if (!focusedEl) {
return { success: false, message: 'No element has focus. Click on an input field first.' };
}
// Check if it's a typeable element
const isInput = focusedEl.tagName === 'INPUT';
const isTextarea = focusedEl.tagName === 'TEXTAREA';
const isContentEditable = focusedEl.getAttribute('contenteditable') === 'true';
if (!isInput && !isTextarea && !isContentEditable) {
return { success: false, message: `Element ${focusedEl.tagName} is not typeable. Click on an input field first.` };
}
// Type each character one by one
for (let i = 0; i < textToType.length; i++) {
const char = textToType[i];
// Dispatch keyboard events for this character
const keyEventInit: KeyboardEventInit = {
key: char,
code: char === ' ' ? 'Space' : `Key${char.toUpperCase()}`,
bubbles: true,
cancelable: true
};
focusedEl.dispatchEvent(new KeyboardEvent('keydown', keyEventInit));
focusedEl.dispatchEvent(new KeyboardEvent('keypress', keyEventInit));
// Update the value directly (simpler approach that works for all input types)
if (isInput || isTextarea) {
const inputEl = focusedEl as HTMLInputElement | HTMLTextAreaElement;
// Simply set the value directly (works for Google search and most inputs)
inputEl.value = inputEl.value + char;
// Dispatch input event for each character (for React/Vue/Angular)
inputEl.dispatchEvent(new Event('input', { bubbles: true }));
inputEl.dispatchEvent(new Event('change', { bubbles: true }));
} else if (isContentEditable) {
// For contenteditable, append the character
focusedEl.textContent = (focusedEl.textContent || '') + char;
focusedEl.dispatchEvent(new Event('input', { bubbles: true }));
}
focusedEl.dispatchEvent(new KeyboardEvent('keyup', keyEventInit));
}
return {
success: true,
message: `Typed "${textToType}" into ${focusedEl.tagName}`,
element: focusedEl.tagName
};
}
return { success: false, message: 'Value required for keyboard_type action' };
case 'press_key':
// Press a specific key on the currently focused element
const keyToPress = (key || value || target || 'Enter') as string;
const focusedElement = document.activeElement;
if (focusedElement) {
const keyEventInit: KeyboardEventInit = {
key: keyToPress,
code: keyToPress === 'Enter' ? 'Enter' : keyToPress === 'Tab' ? 'Tab' : keyToPress === 'Escape' ? 'Escape' : `Key${keyToPress}`,
bubbles: true,
cancelable: true
};
focusedElement.dispatchEvent(new KeyboardEvent('keydown', keyEventInit));
focusedElement.dispatchEvent(new KeyboardEvent('keypress', keyEventInit));
focusedElement.dispatchEvent(new KeyboardEvent('keyup', keyEventInit));
return { success: true, message: `Pressed ${keyToPress} key` };
}
return { success: false, message: 'No focused element to send key to' };
case 'clear_input':
// Clear the currently focused input field
const activeEl = document.activeElement as HTMLInputElement;
if (activeEl && (activeEl.tagName === 'INPUT' || activeEl.tagName === 'TEXTAREA' || activeEl.getAttribute('contenteditable') === 'true')) {
// Select all and delete
if (activeEl.tagName === 'INPUT' || activeEl.tagName === 'TEXTAREA') {
activeEl.select();
document.execCommand('delete');
activeEl.value = '';
activeEl.dispatchEvent(new Event('input', { bubbles: true }));
} else {
activeEl.textContent = '';
activeEl.dispatchEvent(new Event('input', { bubbles: true }));
}
return { success: true, message: 'Cleared input field' };
}
return { success: false, message: 'No input field focused to clear' };
case 'key_combination':
// Press a combination of keys like ["Control", "A"] or ["Enter"]
const keysList = keys || ['Enter'];
const targetEl = document.activeElement || document.body;
// Hold down all keys except the last one
for (let i = 0; i < keysList.length - 1; i++) {
const k = keysList[i];
targetEl.dispatchEvent(new KeyboardEvent('keydown', {
key: k,
code: k,
bubbles: true,
cancelable: true
}));
}
// Press the last key
const lastKey = keysList[keysList.length - 1];
targetEl.dispatchEvent(new KeyboardEvent('keydown', { key: lastKey, code: lastKey, bubbles: true }));
targetEl.dispatchEvent(new KeyboardEvent('keypress', { key: lastKey, code: lastKey, bubbles: true }));
targetEl.dispatchEvent(new KeyboardEvent('keyup', { key: lastKey, code: lastKey, bubbles: true }));
// Release all held keys in reverse order
for (let i = keysList.length - 2; i >= 0; i--) {
const k = keysList[i];
targetEl.dispatchEvent(new KeyboardEvent('keyup', {
key: k,
code: k,
bubbles: true,
cancelable: true
}));
}
return { success: true, message: `Pressed key combination: ${keysList.join('+')}` };
case 'hover':
// Hover at specific coordinates
if (coordinates) {
const hoverEl = document.elementFromPoint(coordinates.x, coordinates.y);
if (hoverEl) {
hoverEl.dispatchEvent(new MouseEvent('mouseover', {
bubbles: true,
cancelable: true,
view: window,
clientX: coordinates.x,
clientY: coordinates.y
}));
hoverEl.dispatchEvent(new MouseEvent('mouseenter', {
bubbles: true,
cancelable: true,
view: window,
clientX: coordinates.x,
clientY: coordinates.y
}));
return { success: true, message: `Hovered at (${coordinates.x}, ${coordinates.y})`, element: hoverEl.tagName };
}
return { success: false, message: `No element at (${coordinates.x}, ${coordinates.y})` };
}
return { success: false, message: 'Coordinates required for hover' };
case 'drag_drop':
// Drag and drop from coordinates to destination
if (coordinates && destination) {
const dragEl = document.elementFromPoint(coordinates.x, coordinates.y);
const dropEl = document.elementFromPoint(destination.x, destination.y);
if (dragEl && dropEl) {
try {
const dataTransfer = new DataTransfer();
dataTransfer.effectAllowed = 'all';
// Step 1: Mouse down at source
dragEl.dispatchEvent(new MouseEvent('mousedown', {
bubbles: true,
cancelable: true,
view: window,
clientX: coordinates.x,
clientY: coordinates.y,
buttons: 1
}));
// Step 2: Drag start event
dragEl.dispatchEvent(new DragEvent('dragstart', {
bubbles: true,
cancelable: true,
view: window,
clientX: coordinates.x,
clientY: coordinates.y,
dataTransfer: dataTransfer
}));
// Step 3: Simulate drag movement with mousemove events
// Calculate steps between source and destination
const steps = 10;
const stepX = (destination.x - coordinates.x) / steps;
const stepY = (destination.y - coordinates.y) / steps;
for (let i = 1; i <= steps; i++) {
const currentX = coordinates.x + stepX * i;
const currentY = coordinates.y + stepY * i;
const currentEl = document.elementFromPoint(currentX, currentY);
// Mouse move
if (currentEl) {
currentEl.dispatchEvent(new MouseEvent('mousemove', {
bubbles: true,
cancelable: true,
view: window,
clientX: currentX,
clientY: currentY,
buttons: 1
}));
// Drag over
currentEl.dispatchEvent(new DragEvent('dragover', {
bubbles: true,
cancelable: true,
view: window,
clientX: currentX,
clientY: currentY,
dataTransfer: dataTransfer
}));
// Drag enter on first arrival
if (i === 1 && currentEl !== dragEl) {
currentEl.dispatchEvent(new DragEvent('dragenter', {
bubbles: true,
cancelable: true,
view: window,
clientX: currentX,
clientY: currentY,
dataTransfer: dataTransfer
}));
}
}
}
// Step 4: Drop at destination
dropEl.dispatchEvent(new DragEvent('drop', {
bubbles: true,
cancelable: true,
view: window,
clientX: destination.x,
clientY: destination.y,
dataTransfer: dataTransfer
}));
// Step 5: Drag end on source
dragEl.dispatchEvent(new DragEvent('dragend', {
bubbles: true,
cancelable: true,
view: window,
clientX: destination.x,
clientY: destination.y,
dataTransfer: dataTransfer
}));
// Step 6: Mouse up at destination
dropEl.dispatchEvent(new MouseEvent('mouseup', {
bubbles: true,
cancelable: true,
view: window,
clientX: destination.x,
clientY: destination.y
}));
return { success: true, message: `Dragged from (${coordinates.x}, ${coordinates.y}) to (${destination.x}, ${destination.y})` };
} catch (error) {
return { success: false, message: `Drag and drop failed: ${error}` };
}
}
return { success: false, message: 'Could not find elements at drag or drop coordinates' };
}
return { success: false, message: 'Both coordinates and destination required for drag_drop' };
case 'mouse_move':
// Simulate mouse move by dispatching mouse events
if (coordinates) {
const element = document.elementFromPoint(coordinates.x, coordinates.y);
if (element) {
const moveEvent = new MouseEvent('mousemove', {
bubbles: true,
cancelable: true,
clientX: coordinates.x,
clientY: coordinates.y,
view: window
});
element.dispatchEvent(moveEvent);
return { success: true, message: `Mouse moved to (${coordinates.x}, ${coordinates.y})` };
}
return { success: false, message: `No element at coordinates (${coordinates.x}, ${coordinates.y})` };
}
return { success: false, message: 'Coordinates required for mouse_move action' };
case 'screenshot':
// This would need to be handled by the background script
return { success: true, message: 'Screenshot request sent to background' };
default:
return { success: false, message: `Unknown action: ${action}` };
}
} catch (error) {
return { success: false, message: `Error: ${(error as Error).message}` };
}
}
// Listen for messages from background script or sidebar
chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => {
if (request.type === 'PING') {
// Respond to ping to confirm content script is loaded
sendResponse({ success: true });
return true;
}
if (request.type === 'GET_PAGE_CONTEXT') {
const context = extractPageContext();
sendResponse(context);
return true;
}
if (request.type === 'EXECUTE_ACTION') {
const result = executePageAction(
request.action,
request.target,
request.value,
request.selector,
request.coordinates,
request.direction,
request.amount,
request.key,
request.keys,
request.destination
);
// Handle both synchronous and asynchronous results
if (result instanceof Promise) {
result.then(sendResponse);
return true; // Keep message channel open for async response
} else {
sendResponse(result);
return true;
}
}
if (request.type === 'GET_SELECTED_TEXT') {
const selectedText = window.getSelection()?.toString() || '';
sendResponse({ text: selectedText });
return true;
}
});
/**
* Send page load event to background when DOM is fully ready
* Waits for DOM to be interactive before sending message to ensure
* all page data (links, forms, etc.) is available
*/
function sendPageLoadMessage() {
chrome.runtime.sendMessage({
type: 'PAGE_LOADED',
url: window.location.href,
title: document.title,
timestamp: Date.now()
}).catch(error => {
// Silently fail if background script is not available
// (might happen on restricted pages)
console.debug('Could not send PAGE_LOADED message:', error);
});
console.log('Atlas content script loaded on:', window.location.href);
}
// Wait for DOM to be ready before sending page load message
// This prevents race conditions where page data isn't available yet
if (document.readyState === 'loading') {
// DOM is still loading, wait for it
document.addEventListener('DOMContentLoaded', sendPageLoadMessage, { once: true });
} else {
// DOM is already interactive or complete
sendPageLoadMessage();
}