Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat: async event queue with input session and new messaging protocol

Luis Ortiz47e78fce4422
diff --git a/package-lock.json b/package-lock.json
index 3d0945c..b004a60 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -24,6 +24,7 @@
"dexie": "^4.3.0",
"jspdf": "^4.2.1",
"lucide-react": "^0.577.0",
+ "p-queue": "^9.1.1",
"radix-ui": "^1.4.3",
"react": "^19.2.4",
"react-dom": "^19.2.4",
@@ -6793,6 +6794,34 @@
"node": ">= 0.4.0"
}
},
+ "node_modules/p-queue": {
+ "version": "9.1.1",
+ "resolved": "https://registry.npmjs.org/p-queue/-/p-queue-9.1.1.tgz",
+ "integrity": "sha512-yQS1vV2V7Q14MQrgD8jMNY5owPuGgVHVdSK8NqmKpOVajnjbaeMa6uLOzTALPtvJ7Vo4bw0BGsw7qfUT8z24Ig==",
+ "license": "MIT",
+ "dependencies": {
+ "eventemitter3": "^5.0.1",
+ "p-timeout": "^7.0.0"
+ },
+ "engines": {
+ "node": ">=20"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
+ "node_modules/p-timeout": {
+ "version": "7.0.1",
+ "resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-7.0.1.tgz",
+ "integrity": "sha512-AxTM2wDGORHGEkPCt8yqxOTMgpfbEHqF51f/5fJCmwFC3C/zNcGT63SymH2ttOAaiIws2zVg4+izQCjrakcwHg==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=20"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
"node_modules/package-json": {
"version": "10.0.1",
"resolved": "https://registry.npmjs.org/package-json/-/package-json-10.0.1.tgz",
diff --git a/package.json b/package.json
index 6ad83de..a5f6e08 100644
--- a/package.json
+++ b/package.json
@@ -28,6 +28,7 @@
"dexie": "^4.3.0",
"jspdf": "^4.2.1",
"lucide-react": "^0.577.0",
+ "p-queue": "^9.1.1",
"radix-ui": "^1.4.3",
"react": "^19.2.4",
"react-dom": "^19.2.4",
diff --git a/src/core/capture/events.ts b/src/core/capture/events.ts
deleted file mode 100644
index 8014754..0000000
--- a/src/core/capture/events.ts
+++ /dev/null
@@ -1,359 +0,0 @@
-import { logger } from '@/lib/logger';
-import { sendMessage } from '@/lib/messaging';
-import { extractElementMeta } from './element-meta';
-
-const FOCUSABLE_SELECTOR = 'a[href], button, input, select, textarea, [role="button"], [role="link"], [role="tab"], [role="menuitem"], [role="checkbox"], [role="radio"], [tabindex], [contenteditable="true"]';
-
-const MERGE_WINDOW_MS = 800;
-const MERGE_MAX_ABSORB = 4;
-const DEDUP_MS = 300;
-const DRAG_MIN_PX = 30;
-const INTERCEPT_DELAY_MS = 100;
-const MAX_ELEMENT_RATIO = 0.8;
-
-let lastClickTarget: Element | null = null;
-let lastClickTime = 0;
-
-function findFocusableAncestor(el: Element): HTMLElement {
- const focusable = el.closest(FOCUSABLE_SELECTOR);
- if (focusable instanceof HTMLElement) return focusable;
- if (el instanceof HTMLElement) return el;
- if (el.parentElement instanceof HTMLElement) return el.parentElement;
- return document.body;
-}
-
-function isTextField(el: Element): boolean {
- if (el instanceof HTMLInputElement) {
- const textTypes = ['text', 'email', 'password', 'search', 'tel', 'url', 'number'];
- return textTypes.includes(el.type);
- }
- return el instanceof HTMLTextAreaElement
- || (el instanceof HTMLElement && el.isContentEditable);
-}
-
-function isNavigatingClick(el: HTMLElement): boolean {
- const anchor = el.closest('a[href]');
- if (!anchor) return false;
- const href = anchor.getAttribute('href');
- if (!href || href === '#' || href.startsWith('javascript:')) return false;
- return true;
-}
-
-function isTooLarge(el: Element): boolean {
- const rect = el.getBoundingClientRect();
- return (rect.width / window.innerWidth > MAX_ELEMENT_RATIO)
- && (rect.height / window.innerHeight > MAX_ELEMENT_RATIO);
-}
-
-interface HighlightHost {
- host: HTMLElement;
- overlay: HTMLDivElement;
-}
-
-function createHighlightOverlay(): HighlightHost {
- const host = document.createElement('mimik-highlight');
- host.setAttribute('data-mimik-ignore', '');
- host.style.cssText = 'position:fixed;top:0;left:0;z-index:2147483647;pointer-events:none;';
- const shadow = host.attachShadow({ mode: 'closed' });
-
- const overlay = document.createElement('div');
- overlay.style.cssText = `
- position: fixed;
- pointer-events: none;
- outline: 2.5px dashed #F59E0B;
- outline-offset: 3px;
- border-radius: 4px;
- box-sizing: border-box;
- opacity: 0;
- transition: opacity 0.15s ease;
- `;
- shadow.appendChild(overlay);
- document.documentElement.appendChild(host);
- return { host, overlay };
-}
-
-interface MergeWindow {
- startTime: number;
- count: number;
- timer: ReturnType<typeof setTimeout> | null;
- firstTarget: Element | null;
-}
-
-function createMergeWindow(): MergeWindow {
- return { startTime: 0, count: 0, timer: null, firstTarget: null };
-}
-
-function shouldAbsorb(win: MergeWindow, target: Element): boolean {
- if (win.count === 0) return false;
- const elapsed = Date.now() - win.startTime;
- if (elapsed >= MERGE_WINDOW_MS) return false;
- if (win.count >= MERGE_MAX_ABSORB) return false;
- if (target === win.firstTarget) return false;
- return true;
-}
-
-function resetWindow(win: MergeWindow): void {
- if (win.timer) clearTimeout(win.timer);
- win.startTime = 0;
- win.count = 0;
- win.timer = null;
- win.firstTarget = null;
-}
-
-export interface CaptureHandle {
- stop: () => void;
- hideOverlay: () => void;
- showOverlay: () => void;
-}
-
-export function startCapture(guideId: string): CaptureHandle {
- let highlight: HighlightHost | null = null;
- let hoveredElement: Element | null = null;
- let hideTimer: ReturnType<typeof setTimeout> | null = null;
- let rafId: number | null = null;
- let dragStartX: number | null = null;
- let dragStartY: number | null = null;
- let dragStartElement: Element | null = null;
- let eventQueue: Promise<void> = Promise.resolve();
- const mergeWin = createMergeWindow();
-
- function enqueue(fn: () => void) {
- eventQueue = eventQueue.then(() => {
- try { fn(); } catch (err) { logger.warn('Event handler error', err); }
- });
- }
-
- function showHighlight(target: Element) {
- if (!highlight) highlight = createHighlightOverlay();
- if (hideTimer) { clearTimeout(hideTimer); hideTimer = null; }
- const rect = target.getBoundingClientRect();
- highlight.overlay.style.top = `${rect.top}px`;
- highlight.overlay.style.left = `${rect.left}px`;
- highlight.overlay.style.width = `${rect.width}px`;
- highlight.overlay.style.height = `${rect.height}px`;
- highlight.overlay.style.opacity = '1';
- }
-
- function hideHighlight() {
- if (highlight) highlight.overlay.style.opacity = '0';
- }
-
- function handleMouseOver(e: MouseEvent) {
- const rawTarget = e.target;
- if (!rawTarget || !(rawTarget instanceof Element)) return;
- if (rawTarget.closest('[data-mimik-ignore]')) return;
- const target = findFocusableAncestor(rawTarget);
- if (target === document.body || target === document.documentElement) return;
- if (isTooLarge(target)) return;
- if (target === hoveredElement) return;
- hoveredElement = target;
- if (rafId) cancelAnimationFrame(rafId);
- rafId = requestAnimationFrame(() => showHighlight(target));
- }
-
- function handleMouseOut(e: MouseEvent) {
- const related = e.relatedTarget;
- if (related instanceof Element && hoveredElement?.contains(related)) return;
- hoveredElement = null;
- if (hideTimer) clearTimeout(hideTimer);
- hideTimer = setTimeout(hideHighlight, 80);
- }
-
- function sendAction(action: string, meta: ReturnType<typeof extractElementMeta>) {
- sendMessage('userAction', { guideId, action, elementMeta: meta })
- .catch(err => logger.warn('Failed to send action', err));
- }
-
- function tryMergeOrSend(target: HTMLElement, action: string) {
- if (shouldAbsorb(mergeWin, target)) {
- mergeWin.count++;
- if (mergeWin.timer) clearTimeout(mergeWin.timer);
- mergeWin.timer = setTimeout(() => resetWindow(mergeWin), MERGE_WINDOW_MS);
- return;
- }
-
- resetWindow(mergeWin);
- mergeWin.startTime = Date.now();
- mergeWin.count = 1;
- mergeWin.firstTarget = target;
- mergeWin.timer = setTimeout(() => resetWindow(mergeWin), MERGE_WINDOW_MS);
-
- try {
- const meta = extractElementMeta(target);
- sendAction(action, meta);
- } catch (err) {
- logger.warn('Failed to capture action', err);
- }
- }
-
- function handleClick(e: MouseEvent) {
- const rawTarget = e.target;
- if (!rawTarget || !(rawTarget instanceof Element)) return;
-
- const target = findFocusableAncestor(rawTarget);
- if (target.closest('[data-mimik-ignore]')) return;
-
- const now = Date.now();
- if (target === lastClickTarget && now - lastClickTime < DEDUP_MS) return;
- lastClickTarget = target;
- lastClickTime = now;
-
- if (!isTextField(target) && !(target instanceof HTMLSelectElement) && !(target instanceof HTMLOptionElement)) {
- if (isNavigatingClick(target)) {
- e.preventDefault();
- e.stopImmediatePropagation();
-
- enqueue(() => tryMergeOrSend(target, 'click'));
-
- const anchor = target.closest('a[href]') as HTMLAnchorElement;
- if (anchor) {
- const href = anchor.href;
- requestAnimationFrame(() => {
- setTimeout(() => { window.location.href = href; }, INTERCEPT_DELAY_MS);
- });
- }
- return;
- }
- }
-
- enqueue(() => tryMergeOrSend(target, 'click'));
- }
-
- function handleAuxClick(e: MouseEvent) {
- const rawTarget = e.target;
- if (!rawTarget || !(rawTarget instanceof Element)) return;
- const target = findFocusableAncestor(rawTarget);
- if (target.closest('[data-mimik-ignore]')) return;
-
- enqueue(() => tryMergeOrSend(target, 'auxclick'));
- }
-
- function handleKeydown(e: KeyboardEvent) {
- const target = e.target instanceof HTMLElement ? e.target : document.activeElement;
- if (!target || !(target instanceof HTMLElement)) return;
- if (target.closest('[data-mimik-ignore]')) return;
-
- enqueue(() => {
- try {
- const meta = extractElementMeta(target);
- sendAction(`keydown:${e.key}`, meta);
- } catch (err) {
- logger.warn('Failed to capture keydown', err);
- }
- });
- }
-
- function handleInput(e: Event) {
- const target = e.target;
- if (!target || !(target instanceof HTMLElement)) return;
- if (!(target instanceof HTMLInputElement
- || target instanceof HTMLTextAreaElement
- || target instanceof HTMLSelectElement
- || target.isContentEditable)) return;
-
- enqueue(() => {
- try {
- const meta = extractElementMeta(target);
- sendAction('input', meta);
- } catch (err) {
- logger.warn('Failed to capture input', err);
- }
- });
- }
-
- function handleClipboard(e: ClipboardEvent) {
- const target = e.target instanceof HTMLElement ? e.target : document.activeElement;
- if (!target || !(target instanceof HTMLElement)) return;
- if (target.closest('[data-mimik-ignore]')) return;
-
- enqueue(() => {
- try {
- const meta = extractElementMeta(target);
- sendAction(e.type, meta);
- } catch (err) {
- logger.warn('Failed to capture clipboard', err);
- }
- });
- }
-
- function handleMouseDown(e: MouseEvent) {
- dragStartX = e.pageX;
- dragStartY = e.pageY;
- dragStartElement = e.target instanceof Element ? e.target : null;
- }
-
- function handleMouseUp(e: MouseEvent) {
- if (dragStartX == null || dragStartY == null || !dragStartElement) {
- dragStartX = dragStartY = null;
- dragStartElement = null;
- return;
- }
-
- const dx = Math.abs(e.pageX - dragStartX);
- const dy = Math.abs(e.pageY - dragStartY);
-
- if (dx >= DRAG_MIN_PX || dy >= DRAG_MIN_PX) {
- const target = findFocusableAncestor(dragStartElement);
- if (!target.closest('[data-mimik-ignore]')) {
- enqueue(() => {
- try {
- const meta = extractElementMeta(target);
- sendAction('drag', meta);
- } catch (err) {
- logger.warn('Failed to capture drag', err);
- }
- });
- }
- }
-
- dragStartX = dragStartY = null;
- dragStartElement = null;
- }
-
- window.addEventListener('click', handleClick, { capture: true });
- window.addEventListener('auxclick', handleAuxClick, { capture: true });
- window.addEventListener('keydown', handleKeydown, { capture: true });
- window.addEventListener('input', handleInput, { capture: true });
- window.addEventListener('copy', handleClipboard as EventListener, { capture: true });
- window.addEventListener('paste', handleClipboard as EventListener, { capture: true });
- window.addEventListener('cut', handleClipboard as EventListener, { capture: true });
- window.addEventListener('mousedown', handleMouseDown, { capture: true });
- window.addEventListener('mouseup', handleMouseUp, { capture: true });
- window.addEventListener('mouseover', handleMouseOver, { capture: true });
- window.addEventListener('mouseout', handleMouseOut, { capture: true });
-
- return {
- stop() {
- window.removeEventListener('click', handleClick, { capture: true });
- window.removeEventListener('auxclick', handleAuxClick, { capture: true });
- window.removeEventListener('keydown', handleKeydown, { capture: true });
- window.removeEventListener('input', handleInput, { capture: true });
- window.removeEventListener('copy', handleClipboard as EventListener, { capture: true });
- window.removeEventListener('paste', handleClipboard as EventListener, { capture: true });
- window.removeEventListener('cut', handleClipboard as EventListener, { capture: true });
- window.removeEventListener('mousedown', handleMouseDown, { capture: true });
- window.removeEventListener('mouseup', handleMouseUp, { capture: true });
- window.removeEventListener('mouseover', handleMouseOver, { capture: true });
- window.removeEventListener('mouseout', handleMouseOut, { capture: true });
- if (hideTimer) clearTimeout(hideTimer);
- if (rafId) cancelAnimationFrame(rafId);
- resetWindow(mergeWin);
- if (highlight) {
- highlight.host.remove();
- highlight = null;
- }
- hoveredElement = null;
- },
- hideOverlay() {
- logger.debug('hideOverlay called, highlight exists:', !!highlight);
- if (highlight) {
- highlight.host.style.display = 'none';
- logger.debug('Overlay hidden, display:', highlight.host.style.display);
- }
- },
- showOverlay() {
- logger.debug('showOverlay called');
- if (highlight) highlight.host.style.display = '';
- },
- };
-}
diff --git a/src/core/capture/events/handlers.ts b/src/core/capture/events/handlers.ts
new file mode 100644
index 0000000..4010973
--- /dev/null
+++ b/src/core/capture/events/handlers.ts
@@ -0,0 +1,233 @@
+import PQueue from 'p-queue';
+import { sendMessage } from '@/lib/messaging';
+import { extractElementMeta } from '../dom/element-meta';
+import { extractDOMContext } from '../dom/context';
+import { findFocusableAncestor, isTextField, isNavigatingClick, isTooLarge, isMimikElement } from '../dom/element-utils';
+import { HighlightManager } from './highlight';
+import { InputSession } from './input-session';
+
+const DEDUP_MS = 300;
+const DRAG_MIN_PX = 30;
+const INTERCEPT_DELAY_MS = 100;
+
+let lastClickTarget: Element | null = null;
+let lastClickTime = 0;
+
+export interface CaptureHandle {
+ stop: () => void;
+ hideOverlay: () => void;
+ showOverlay: () => void;
+}
+
+class CaptureController {
+ private hl = new HighlightManager();
+ private input: InputSession;
+ private queue = new PQueue({ concurrency: 1 });
+ private listeners: [string, EventListener][] = [];
+ private dragStartX: number | null = null;
+ private dragStartY: number | null = null;
+ private dragStartElement: Element | null = null;
+
+ constructor(private guideId: string) {
+ this.input = new InputSession(guideId, this.hl);
+ this.listeners = [
+ ['click', this.onClick.bind(this)],
+ ['auxclick', this.onAuxClick.bind(this)],
+ ['keydown', this.onKeydown.bind(this)],
+ ['input', this.onInput.bind(this)],
+ ['copy', this.onClipboard.bind(this)],
+ ['paste', this.onClipboard.bind(this)],
+ ['cut', this.onClipboard.bind(this)],
+ ['pointerdown', this.onPointerDown.bind(this)],
+ ['pointerup', this.onPointerUp.bind(this)],
+ ['dragend', this.onDragEnd.bind(this)],
+ ['mouseover', this.onMouseOver.bind(this)],
+ ['mouseout', this.onMouseOut.bind(this)],
+ ['focusin', this.onFocusIn.bind(this)],
+ ['focusout', this.onFocusOut.bind(this)],
+ ];
+ for (const [event, handler] of this.listeners) {
+ window.addEventListener(event, handler, { capture: true });
+ }
+ }
+
+ private async captureAction(action: string, target: HTMLElement) {
+ this.hl.hideInstant();
+ try {
+ await sendMessage('captureStep', {
+ guideId: this.guideId,
+ action,
+ elementMeta: extractElementMeta(target),
+ domContext: extractDOMContext(target, action),
+ });
+ } finally {
+ this.hl.showInstant();
+ }
+ }
+
+ private resolveTarget(raw: EventTarget | null): HTMLElement | null {
+ if (!raw || !(raw instanceof Element) || isMimikElement(raw)) return null;
+ const target = findFocusableAncestor(raw);
+ if (target === document.body || target === document.documentElement || isTooLarge(target)) return null;
+ return target;
+ }
+
+ private onMouseOver(e: Event) {
+ const target = this.resolveTarget((e as MouseEvent).target);
+ if (target) this.hl.scheduleShow(target);
+ }
+
+ private onMouseOut(e: Event) {
+ const related = (e as MouseEvent).relatedTarget;
+ if (related instanceof Element && this.hl.hoveredElement?.contains(related)) return;
+ this.hl.scheduleHide();
+ }
+
+ private onFocusIn(e: Event) {
+ const target = this.resolveTarget((e as FocusEvent).target);
+ if (target) this.hl.scheduleShow(target);
+ }
+
+ private onClick(e: Event) {
+ const me = e as MouseEvent;
+ const raw = me.target;
+ if (!raw || !(raw instanceof Element)) return;
+ const target = findFocusableAncestor(raw);
+ if (isMimikElement(target)) return;
+
+ const now = Date.now();
+ if (target === lastClickTarget && now - lastClickTime < DEDUP_MS) return;
+ lastClickTarget = target;
+ lastClickTime = now;
+
+ if (isTextField(target)) {
+ this.queue.add(async () => {
+ if (this.input.active && this.input.target !== target) await this.input.finalize();
+ if (!this.input.active) await this.input.start(target);
+ });
+ return;
+ }
+
+ if (isNavigatingClick(target)) {
+ me.preventDefault();
+ me.stopImmediatePropagation();
+ this.queue.add(() => this.captureAction('click', target));
+ const anchor = target.closest('a[href]') as HTMLAnchorElement;
+ if (anchor) {
+ const href = anchor.href;
+ requestAnimationFrame(() => setTimeout(() => { window.location.href = href; }, INTERCEPT_DELAY_MS));
+ }
+ return;
+ }
+
+ this.queue.add(() => this.captureAction('click', target));
+ }
+
+ private onAuxClick(e: Event) {
+ const raw = (e as MouseEvent).target;
+ if (!raw || !(raw instanceof Element)) return;
+ const target = findFocusableAncestor(raw);
+ if (isMimikElement(target)) return;
+ this.queue.add(() => this.captureAction('auxclick', target));
+ }
+
+ private onKeydown(e: Event) {
+ const ke = e as KeyboardEvent;
+ const target = ke.target instanceof HTMLElement ? ke.target : document.activeElement;
+ if (!target || !(target instanceof HTMLElement) || isMimikElement(target)) return;
+
+ if (this.input.active && (ke.key === 'Enter' || ke.key === 'Escape')) {
+ this.queue.add(() => this.input.finalize());
+ return;
+ }
+
+ if (isTextField(target)) return;
+ this.queue.add(() => this.captureAction(`keydown:${ke.key}`, target));
+ }
+
+ private onInput(e: Event) {
+ const target = e.target;
+ if (!target || !(target instanceof HTMLElement)) return;
+ if (!(target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement || target.isContentEditable)) return;
+
+ if (target instanceof HTMLSelectElement) {
+ this.queue.add(() => this.captureAction('input', target));
+ return;
+ }
+
+ if (this.input.active && this.input.target !== target) {
+ this.queue.add(() => this.input.finalize());
+ }
+
+ if (!this.input.active) {
+ this.queue.add(() => this.input.start(target));
+ } else {
+ this.input.update(target);
+ }
+ }
+
+ private onFocusOut(e: Event) {
+ if (!this.input.active) return;
+ const related = (e as FocusEvent).relatedTarget;
+ if (related instanceof Element && related === this.input.target) return;
+ this.queue.add(() => this.input.finalize());
+ }
+
+ private onClipboard(e: Event) {
+ const target = (e as ClipboardEvent).target instanceof HTMLElement ? (e as ClipboardEvent).target as HTMLElement : document.activeElement;
+ if (!target || !(target instanceof HTMLElement) || isMimikElement(target)) return;
+ this.queue.add(() => this.captureAction(e.type, target));
+ }
+
+ private onPointerDown(e: Event) {
+ const pe = e as PointerEvent;
+ this.dragStartX = pe.pageX;
+ this.dragStartY = pe.pageY;
+ this.dragStartElement = pe.target instanceof Element ? pe.target : null;
+ }
+
+ private onPointerUp(e: Event) {
+ const pe = e as PointerEvent;
+ if (this.dragStartX == null || this.dragStartY == null || !this.dragStartElement) {
+ this.dragStartX = this.dragStartY = null;
+ this.dragStartElement = null;
+ return;
+ }
+
+ const dx = Math.abs(pe.pageX - this.dragStartX);
+ const dy = Math.abs(pe.pageY - this.dragStartY);
+
+ if (dx >= DRAG_MIN_PX || dy >= DRAG_MIN_PX) {
+ const target = findFocusableAncestor(this.dragStartElement);
+ if (!isMimikElement(target)) this.queue.add(() => this.captureAction('drag', target));
+ }
+
+ this.dragStartX = this.dragStartY = null;
+ this.dragStartElement = null;
+ }
+
+ private onDragEnd(e: Event) {
+ if (!e.target || !(e.target instanceof Element) || isMimikElement(e.target)) return;
+ this.queue.add(() => this.captureAction('drag', findFocusableAncestor(e.target as Element)));
+ }
+
+ stop() {
+ for (const [event, handler] of this.listeners) {
+ window.removeEventListener(event, handler, { capture: true });
+ }
+ this.queue.add(() => this.input.finalize());
+ this.hl.dispose();
+ }
+
+ hideOverlay() { this.hl.hideInstant(); }
+ showOverlay() { this.hl.showInstant(); }
+}
+
+export function startCapture(guideId: string): CaptureHandle {
+ const controller = new CaptureController(guideId);
+ return {
+ stop: () => controller.stop(),
+ hideOverlay: () => controller.hideOverlay(),
+ showOverlay: () => controller.showOverlay(),
+ };
+}
diff --git a/src/core/capture/events/highlight.ts b/src/core/capture/events/highlight.ts
new file mode 100644
index 0000000..3f0610a
--- /dev/null
+++ b/src/core/capture/events/highlight.ts
@@ -0,0 +1,71 @@
+export interface Highlight {
+ host: HTMLElement;
+ overlay: HTMLDivElement;
+}
+
+export function createHighlight(): Highlight {
+ const host = document.createElement('mimik-highlight');
+ host.setAttribute('data-mimik-ignore', '');
+ host.style.cssText = 'position:fixed;top:0;left:0;z-index:2147483647;pointer-events:none;';
+ const shadow = host.attachShadow({ mode: 'closed' });
+ const overlay = document.createElement('div');
+ overlay.style.cssText = `
+ position: fixed; pointer-events: none;
+ outline: 2.5px dashed #F59E0B; outline-offset: 3px;
+ border-radius: 4px; box-sizing: border-box;
+ opacity: 0; transition: opacity 0.15s ease;
+ `;
+ shadow.appendChild(overlay);
+ document.documentElement.appendChild(host);
+ return { host, overlay };
+}
+
+export class HighlightManager {
+ private highlight: Highlight | null = null;
+ private hideTimer: ReturnType<typeof setTimeout> | null = null;
+ private rafId: number | null = null;
+ hoveredElement: Element | null = null;
+
+ show(target: Element) {
+ if (!this.highlight) this.highlight = createHighlight();
+ if (this.hideTimer) { clearTimeout(this.hideTimer); this.hideTimer = null; }
+ const rect = target.getBoundingClientRect();
+ this.highlight.overlay.style.top = `${rect.top}px`;
+ this.highlight.overlay.style.left = `${rect.left}px`;
+ this.highlight.overlay.style.width = `${rect.width}px`;
+ this.highlight.overlay.style.height = `${rect.height}px`;
+ this.highlight.overlay.style.opacity = '1';
+ }
+
+ hide() {
+ if (this.highlight) this.highlight.overlay.style.opacity = '0';
+ }
+
+ hideInstant() {
+ if (this.highlight) this.highlight.host.style.display = 'none';
+ }
+
+ showInstant() {
+ if (this.highlight) this.highlight.host.style.display = '';
+ }
+
+ scheduleShow(target: Element) {
+ if (target === this.hoveredElement) return;
+ this.hoveredElement = target;
+ if (this.rafId) cancelAnimationFrame(this.rafId);
+ this.rafId = requestAnimationFrame(() => this.show(target));
+ }
+
+ scheduleHide() {
+ this.hoveredElement = null;
+ if (this.hideTimer) clearTimeout(this.hideTimer);
+ this.hideTimer = setTimeout(() => this.hide(), 80);
+ }
+
+ dispose() {
+ if (this.hideTimer) clearTimeout(this.hideTimer);
+ if (this.rafId) cancelAnimationFrame(this.rafId);
+ if (this.highlight) { this.highlight.host.remove(); this.highlight = null; }
+ this.hoveredElement = null;
+ }
+}
diff --git a/src/core/capture/events/input-session.ts b/src/core/capture/events/input-session.ts
new file mode 100644
index 0000000..1b461fb
--- /dev/null
+++ b/src/core/capture/events/input-session.ts
@@ -0,0 +1,65 @@
+import { sendMessage } from '@/lib/messaging';
+import { logger } from '@/lib/logger';
+import { extractElementMeta } from '../dom/element-meta';
+import { extractDOMContext } from '../dom/context';
+import { getFieldValue, getFieldLabel } from '../dom/element-utils';
+import type { HighlightManager } from './highlight';
+
+export class InputSession {
+ stepId: string | null = null;
+ target: HTMLElement | null = null;
+
+ private guideId: string;
+ private hl: HighlightManager;
+
+ constructor(guideId: string, hl: HighlightManager) {
+ this.guideId = guideId;
+ this.hl = hl;
+ }
+
+ get active() { return this.stepId !== null; }
+
+ async start(target: HTMLElement) {
+ this.hl.hideInstant();
+ try {
+ const res = await sendMessage('captureStep', {
+ guideId: this.guideId,
+ action: 'input',
+ elementMeta: extractElementMeta(target),
+ domContext: extractDOMContext(target, 'input'),
+ });
+ if ('stepId' in res) {
+ this.stepId = res.stepId;
+ this.target = target;
+ }
+ } finally {
+ this.hl.showInstant();
+ }
+ }
+
+ update(target: HTMLElement) {
+ if (!this.stepId) return;
+ const val = getFieldValue(target);
+ const desc = val ? `Type "${val}" in ${getFieldLabel(target)}` : `Clear ${getFieldLabel(target)}`;
+ sendMessage('updateInputStep', { stepId: this.stepId, description: desc })
+ .catch(err => logger.warn('Failed to update input step', err));
+ }
+
+ async finalize() {
+ if (!this.target || !this.stepId) return;
+ const target = this.target;
+ const stepId = this.stepId;
+ this.stepId = null;
+ this.target = null;
+ this.hl.hideInstant();
+ try {
+ await sendMessage('finalizeInputStep', {
+ stepId,
+ elementMeta: extractElementMeta(target),
+ domContext: extractDOMContext(target, 'input'),
+ });
+ } finally {
+ this.hl.showInstant();
+ }
+ }
+}
diff --git a/src/core/capture/screenshot.ts b/src/core/capture/screenshot.ts
deleted file mode 100644
index 87385d8..0000000
--- a/src/core/capture/screenshot.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import { captureVisibleTab } from '@/lib/browser-api';
-import { saveScreenshot } from '@/core/guides/service';
-import type { Screenshot, ElementMeta } from '@/core/guides/types';
-
-export async function captureAndStore(tabId: number, stepId: string): Promise<Screenshot> {
- const dataUrl = await captureVisibleTab('jpeg', 90);
- const blob = await fetch(dataUrl).then(r => r.blob());
- const screenshot: Screenshot = {
- id: crypto.randomUUID(), stepId, blob, mimeType: 'image/jpeg', width: 0, height: 0,
- };
- await saveScreenshot(screenshot);
- return screenshot;
-}
-
-export function dataUrlToBlob(dataUrl: string): Promise<Blob> {
- return fetch(dataUrl).then(r => r.blob());
-}
-
-export async function captureAnnotated(
- tabId: number,
- stepId: string,
- elementMeta: ElementMeta
-): Promise<Screenshot> {
- const dataUrl = await captureVisibleTab('jpeg', 90);
-
- const blob = await fetch(dataUrl).then(r => r.blob());
- const img = await createImageBitmap(blob);
-
- const screenshot: Screenshot = {
- id: crypto.randomUUID(),
- stepId,
- blob,
- mimeType: 'image/jpeg',
- width: img.width,
- height: img.height,
- bounds: {
- x: elementMeta.rect.x,
- y: elementMeta.rect.y,
- width: elementMeta.rect.width,
- height: elementMeta.rect.height,
- },
- pixelRatio: elementMeta.devicePixelRatio,
- };
-
- img.close();
- await saveScreenshot(screenshot);
- return screenshot;
-}
diff --git a/src/lib/messaging.ts b/src/lib/messaging.ts
index 98a394e..f423bce 100644
--- a/src/lib/messaging.ts
+++ b/src/lib/messaging.ts
@@ -1,5 +1,6 @@
import { defineExtensionMessaging } from '@webext-core/messaging';
import type { ElementMeta } from '@/core/guides/types';
+import type { DOMContext } from '@/core/capture/dom/context';
import type { CaptureStateValue } from '@/core/capture/machine';
export interface GetStateResponse {
@@ -21,17 +22,37 @@ export interface StopRecordingResponse {
guideId?: string;
}
-export interface UserActionData {
+export interface CaptureStepData {
guideId: string;
action: string;
elementMeta: ElementMeta;
+ domContext?: DOMContext;
}
-export type UserActionResponse =
+export type CaptureStepResponse =
| { stepId: string }
| { ignored: true }
| { error: string };
+export interface UpdateInputStepData {
+ stepId: string;
+ description: string;
+}
+
+export interface UpdateInputStepResponse {
+ updated: boolean;
+}
+
+export interface FinalizeInputStepData {
+ stepId: string;
+ elementMeta: ElementMeta;
+ domContext?: DOMContext;
+}
+
+export interface FinalizeInputStepResponse {
+ updated: boolean;
+}
+
export interface RrwebChunkData {
guideId: string;
events: unknown[];
@@ -46,7 +67,9 @@ interface MimikProtocol {
getState(): GetStateResponse;
startRecording(data: StartRecordingData): StartRecordingResponse;
stopRecording(): StopRecordingResponse;
- userAction(data: UserActionData): UserActionResponse;
+ captureStep(data: CaptureStepData): CaptureStepResponse;
+ updateInputStep(data: UpdateInputStepData): UpdateInputStepResponse;
+ finalizeInputStep(data: FinalizeInputStepData): FinalizeInputStepResponse;
rrwebChunk(data: RrwebChunkData): RrwebChunkResponse;
}