Skip to content

Commit cdfcb6c

Browse files
committed
added red highlight for input elements
1 parent 977b066 commit cdfcb6c

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

extension/src/entrypoints/content.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ function startRecorder() {
208208
document.addEventListener('keydown', handleKeydown, true);
209209
document.addEventListener('mouseover', handleMouseOver, true);
210210
document.addEventListener('mouseout', handleMouseOut, true);
211+
document.addEventListener('focus', handleFocus, true);
212+
document.addEventListener('blur', handleBlur, true);
211213
console.log('Permanently attached custom event listeners.');
212214
}
213215

@@ -225,6 +227,8 @@ function stopRecorder() {
225227
document.removeEventListener('keydown', handleKeydown, true); // Remove keydown listener
226228
document.removeEventListener('mouseover', handleMouseOver, true);
227229
document.removeEventListener('mouseout', handleMouseOut, true);
230+
document.removeEventListener('focus', handleFocus, true);
231+
document.removeEventListener('blur', handleBlur, true);
228232
} else {
229233
console.log('Recorder not running, cannot stop.');
230234
}
@@ -397,6 +401,7 @@ function handleKeydown(event: KeyboardEvent) {
397401

398402
// Store the current overlay to manage its lifecycle
399403
let currentOverlay: HTMLDivElement | null = null;
404+
let currentFocusOverlay: HTMLDivElement | null = null;
400405

401406
// Handle mouseover to create overlay
402407
function handleMouseOver(event: MouseEvent) {
@@ -474,6 +479,69 @@ function handleMouseOut(event: MouseEvent) {
474479
}
475480
}
476481

482+
// Handle focus to create red overlay for input elements
483+
function handleFocus(event: FocusEvent) {
484+
if (!isRecordingActive) return;
485+
const targetElement = event.target as HTMLElement;
486+
if (
487+
!targetElement ||
488+
!['INPUT', 'TEXTAREA', 'SELECT'].includes(targetElement.tagName)
489+
)
490+
return;
491+
492+
// Remove any existing focus overlay to avoid duplicates
493+
if (currentFocusOverlay) {
494+
currentFocusOverlay.remove();
495+
currentFocusOverlay = null;
496+
}
497+
498+
try {
499+
const xpath = getXPath(targetElement);
500+
let elementToHighlight: HTMLElement | null = document.evaluate(
501+
xpath,
502+
document,
503+
null,
504+
XPathResult.FIRST_ORDERED_NODE_TYPE,
505+
null
506+
).singleNodeValue as HTMLElement | null;
507+
if (!elementToHighlight) {
508+
const enhancedSelector = getEnhancedCSSSelector(targetElement, xpath);
509+
elementToHighlight = document.querySelector(enhancedSelector);
510+
}
511+
if (elementToHighlight) {
512+
const rect = elementToHighlight.getBoundingClientRect();
513+
const focusOverlay = document.createElement('div');
514+
focusOverlay.className = 'focus-overlay';
515+
Object.assign(focusOverlay.style, {
516+
position: 'absolute',
517+
top: `${rect.top + window.scrollY}px`,
518+
left: `${rect.left + window.scrollX}px`,
519+
width: `${rect.width}px`,
520+
height: `${rect.height}px`,
521+
border: '2px solid red',
522+
backgroundColor: 'rgba(255, 0, 0, 0.2)', // Red tint
523+
pointerEvents: 'none',
524+
zIndex: '2147483100', // Higher than mouseover overlay (2147483000)
525+
});
526+
document.body.appendChild(focusOverlay);
527+
currentFocusOverlay = focusOverlay;
528+
} else {
529+
console.warn('No element found to highlight for focus, xpath:', xpath);
530+
}
531+
} catch (error) {
532+
console.error('Error creating focus overlay:', error);
533+
}
534+
}
535+
536+
// Handle blur to remove focus overlay
537+
function handleBlur(event: FocusEvent) {
538+
if (!isRecordingActive) return;
539+
if (currentFocusOverlay) {
540+
currentFocusOverlay.remove();
541+
currentFocusOverlay = null;
542+
}
543+
}
544+
477545
export default defineContentScript({
478546
matches: ['<all_urls>'],
479547
main(ctx) {
@@ -528,6 +596,8 @@ export default defineContentScript({
528596
document.removeEventListener('keydown', handleKeydown, true);
529597
document.removeEventListener('mouseover', handleMouseOver, true);
530598
document.removeEventListener('mouseout', handleMouseOut, true);
599+
document.removeEventListener('focus', handleFocus, true);
600+
document.removeEventListener('blur', handleBlur, true);
531601
stopRecorder(); // Ensure rrweb is stopped
532602
});
533603

0 commit comments

Comments
 (0)