Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(chrome-extension): add content scripts and recording overlay
Richie McIlroy08177f8cfc7d
diff --git a/apps/chrome-extension/src/content/bootstrap.ts b/apps/chrome-extension/src/content/bootstrap.tsnew file mode 100644index 000000000..7369689b3--- /dev/null+++ b/apps/chrome-extension/src/content/bootstrap.ts@@ -0,0 +1,161 @@+import {+ isOverlayMessage,+ isRecordingStatusBroadcast,+} from "../shared/messages";+import {+ RECORDING_STATE_KEY,+ SHARED_UI_STATE_KEY,+} from "../shared/storage-keys";++// The manifest injects only this bootstrap into every page: a few KB of+// vanilla code that decides whether the page actually needs the recorder UI.+// The full overlay/recording-bar bundle (React, icons, ~250KB) is an ES+// module listed in web_accessible_resources and is dynamically imported only+// once recording state or a service-worker message says this tab should show+// something. Plain page loads must stay cheap: reading chrome.storage does+// not wake the MV3 service worker, so the bootstrap never sends runtime+// messages of its own.++type OverlayModule = {+ init: (startupMessages: readonly unknown[]) => void;+};++// The phases for which the floating UI (recording bar, camera preview)+// renders. Every other phase needs no UI until a message or a storage change+// says otherwise: "error" reopens the recorder panel through the shared UI+// state's panelOpen flag, which is watched below.+const UI_PHASES = new Set(["creating", "recording", "paused"]);++const BOOTSTRAP_FLAG = "__capExtensionContentBootstrap";++const readPhase = (value: unknown): string | null => {+ if (!value || typeof value !== "object") return null;+ const status = (value as { status?: unknown }).status;+ if (!status || typeof status !== "object") return null;+ const phase = (status as { phase?: unknown }).phase;+ return typeof phase === "string" ? phase : null;+};++const isUiPhase = (value: unknown) => {+ const phase = readPhase(value);+ return phase !== null && UI_PHASES.has(phase);+};++const readPanelOpen = (value: unknown): boolean =>+ !!value &&+ typeof value === "object" &&+ (value as { panelOpen?: unknown }).panelOpen === true;++const bootstrap = () => {+ const overlayModuleUrl = chrome.runtime.getURL("content/overlay.js");+ // Messages acknowledged while the overlay module is still being fetched.+ // init() hands them to the module, whose components replay them on mount,+ // so the panel toggle or webcam settings push that triggered the lazy+ // load is not dropped.+ const pendingMessages: unknown[] = [];+ let modulePromise: Promise<void> | null = null;+ let moduleStarted = false;++ const startOverlayModule = () => {+ modulePromise ??= import(/* @vite-ignore */ overlayModuleUrl)+ .then((module: OverlayModule) => {+ moduleStarted = true;+ // The module registers its own runtime and storage listeners;+ // from here the bootstrap goes dormant. Messages arriving in the+ // brief window before the module's listeners mount are covered by+ // the service worker's send retries and the storage mirror.+ chrome.runtime.onMessage.removeListener(handleRuntimeMessage);+ chrome.storage.onChanged.removeListener(handleStorageChange);+ module.init(pendingMessages);+ })+ .catch(() => {+ // Leave the trigger listeners armed so a later signal retries.+ modulePromise = null;+ });+ return modulePromise;+ };++ const handleStorageChange = (+ changes: Record<string, chrome.storage.StorageChange>,+ areaName: string,+ ) => {+ if (areaName !== "session") return;+ if (+ isUiPhase(changes[RECORDING_STATE_KEY]?.newValue) ||+ readPanelOpen(changes[SHARED_UI_STATE_KEY]?.newValue)+ ) {+ void startOverlayModule();+ }+ };++ const handleRuntimeMessage = (+ message: unknown,+ _sender: chrome.runtime.MessageSender,+ sendResponse: (response?: unknown) => void,+ ) => {+ if (moduleStarted) return false;++ if (isOverlayMessage(message)) {+ // Acknowledge like the full overlay does so the service worker's+ // delivery check (and its inject-and-retry fallback) sees this tab+ // as alive.+ sendResponse({ ok: true });+ if (+ message.type === "overlay-hide" ||+ message.type === "overlay-enter-auto-pip" ||+ message.type === "overlay-exit-auto-pip"+ ) {+ // Nothing is mounted, so there is nothing to hide or to move+ // into Picture in Picture; loading the UI just to no-op is waste.+ return false;+ }+ pendingMessages.push(message);+ void startOverlayModule();+ return false;+ }++ if (+ isRecordingStatusBroadcast(message) &&+ UI_PHASES.has(message.status.phase)+ ) {+ pendingMessages.push(message);+ void startOverlayModule();+ }++ return false;+ };++ chrome.runtime.onMessage.addListener(handleRuntimeMessage);+ chrome.storage.onChanged.addListener(handleStorageChange);++ // One cheap session-storage read decides whether this page needs UI right+ // away: a recording in progress or the recorder panel open (the panel+ // follows the user across tabs).+ try {+ chrome.storage.session.get(+ [RECORDING_STATE_KEY, SHARED_UI_STATE_KEY],+ (items) => {+ if (chrome.runtime.lastError || !items) return;+ if (+ isUiPhase(items[RECORDING_STATE_KEY]) ||+ readPanelOpen(items[SHARED_UI_STATE_KEY])+ ) {+ void startOverlayModule();+ }+ },+ );+ } catch {+ // Session storage access is widened by the service worker on startup;+ // until that has happened there is no recording state to show either.+ }+};++// chrome.scripting.executeScript re-runs this file in the same isolated+// world (the service worker injects it before messaging tabs that predate+// the extension), so a second execution must not stack duplicate listeners+// or reload the overlay module.+const globalScope = globalThis as Record<string, unknown>;+if (globalScope[BOOTSTRAP_FLAG] !== true) {+ globalScope[BOOTSTRAP_FLAG] = true;+ bootstrap();+}diff --git a/apps/chrome-extension/src/content/overlay.css b/apps/chrome-extension/src/content/overlay.cssnew file mode 100644index 000000000..042b56a97--- /dev/null+++ b/apps/chrome-extension/src/content/overlay.css@@ -0,0 +1,638 @@+:host {+ all: initial;+ position: fixed;+ inset: 0;+ z-index: 2147483647;+ display: block;+ overflow: visible;+ pointer-events: none;+ color-scheme: light;+ isolation: isolate;+ contain: layout style paint;+}++:host,+:host * {+ box-sizing: border-box;+ font-synthesis: none;+ text-shadow: none;+ text-transform: none;+}++.cap-extension-camera-window {+ position: fixed;+ left: 0;+ top: 0;+ z-index: 2147483647;+ cursor: move;+ pointer-events: auto;+ touch-action: none;+ user-select: none;+ will-change: transform;+ color: #202020;+ font-family:+ Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,+ "Segoe UI", sans-serif;+ font-size: 14px;+ font-weight: 400;+ letter-spacing: 0;+ line-height: 1.2;+}++.cap-extension-camera-window.is-dragging {+ cursor: grabbing;+}++.cap-extension-drag-surface {+ all: unset;+ position: fixed;+ inset: 0;+ display: block;+ z-index: 2147483647;+ cursor: grabbing;+ pointer-events: auto;+ touch-action: none;+ user-select: none;+}++.cap-extension-camera-shell {+ position: relative;+ display: flex;+ width: 100%;+ height: 100%;+ flex-direction: column;+ cursor: move;+}++.cap-extension-camera-bar {+ display: flex;+ height: 52px;+ align-items: flex-start;+ justify-content: center;+ pointer-events: none;+}++.cap-extension-camera-controls {+ display: flex;+ align-items: center;+ gap: 4px;+ padding: 4px;+ border: 1px solid rgba(255, 255, 255, 0.2);+ border-radius: 12px;+ background: #fcfcfc;+ box-shadow: 0 16px 48px rgba(0, 0, 0, 0.16);+ color: #838383;+ opacity: 0;+ pointer-events: auto;+ transform: translateY(8px);+ transition:+ opacity 160ms ease,+ transform 160ms ease;+}++.cap-extension-camera-window:hover .cap-extension-camera-controls,+.cap-extension-camera-controls:focus-within {+ opacity: 1;+ transform: translateY(0);+}++.cap-extension-camera-control {+ all: unset;+ display: flex;+ width: 38px;+ height: 38px;+ align-items: center;+ justify-content: center;+ border-radius: 8px;+ color: #838383;+ cursor: pointer;+ transition:+ background 160ms ease,+ color 160ms ease,+ opacity 160ms ease;+}++.cap-extension-camera-control:hover,+.cap-extension-camera-control:focus-visible,+.cap-extension-camera-control.is-active {+ background: #f0f0f0;+ color: #202020;+}++.cap-extension-camera-control:focus-visible {+ outline: 1px solid #5eb1ef;+ outline-offset: 1px;+}++.cap-extension-camera-control svg {+ display: block;+ width: 22px;+ height: 22px;+ flex: 0 0 auto;+}++.cap-extension-camera-control:disabled {+ opacity: 0.4;+ cursor: not-allowed;+}++.cap-extension-parent-pip-video {+ position: fixed;+ right: 0;+ bottom: 0;+ width: 1px;+ height: 1px;+ opacity: 0;+ pointer-events: none;+}++.cap-extension-camera-frame {+ position: relative;+ overflow: hidden;+ border: 0;+ background: #000;+ box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);+ color: #fff;+}++.cap-extension-camera-iframe {+ position: absolute;+ inset: 0;+ display: block;+ width: 100%;+ height: 100%;+ border: 0;+ background: #000;+ pointer-events: auto;+}++.cap-extension-camera-last-frame {+ position: absolute;+ inset: 0;+ z-index: 1;+ display: block;+ width: 100%;+ height: 100%;+ object-fit: cover;+ opacity: 1;+ pointer-events: none;+ transition: opacity 140ms ease;+}++.cap-extension-camera-last-frame.is-hidden {+ opacity: 0;+}++.cap-extension-camera-last-frame.is-mirrored {+ transform: scaleX(-1);+}++.cap-extension-camera-frame.is-pip .cap-extension-camera-iframe,+.cap-extension-camera-frame.is-pip .cap-extension-camera-last-frame {+ visibility: hidden;+}++.cap-extension-camera-loading,+.cap-extension-camera-error,+.cap-extension-camera-pip-active {+ position: absolute;+ inset: 0;+ z-index: 2;+ display: flex;+ align-items: center;+ justify-content: center;+}++.cap-extension-camera-loading {+ background: rgba(0, 0, 0, 0.26);+}++.cap-extension-camera-loading span {+ display: block;+ width: 28px;+ height: 28px;+ border: 2px solid rgba(255, 255, 255, 0.3);+ border-top-color: rgba(255, 255, 255, 0.9);+ border-radius: 999px;+ animation: cap-extension-spin 760ms linear infinite;+}++.cap-extension-camera-error {+ padding: 18px;+ background: rgba(0, 0, 0, 0.82);+ color: rgba(255, 255, 255, 0.9);+ font-size: 12px;+ font-weight: 500;+ line-height: 1.35;+ text-align: center;+}++@keyframes cap-extension-spin {+ to {+ transform: rotate(360deg);+ }+}++.cap-extension-camera-pip-active {+ background: linear-gradient(180deg, #1b1c21 0%, #101114 100%);+}++.cap-extension-camera-pip-active > div {+ display: flex;+ align-items: center;+ gap: 8px;+ max-width: calc(100% - 24px);+ padding: 8px 12px;+ border: 1px solid rgba(255, 255, 255, 0.1);+ border-radius: 999px;+ background: rgba(0, 0, 0, 0.6);+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.24);+ backdrop-filter: blur(12px);+ color: rgba(255, 255, 255, 0.9);+ font-size: 12px;+ font-weight: 500;+ white-space: nowrap;+}++.cap-extension-camera-pip-active button {+ all: unset;+ display: flex;+ width: 16px;+ height: 16px;+ align-items: center;+ justify-content: center;+ border-radius: 999px;+ cursor: pointer;+ transition: background 160ms ease;+}++.cap-extension-camera-pip-active button:hover,+.cap-extension-camera-pip-active button:focus-visible {+ background: rgba(255, 255, 255, 0.2);+}++.cap-extension-camera-pip-active button:focus-visible {+ outline: 1px solid rgba(255, 255, 255, 0.65);+ outline-offset: 1px;+}++.cap-extension-panel-backdrop {+ all: unset;+ position: fixed;+ inset: 0;+ z-index: 2147483646;+ display: block;+ background: transparent;+ pointer-events: auto;+ cursor: default;+}++.cap-extension-panel-backdrop:focus-visible {+ outline: none;+}++.cap-extension-panel {+ position: fixed;+ top: 16px;+ right: 16px;+ z-index: 2147483647;+ overflow: hidden;+ border: 1px solid rgba(18, 18, 23, 0.1);+ border-radius: 20px;+ background: #f9f9f9;+ box-shadow:+ 0 24px 64px rgba(0, 0, 0, 0.28),+ 0 4px 16px rgba(0, 0, 0, 0.12);+ pointer-events: auto;+ transition: height 180ms ease;+ animation: cap-extension-panel-in 240ms cubic-bezier(0.16, 1, 0.3, 1);+ will-change: transform, height;+}++.cap-extension-panel-iframe {+ display: block;+ width: 100%;+ height: 100%;+ border: 0;+ background: transparent;+}++@keyframes cap-extension-panel-in {+ from {+ opacity: 0;+ transform: translateY(-10px) scale(0.98);+ }+ to {+ opacity: 1;+ transform: translateY(0) scale(1);+ }+}++.cap-extension-control-bar {+ position: fixed;+ z-index: 2147483647;+ display: flex;+ align-items: center;+ gap: 12px;+ padding: 10px 12px;+ border: 1px solid rgba(18, 18, 23, 0.08);+ border-radius: 18px;+ background: rgba(252, 252, 252, 0.97);+ box-shadow:+ 0 24px 64px rgba(0, 0, 0, 0.28),+ 0 4px 16px rgba(0, 0, 0, 0.12);+ color: #202020;+ cursor: grab;+ pointer-events: auto;+ user-select: none;+ backdrop-filter: blur(20px);+ font-family:+ Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,+ "Segoe UI", sans-serif;+ font-size: 13px;+ letter-spacing: 0;+ line-height: 1;+ animation: cap-extension-bar-in 260ms cubic-bezier(0.16, 1, 0.3, 1);+}++.cap-extension-control-bar.is-dragging {+ cursor: grabbing;+}++.cap-extension-control-bar-info {+ display: flex;+ align-items: center;+ gap: 10px;+ padding-left: 2px;+}++.cap-extension-control-bar-logo {+ display: block;+ width: 30px;+ height: 30px;+ flex: 0 0 auto;+ border-radius: 8px;+ pointer-events: none;+}++.cap-extension-control-bar-text {+ display: flex;+ flex-direction: column;+ gap: 4px;+}++.cap-extension-control-bar-title {+ font-size: 14px;+ font-weight: 600;+ line-height: 1;+ color: #18181b;+ white-space: nowrap;+}++.cap-extension-control-bar-title.is-timer {+ min-width: 44px;+ font-size: 15px;+ font-variant-numeric: tabular-nums;+}++.cap-extension-control-bar-title.is-warning {+ color: #e5484d;+}++.cap-extension-control-bar-subtitle {+ display: flex;+ align-items: center;+ gap: 5px;+ font-size: 11.5px;+ font-weight: 500;+ line-height: 1;+ color: #83838b;+ white-space: nowrap;+}++.cap-extension-control-bar-dot {+ width: 8px;+ height: 8px;+ flex: 0 0 auto;+ border-radius: 999px;+}++.cap-extension-control-bar-dot.is-recording {+ background: #e5484d;+ box-shadow: 0 0 0 3px rgba(229, 72, 77, 0.18);+ animation: cap-extension-pulse 1.6s ease-in-out infinite;+}++.cap-extension-control-bar-dot.is-creating {+ background: #f59f00;+ box-shadow: 0 0 0 3px rgba(245, 159, 0, 0.18);+ animation: cap-extension-pulse 1.6s ease-in-out infinite;+}++.cap-extension-control-bar-dot.is-paused {+ background: #b4b4b4;+}++.cap-extension-control-bar-dot.is-ready {+ background: #3b82f6;+ box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.16);+}++.cap-extension-control-bar-divider {+ width: 1px;+ align-self: stretch;+ margin: 2px 0;+ background: rgba(18, 18, 23, 0.08);+}++.cap-extension-control-bar-actions {+ display: flex;+ align-items: center;+ gap: 6px;+}++.cap-extension-control-bar-icon-button {+ all: unset;+ box-sizing: border-box;+ display: flex;+ width: 40px;+ height: 40px;+ align-items: center;+ justify-content: center;+ border-radius: 12px;+ color: #5f5f66;+ cursor: pointer;+ transition:+ background 160ms ease,+ color 160ms ease;+}++.cap-extension-control-bar-icon-button svg {+ display: block;+ flex: 0 0 auto;+}++.cap-extension-control-bar-icon-button:hover,+.cap-extension-control-bar-icon-button:focus-visible {+ background: rgba(18, 18, 23, 0.06);+ color: #18181b;+}++.cap-extension-control-bar-icon-button:focus-visible {+ outline: 2px solid #5eb1ef;+ outline-offset: 1px;+}++.cap-extension-control-bar-icon-button:disabled {+ opacity: 0.45;+ cursor: not-allowed;+}++.cap-extension-control-bar-icon-button.is-quiet {+ color: #9b9ba3;+}++.cap-extension-control-bar-pill {+ all: unset;+ box-sizing: border-box;+ display: flex;+ height: 40px;+ align-items: center;+ gap: 8px;+ padding: 0 18px;+ border-radius: 12px;+ border: 1px solid transparent;+ box-shadow: 0 1.5px 0 0 rgba(255, 255, 255, 0.2) inset;+ color: #fff;+ font-size: 13.5px;+ font-weight: 600;+ line-height: 1;+ white-space: nowrap;+ cursor: pointer;+ transition:+ background 160ms ease,+ border-color 160ms ease;+}++.cap-extension-control-bar-pill svg {+ display: block;+ flex: 0 0 auto;+}++.cap-extension-control-bar-pill:focus-visible {+ outline: 2px solid #5eb1ef;+ outline-offset: 1px;+}++.cap-extension-control-bar-pill:disabled {+ opacity: 0.6;+ cursor: not-allowed;+}++.cap-extension-control-bar-pill.is-stop {+ gap: 7px;+ padding: 0 16px;+ background: #e5484d;+ border-color: #d93036;+}++.cap-extension-control-bar-pill.is-stop:hover:not(:disabled),+.cap-extension-control-bar-pill.is-stop:focus-visible {+ background: #d93036;+}++.cap-extension-control-bar-pill.is-start {+ background: #2563eb;+ border-color: #1d4ed8;+}++.cap-extension-control-bar-pill.is-start:hover:not(:disabled),+.cap-extension-control-bar-pill.is-start:focus-visible {+ background: #1d4ed8;+}++.cap-extension-recording-rail {+ position: fixed;+ left: 16px;+ top: 50%;+ z-index: 2147483647;+ display: flex;+ flex-direction: column;+ align-items: center;+ gap: 8px;+ padding: 10px 8px 8px;+ border: 1px solid rgba(18, 18, 23, 0.08);+ border-radius: 18px;+ background: rgba(252, 252, 252, 0.97);+ box-shadow:+ 0 24px 64px rgba(0, 0, 0, 0.28),+ 0 4px 16px rgba(0, 0, 0, 0.12);+ color: #202020;+ pointer-events: auto;+ user-select: none;+ backdrop-filter: blur(20px);+ font-family:+ Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,+ "Segoe UI", sans-serif;+ font-size: 13px;+ letter-spacing: 0;+ line-height: 1;+ transform: translateY(-50%);+ animation: cap-extension-rail-in 260ms cubic-bezier(0.16, 1, 0.3, 1);+}++.cap-extension-recording-rail-timer {+ display: flex;+ flex-direction: column;+ align-items: center;+ gap: 7px;+ padding: 4px 6px 2px;+}++.cap-extension-recording-rail-time {+ min-width: 40px;+ font-size: 15px;+ font-weight: 600;+ line-height: 1;+ color: #18181b;+ text-align: center;+ font-variant-numeric: tabular-nums;+ white-space: nowrap;+}++.cap-extension-recording-rail-timer.is-warning+ .cap-extension-recording-rail-time {+ color: #e5484d;+}++@keyframes cap-extension-rail-in {+ from {+ opacity: 0;+ transform: translateY(-50%) translateX(-14px) scale(0.97);+ }+ to {+ opacity: 1;+ transform: translateY(-50%) translateX(0) scale(1);+ }+}++@keyframes cap-extension-pulse {+ 0%,+ 100% {+ opacity: 1;+ }+ 50% {+ opacity: 0.35;+ }+}++@keyframes cap-extension-bar-in {+ from {+ opacity: 0;+ transform: translateY(14px) scale(0.97);+ }+ to {+ opacity: 1;+ transform: translateY(0) scale(1);+ }+}diff --git a/apps/chrome-extension/src/content/overlay.tsx b/apps/chrome-extension/src/content/overlay.tsxnew file mode 100644index 000000000..4f4d37af2--- /dev/null+++ b/apps/chrome-extension/src/content/overlay.tsx@@ -0,0 +1,1758 @@+import {+ Circle,+ FlipHorizontal,+ Maximize2,+ PictureInPicture,+ RectangleHorizontal,+ Square,+ X,+} from "lucide-react";+import {+ type PointerEvent as ReactPointerEvent,+ useCallback,+ useEffect,+ useRef,+ useState,+} from "react";+import { createRoot } from "react-dom/client";+import { isOverlayMessage } from "../shared/messages";+import { sendServiceWorkerMessage } from "../shared/runtime";+import {+ loadLastWebcamPreviewFrame,+ loadOverlayUiState,+ loadSettings,+ loadSharedRecordingState,+ loadSharedUiState,+ loadWebcamPreviewDismissed,+ OVERLAY_UI_STATE_KEY,+ SETTINGS_KEY,+ SHARED_UI_STATE_KEY,+ saveLastWebcamPreviewFrame,+ saveSettings,+ updateOverlayUiState,+ updateSharedUiState,+ WEBCAM_PREVIEW_DISMISSED_KEY,+} from "../shared/storage";+import type {+ CameraPreviewEventRelay,+ ExtensionSettings,+ OverlayPosition,+ RecordingStatus,+ WebcamPosition,+ WebcamPreviewFrame,+ WebcamSettings,+ WebcamShape,+} from "../shared/types";+import {+ toSessionDescriptionInit,+ waitForIceGatheringComplete,+} from "../shared/webrtc";+import overlayCss from "./overlay.css?inline";+import { RecordingBarOverlay } from "./recording-bar";++const ROOT_ID = "cap-extension-recorder-overlay";+const WINDOW_PADDING = 20;+const BAR_HEIGHT = 52;+// The iframe tokens are readable from the host page DOM (they sit in the+// iframe src), so token checks alone cannot authenticate window messages.+// Frames we embed always run on the extension origin; require it too.+const EXTENSION_ORIGIN = new URL(chrome.runtime.getURL("")).origin;+const PREVIEW_TOKEN =+ crypto.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(36)}`;+const PREVIEW_URL = chrome.runtime.getURL("camera-preview.html");+const PREVIEW_SRC = `${PREVIEW_URL}#${encodeURIComponent(PREVIEW_TOKEN)}`;+const PREVIEW_ERROR_DELAY_MS = 1200;+const PANEL_TOKEN =+ crypto.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(36)}`;+const PANEL_URL = chrome.runtime.getURL("popup.html");+const PANEL_SRC = `${PANEL_URL}#${encodeURIComponent(PANEL_TOKEN)}`;+const PANEL_WIDTH = 300;+const PANEL_DEFAULT_HEIGHT = 460;+const PANEL_MARGIN = 16;+// Persisting every 700ms preview frame to session storage broadcasts a+// 10-30KB onChanged event to every open tab; the cached frame is only a+// placeholder, so a coarse cadence is plenty.+const FRAME_PERSIST_INTERVAL_MS = 5000;++// The extension iframes (camera preview, recorder panel) are web accessible,+// so the service worker only honours requests from frames whose URL-hash+// token was registered by this content script. Registration must complete+// before an iframe is rendered, otherwise its first camera connect races the+// registry write.+let overlayTokensRegistration: Promise<boolean> | null = null;++const ensureOverlayTokensRegistered = () => {+ overlayTokensRegistration ??= Promise.all(+ [PREVIEW_TOKEN, PANEL_TOKEN].map((token) =>+ sendServiceWorkerMessage({+ target: "service-worker",+ type: "register-overlay-token",+ token,+ }),+ ),+ )+ .then((responses) => {+ const registered = responses.every((response) => response.ok);+ if (!registered) {+ overlayTokensRegistration = null;+ }+ return registered;+ })+ .catch(() => {+ overlayTokensRegistration = null;+ return false;+ });+ return overlayTokensRegistration;+};+type VideoDimensions = {+ width: number;+ height: number;+};++// Preview events (frames, drag, errors) arrive from the camera-preview+// iframe via the service worker relay (chrome.tabs.sendMessage), never via+// window.postMessage — the host page shares this window and could read+// webcam frames out of a postMessage stream.+type PreviewEventRelay = CameraPreviewEventRelay;++type PreviewParentMessage =+ | {+ source: "cap-extension-overlay";+ token: string;+ type: "settings";+ settings: WebcamSettings;+ }+ | {+ source: "cap-extension-overlay";+ token: string;+ type: "toggle-pip";+ }+ | {+ source: "cap-extension-overlay";+ token: string;+ type: "enter-pip";+ }+ | {+ source: "cap-extension-overlay";+ token: string;+ type: "exit-auto-pip";+ }+ | {+ source: "cap-extension-overlay";+ token: string;+ type: "stop";+ };++const classNames = (...values: Array<string | false | null | undefined>) =>+ values.filter(Boolean).join(" ");++// Messages the bootstrap content script acknowledged while this module was+// still being fetched. Each message-handling component replays them through+// its own handler exactly once when it registers its runtime listener, so+// the panel toggle or webcam settings push that triggered the lazy load is+// not dropped.+let startupMessages: readonly unknown[] = [];++const replayStartupMessages = (+ handleMessage: (+ message: unknown,+ sender: chrome.runtime.MessageSender,+ sendResponse: (response?: unknown) => void,+ ) => boolean | undefined,+) => {+ for (const message of startupMessages) {+ handleMessage(message, {} as chrome.runtime.MessageSender, () => undefined);+ }+};++const isPreviewEventRelay = (value: unknown): value is PreviewEventRelay => {+ if (!value || typeof value !== "object") return false;+ const candidate = value as Partial<PreviewEventRelay>;+ return (+ candidate.source === "cap-extension-camera-preview" &&+ candidate.token === PREVIEW_TOKEN &&+ !!candidate.event &&+ typeof candidate.event === "object"+ );+};++const getPreviewMetrics = (+ base: number,+ shape: WebcamShape,+ dimensions: VideoDimensions | null,+) => {+ if (!dimensions || dimensions.height === 0) {+ return {+ width: base,+ height: base,+ aspectRatio: 1,+ };+ }++ const aspectRatio = dimensions.width / dimensions.height;++ if (shape !== "full") {+ return {+ width: base,+ height: base,+ aspectRatio,+ };+ }++ if (aspectRatio >= 1) {+ return {+ width: base * aspectRatio,+ height: base,+ aspectRatio,+ };+ }++ return {+ width: base,+ height: base / aspectRatio,+ aspectRatio,+ };+};++const getBorderRadius = (size: number, shape: WebcamShape) => {+ if (shape === "round") return "9999px";+ return size <= 230 ? "3rem" : "4rem";+};++const toOverlayPosition = (position: {+ x: number;+ y: number;+}): OverlayPosition => ({+ ...position,+ viewportWidth: window.innerWidth,+ viewportHeight: window.innerHeight,+ updatedAt: Date.now(),+});++const isRecordingPreviewStatus = (status: RecordingStatus | undefined) =>+ status?.phase === "creating" ||+ status?.phase === "recording" ||+ status?.phase === "paused";++const isSameWebcamSettings = (+ current: WebcamSettings | null,+ next: WebcamSettings,+) =>+ current?.enabled === next.enabled &&+ current.deviceId === next.deviceId &&+ current.position === next.position &&+ current.size === next.size &&+ current.shape === next.shape &&+ current.mirror === next.mirror;++const waitForRemoteStream = (peer: RTCPeerConnection) =>+ new Promise<MediaStream>((resolve, reject) => {+ let settled = false;+ const timeout = window.setTimeout(() => {+ if (settled) return;+ settled = true;+ reject(new Error("Camera preview timed out."));+ }, 10000);++ const finish = (stream: MediaStream) => {+ if (settled) return;+ settled = true;+ window.clearTimeout(timeout);+ resolve(stream);+ };++ peer.addEventListener("track", (event) => {+ finish(event.streams[0] ?? new MediaStream([event.track]));+ });++ peer.addEventListener("connectionstatechange", () => {+ if (+ settled ||+ (peer.connectionState !== "failed" && peer.connectionState !== "closed")+ ) {+ return;+ }+ settled = true;+ window.clearTimeout(timeout);+ reject(new Error("Camera preview connection failed."));+ });+ });++const stopStream = (stream: MediaStream | null) => {+ if (!stream) return;+ for (const track of stream.getTracks()) {+ track.stop();+ }+};++const connectCameraPreview = async (+ settings: WebcamSettings,+ sessionId: string,+) => {+ const peer = new RTCPeerConnection();+ peer.addTransceiver("video", { direction: "recvonly" });+ const remoteStreamPromise = waitForRemoteStream(peer);+ await peer.setLocalDescription(await peer.createOffer());+ await waitForIceGatheringComplete(peer);++ const response = await sendServiceWorkerMessage({+ target: "service-worker",+ type: "connect-camera-preview",+ sessionId,+ settings,+ offer: toSessionDescriptionInit(peer.localDescription),+ });++ if (!response.ok) {+ peer.close();+ throw new Error(response.error);+ }+ if (!response.answer) {+ peer.close();+ throw new Error("Camera preview did not return an answer.");+ }++ await peer.setRemoteDescription(response.answer);+ return {+ peer,+ stream: await remoteStreamPromise,+ };+};++type PanelFrameMessage =+ | {+ source: "cap-extension-panel";+ token: string;+ type: "size";+ height: number;+ }+ | {+ source: "cap-extension-panel";+ token: string;+ type: "dismiss";+ };++const isPanelFrameMessage = (value: unknown): value is PanelFrameMessage => {+ if (!value || typeof value !== "object") return false;+ const candidate = value as Partial<PanelFrameMessage>;+ return (+ candidate.source === "cap-extension-panel" &&+ candidate.token === PANEL_TOKEN+ );+};++function RecorderPanelOverlay({+ onOpenChange,+}: {+ onOpenChange: (open: boolean) => void;+}) {+ const [open, setOpen] = useState(false);+ const [pageVisible, setPageVisible] = useState(+ () => document.visibilityState === "visible",+ );+ const [contentHeight, setContentHeight] = useState(PANEL_DEFAULT_HEIGHT);+ const [viewportHeight, setViewportHeight] = useState(+ () => window.innerHeight,+ );+ const [tokenReady, setTokenReady] = useState(false);+ const startupReplayedRef = useRef(false);++ useEffect(() => {+ onOpenChange(open);+ }, [onOpenChange, open]);++ useEffect(() => {+ if (!open || tokenReady) return;+ let disposed = false;+ void ensureOverlayTokensRegistered().then((registered) => {+ if (!disposed && registered) setTokenReady(true);+ });+ return () => {+ disposed = true;+ };+ }, [open, tokenReady]);++ // The open flag lives in chrome.storage.session so the panel follows the+ // user across tabs: opening or closing it anywhere applies everywhere.+ useEffect(() => {+ let disposed = false;++ const syncPanelState = () => {+ loadSharedUiState()+ .then((state) => {+ if (!disposed) setOpen(state.panelOpen);+ })+ .catch(() => undefined);+ };++ const handleStorageChange = (+ changes: Record<string, chrome.storage.StorageChange>,+ areaName: string,+ ) => {+ if (areaName === "session" && changes[SHARED_UI_STATE_KEY]) {+ syncPanelState();+ }+ };++ syncPanelState();+ chrome.storage.onChanged.addListener(handleStorageChange);+ return () => {+ disposed = true;+ chrome.storage.onChanged.removeListener(handleStorageChange);+ };+ }, []);++ useEffect(() => {+ const handleVisibility = () =>+ setPageVisible(document.visibilityState === "visible");+ document.addEventListener("visibilitychange", handleVisibility);+ return () =>+ document.removeEventListener("visibilitychange", handleVisibility);+ }, []);++ const closePanel = useCallback(() => {+ setOpen(false);+ void updateSharedUiState((current) => ({+ ...current,+ panelOpen: false,+ updatedAt: Date.now(),+ })).catch(() => undefined);+ }, []);++ useEffect(() => {+ const handleMessage = (+ message: unknown,+ _sender: chrome.runtime.MessageSender,+ sendResponse: (response?: unknown) => void,+ ) => {+ if (!isOverlayMessage(message)) return false;+ if (message.type === "overlay-panel-toggle") {+ sendResponse({ ok: true });+ // Flip the shared flag; every tab (this one included) follows the+ // storage change. Reopening also resurfaces a dismissed ready bar.+ void updateSharedUiState((current) => ({+ ...current,+ panelOpen: !current.panelOpen,+ readyBarDismissed: current.panelOpen+ ? current.readyBarDismissed+ : false,+ updatedAt: Date.now(),+ }))+ .then((state) => setOpen(state.panelOpen))+ .catch(() => undefined);+ return false;+ }+ if (message.type === "overlay-panel-hide") {+ sendResponse({ ok: true });+ closePanel();+ return false;+ }+ return false;+ };++ chrome.runtime.onMessage.addListener(handleMessage);+ if (!startupReplayedRef.current) {+ startupReplayedRef.current = true;+ replayStartupMessages(handleMessage);+ }+ return () => chrome.runtime.onMessage.removeListener(handleMessage);+ }, [closePanel]);++ useEffect(() => {+ const handleFrameMessage = (event: MessageEvent<unknown>) => {+ if (event.origin !== EXTENSION_ORIGIN) return;+ if (!isPanelFrameMessage(event.data)) return;+ if (event.data.type === "size") {+ setContentHeight(Math.max(320, Math.ceil(event.data.height)));+ return;+ }+ closePanel();+ };++ window.addEventListener("message", handleFrameMessage);+ return () => window.removeEventListener("message", handleFrameMessage);+ }, [closePanel]);++ useEffect(() => {+ if (!open) return;+ const handleResize = () => setViewportHeight(window.innerHeight);+ handleResize();+ window.addEventListener("resize", handleResize);+ return () => window.removeEventListener("resize", handleResize);+ }, [open]);++ // Only the tab on screen renders the iframe; hidden tabs keep just the+ // shared flag so the panel reappears instantly when they come forward.+ // The iframe also waits for its token registration so the panel page can+ // verify it was embedded by this extension.+ if (!open || !pageVisible || !tokenReady) return null;++ const height = Math.min(contentHeight, viewportHeight - PANEL_MARGIN * 2);++ return (+ <>+ <button+ type="button"+ className="cap-extension-panel-backdrop"+ aria-label="Dismiss Cap recorder"+ onClick={closePanel}+ />+ <div+ className="cap-extension-panel"+ role="dialog"+ aria-label="Cap recorder"+ style={{ width: `${PANEL_WIDTH}px`, height: `${height}px` }}+ >+ <iframe+ src={PANEL_SRC}+ title="Cap recorder"+ allow="camera; microphone; autoplay"+ className="cap-extension-panel-iframe"+ />+ </div>+ </>+ );+}++function OverlayApp() {+ const [extensionSettings, setExtensionSettings] =+ useState<ExtensionSettings | null>(null);+ const [position, setPosition] = useState<{ x: number; y: number } | null>(+ null,+ );+ const [persistedWebcamPosition, setPersistedWebcamPosition] =+ useState<OverlayPosition | null>(null);+ const [isDragging, setIsDragging] = useState(false);+ const [videoDimensions, setVideoDimensions] =+ useState<VideoDimensions | null>(null);+ const [lastPreviewFrame, setLastPreviewFrame] =+ useState<WebcamPreviewFrame | null>(null);+ const [livePreviewReady, setLivePreviewReady] = useState(false);+ const [previewError, setPreviewError] = useState<string | null>(null);+ const [showPreviewError, setShowPreviewError] = useState(false);+ const [iframeReady, setIframeReady] = useState(false);+ const [pipSupported, setPipSupported] = useState(false);+ const [parentPipActive, setParentPipActive] = useState(false);+ const [framePipActive, setFramePipActive] = useState(false);+ const [previewOpen, setPreviewOpen] = useState(false);+ const [recordingPreviewActive, setRecordingPreviewActive] = useState(false);+ const [recorderPanelOpen, setRecorderPanelOpen] = useState(false);+ const [previewTokenReady, setPreviewTokenReady] = useState(false);+ const iframeRef = useRef<HTMLIFrameElement>(null);+ const windowRef = useRef<HTMLDivElement>(null);+ const pipVideoRef = useRef<HTMLVideoElement>(null);+ const pipPeerRef = useRef<RTCPeerConnection | null>(null);+ const pipStreamRef = useRef<MediaStream | null>(null);+ const pipSettingsRef = useRef<WebcamSettings | null>(null);+ const previewSessionIdRef = useRef<string | null>(null);+ const webcamRef = useRef<WebcamSettings | null>(null);+ const settingsRef = useRef<ExtensionSettings | null>(null);+ const positionRef = useRef<{ x: number; y: number } | null>(null);+ const videoDimensionsRef = useRef<VideoDimensions | null>(null);+ const dragStartRef = useRef({ x: 0, y: 0 });+ const dragFrameRef = useRef<number | null>(null);+ const isDraggingRef = useRef(false);+ const framePipActiveRef = useRef(false);+ const recordingPreviewActiveRef = useRef(false);+ const previewOpenRef = useRef(false);+ const livePreviewReadyRef = useRef(false);+ const previewDismissedRef = useRef(false);+ const lastFrameRef = useRef<WebcamPreviewFrame | null>(null);+ const lastFrameSavedAtRef = useRef(0);+ const startupReplayedRef = useRef(false);+ const webcam = extensionSettings?.webcam ?? null;+ const previewEnabled = Boolean(+ webcam?.enabled && webcam.deviceId && previewOpen,+ );+ const isInPictureInPicture = parentPipActive || framePipActive;+ const parentPipSupported =+ typeof document !== "undefined" && document.pictureInPictureEnabled;++ // Control messages travel over chrome.runtime instead of window.postMessage:+ // the host page can read the iframe token from the DOM and post forged+ // window messages, but it cannot speak chrome.runtime at all. The preview+ // filters on the token, so only this tab's iframe reacts.+ const postPreviewMessage = useCallback((message: PreviewParentMessage) => {+ chrome.runtime.sendMessage(message, () => {+ void chrome.runtime.lastError;+ });+ }, []);++ const persistPreviewFrame = useCallback(+ (frame: WebcamPreviewFrame | null, force: boolean) => {+ if (!frame) return;+ const now = Date.now();+ if (+ !force &&+ now - lastFrameSavedAtRef.current < FRAME_PERSIST_INTERVAL_MS+ ) {+ return;+ }+ lastFrameSavedAtRef.current = now;+ void saveLastWebcamPreviewFrame(frame).catch(() => undefined);+ },+ [],+ );++ const disconnectParentPipPreview = useCallback(() => {+ pipPeerRef.current?.close();+ stopStream(pipStreamRef.current);+ pipPeerRef.current = null;+ pipStreamRef.current = null;+ pipSettingsRef.current = null;+ const video = pipVideoRef.current;+ if (video) {+ video.srcObject = null;+ video.removeAttribute("src");+ }+ void sendServiceWorkerMessage({+ target: "service-worker",+ type: "disconnect-camera-preview",+ sessionId: `${PREVIEW_TOKEN}:pip`,+ }).catch(() => undefined);+ }, []);++ const disconnectPreviewSession = useCallback(() => {+ const sessionId = previewSessionIdRef.current;+ previewSessionIdRef.current = null;+ if (!sessionId) return;+ void sendServiceWorkerMessage({+ target: "service-worker",+ type: "disconnect-camera-preview",+ sessionId,+ }).catch(() => undefined);+ }, []);++ const stopLocalPreview = useCallback(() => {+ setPreviewOpen(false);+ setLivePreviewReady(false);+ // Surface the freshest captured frame as the placeholder for the next+ // time the preview opens (state updates are skipped while live).+ if (lastFrameRef.current) {+ setLastPreviewFrame(lastFrameRef.current);+ persistPreviewFrame(lastFrameRef.current, true);+ }+ disconnectParentPipPreview();+ disconnectPreviewSession();+ postPreviewMessage({+ source: "cap-extension-overlay",+ token: PREVIEW_TOKEN,+ type: "stop",+ });+ }, [+ disconnectParentPipPreview,+ disconnectPreviewSession,+ persistPreviewFrame,+ postPreviewMessage,+ ]);++ const applyWebcamSettings = useCallback(+ (getNext: (current: WebcamSettings) => WebcamSettings) => {+ setExtensionSettings((current) => {+ if (!current) return current;+ const next = {+ ...current,+ webcam: getNext(current.webcam),+ };+ void saveSettings(next)+ .then(() =>+ sendServiceWorkerMessage({+ target: "service-worker",+ type: "settings-updated",+ settings: next,+ }),+ )+ .catch(() => undefined);+ return next;+ });+ },+ [],+ );++ useEffect(() => {+ webcamRef.current = webcam;+ }, [webcam]);++ useEffect(() => {+ settingsRef.current = extensionSettings;+ }, [extensionSettings]);++ useEffect(() => {+ positionRef.current = position;+ }, [position]);++ useEffect(() => {+ videoDimensionsRef.current = videoDimensions;+ }, [videoDimensions]);++ useEffect(() => {+ framePipActiveRef.current = framePipActive;+ }, [framePipActive]);++ useEffect(() => {+ recordingPreviewActiveRef.current = recordingPreviewActive;+ }, [recordingPreviewActive]);++ useEffect(() => {+ previewOpenRef.current = previewOpen;+ }, [previewOpen]);++ useEffect(() => {+ livePreviewReadyRef.current = livePreviewReady;+ }, [livePreviewReady]);++ useEffect(() => {+ let disposed = false;+ loadLastWebcamPreviewFrame()+ .then((frame) => {+ if (!disposed) setLastPreviewFrame(frame);+ })+ .catch(() => undefined);++ return () => {+ disposed = true;+ };+ }, []);++ // The preview iframe and the PiP fallback may only start once the service+ // worker knows this page's tokens; an unregistered frame is refused camera+ // access.+ useEffect(() => {+ if (!previewEnabled || previewTokenReady) return;+ let disposed = false;+ void ensureOverlayTokensRegistered().then((registered) => {+ if (!disposed && registered) setPreviewTokenReady(true);+ });+ return () => {+ disposed = true;+ };+ }, [previewEnabled, previewTokenReady]);++ useEffect(() => {+ let disposed = false;++ const syncOverlayUiState = () => {+ loadOverlayUiState()+ .then((state) => {+ if (!disposed) setPersistedWebcamPosition(state.webcamPosition);+ })+ .catch(() => undefined);+ };++ const syncSettingsState = () => {+ Promise.all([loadSettings(), loadWebcamPreviewDismissed()])+ .then(([nextSettings, dismissed]) => {+ if (disposed) return;+ previewDismissedRef.current = dismissed;+ setExtensionSettings(nextSettings);+ if (+ dismissed ||+ !nextSettings.webcam.enabled ||+ !nextSettings.webcam.deviceId+ ) {+ setRecordingPreviewActive(false);+ setLivePreviewReady(false);+ stopLocalPreview();+ }+ })+ .catch(() => undefined);+ };++ const syncPreviewDismissedState = () => {+ loadWebcamPreviewDismissed()+ .then((dismissed) => {+ if (disposed) return;+ previewDismissedRef.current = dismissed;+ if (dismissed) {+ setRecordingPreviewActive(false);+ setLivePreviewReady(false);+ stopLocalPreview();+ }+ })+ .catch(() => undefined);+ };++ const handleStorageChange = (+ changes: Record<string, chrome.storage.StorageChange>,+ areaName: string,+ ) => {+ if (areaName === "local" && changes[OVERLAY_UI_STATE_KEY]) {+ syncOverlayUiState();+ }+ if (areaName === "local" && changes[SETTINGS_KEY]) {+ syncSettingsState();+ }+ if (areaName === "session" && changes[WEBCAM_PREVIEW_DISMISSED_KEY]) {+ syncPreviewDismissedState();+ }+ };++ syncOverlayUiState();+ syncPreviewDismissedState();+ chrome.storage.onChanged.addListener(handleStorageChange);+ return () => {+ disposed = true;+ chrome.storage.onChanged.removeListener(handleStorageChange);+ };+ }, [stopLocalPreview]);++ useEffect(() => {+ let disposed = false;+ Promise.all([+ sendServiceWorkerMessage({+ target: "service-worker",+ type: "get-overlay-settings",+ }),+ loadWebcamPreviewDismissed(),+ ])+ .then(([response, dismissed]) => {+ if (!disposed && response.ok && response.settings) {+ previewDismissedRef.current = dismissed;+ setExtensionSettings(response.settings);+ const previewActive = isRecordingPreviewStatus(response.status);+ setRecordingPreviewActive(previewActive);+ // A replayed startup message (the lazy-load trigger) may have+ // already opened the preview before this sync resolves, so only+ // ever turn it on here.+ if (!dismissed && previewActive) setPreviewOpen(true);+ }+ })+ .catch(() => undefined);+ return () => {+ disposed = true;+ };+ }, []);++ useEffect(() => {+ let disposed = false;++ const syncPreviewForVisibility = () => {+ if (document.visibilityState !== "visible") {+ return;+ }++ // Tab switches are frequent, so read settings and the session-storage+ // status mirror instead of a get-overlay-settings round trip that+ // would wake the MV3 service worker on every visibility flip. The+ // recording bar's slow poll keeps the mirror reconciled while active.+ Promise.all([+ loadSettings(),+ loadSharedRecordingState(),+ loadWebcamPreviewDismissed(),+ ])+ .then(([nextSettings, sharedState, dismissed]) => {+ if (disposed) return;+ previewDismissedRef.current = dismissed;+ const previewActive = isRecordingPreviewStatus(sharedState?.status);+ setExtensionSettings(nextSettings);+ if (dismissed) {+ setRecordingPreviewActive(false);+ stopLocalPreview();+ return;+ }+ if (previewActive) {+ setRecordingPreviewActive(true);+ setPreviewOpen(true);+ return;+ }+ if (recordingPreviewActiveRef.current) {+ setRecordingPreviewActive(false);+ stopLocalPreview();+ }+ })+ .catch(() => undefined);+ };++ document.addEventListener("visibilitychange", syncPreviewForVisibility);+ window.addEventListener("focus", syncPreviewForVisibility);+ return () => {+ disposed = true;+ document.removeEventListener(+ "visibilitychange",+ syncPreviewForVisibility,+ );+ window.removeEventListener("focus", syncPreviewForVisibility);+ };+ }, [stopLocalPreview]);++ useEffect(() => {+ const handleMessage = (+ message: unknown,+ _sender: chrome.runtime.MessageSender,+ sendResponse: (response?: unknown) => void,+ ) => {+ if (!isOverlayMessage(message)) return false;++ sendResponse({ ok: true });++ if (message.type === "overlay-hide") {+ setRecordingPreviewActive(false);+ stopLocalPreview();+ return false;+ }++ if (message.type === "overlay-enter-auto-pip") {+ // Only one Picture in Picture surface may drive at a time; racing+ // the parent fallback video against the preview iframe flips PiP+ // on and off and leaves the badge state inconsistent.+ const current = webcamRef.current;+ const frameLive =+ previewOpenRef.current &&+ livePreviewReadyRef.current &&+ Boolean(current?.enabled && current.deviceId);+ if (frameLive) {+ postPreviewMessage({+ source: "cap-extension-overlay",+ token: PREVIEW_TOKEN,+ type: "enter-pip",+ });+ return false;+ }+ const video = pipVideoRef.current;+ if (video && parentPipSupported) {+ void video+ .play()+ .then(() => video.requestPictureInPicture())+ .catch(() => undefined);+ }+ return false;+ }++ if (message.type === "overlay-exit-auto-pip") {+ const video = pipVideoRef.current;+ if (video && document.pictureInPictureElement === video) {+ void document.exitPictureInPicture().catch(() => undefined);+ }+ postPreviewMessage({+ source: "cap-extension-overlay",+ token: PREVIEW_TOKEN,+ type: "exit-auto-pip",+ });+ return false;+ }++ if (message.type !== "overlay-settings") return false;++ if (previewDismissedRef.current) return false;++ const webcamSettings = message.settings;+ const sameLivePreview =+ previewOpenRef.current &&+ livePreviewReadyRef.current &&+ isSameWebcamSettings(webcamRef.current, webcamSettings);+ if (!sameLivePreview) {+ setLivePreviewReady(false);+ } else {+ // "webcam-preview-ready" is normally only sent when the preview+ // first goes live. A restarted service worker loses that flag, so+ // re-announce readiness whenever it pushes settings while the+ // preview is already streaming; recording start waits on it.+ void sendServiceWorkerMessage({+ target: "service-worker",+ type: "webcam-preview-ready",+ }).catch(() => undefined);+ }+ setPreviewError(null);+ setShowPreviewError(false);+ setPreviewOpen(true);+ setRecordingPreviewActive(message.recording);+ if (settingsRef.current) {+ setExtensionSettings({+ ...settingsRef.current,+ webcam: webcamSettings,+ });+ } else {+ sendServiceWorkerMessage({+ target: "service-worker",+ type: "get-overlay-settings",+ })+ .then((response) => {+ if (response.ok && response.settings) {+ setExtensionSettings({+ ...response.settings,+ webcam: webcamSettings,+ });+ }+ })+ .catch(() => undefined);+ }+ return false;+ };++ chrome.runtime.onMessage.addListener(handleMessage);+ if (!startupReplayedRef.current) {+ startupReplayedRef.current = true;+ replayStartupMessages(handleMessage);+ }+ return () => chrome.runtime.onMessage.removeListener(handleMessage);+ }, [parentPipSupported, postPreviewMessage, stopLocalPreview]);++ const beginDrag = useCallback((clientX: number, clientY: number) => {+ isDraggingRef.current = true;+ setIsDragging(true);+ const currentPosition = positionRef.current;+ dragStartRef.current = {+ x: clientX - (currentPosition?.x ?? 0),+ y: clientY - (currentPosition?.y ?? 0),+ };+ }, []);++ const applyDragTransform = useCallback(() => {+ dragFrameRef.current = null;+ const element = windowRef.current;+ const next = positionRef.current;+ if (!element || !next) return;+ element.style.transform = `translate3d(${next.x}px, ${next.y}px, 0)`;+ }, []);++ // Dragging writes the transform straight to the DOM inside one rAF per+ // frame; going through React state for every pointermove made the preview+ // visibly lag behind the cursor.+ const moveDrag = useCallback(+ (clientX: number, clientY: number) => {+ const webcamSettings = webcamRef.current;+ if (!isDraggingRef.current || !webcamSettings) return;++ const metrics = getPreviewMetrics(+ webcamSettings.size,+ webcamSettings.shape,+ videoDimensionsRef.current,+ );+ const totalHeight = metrics.height + BAR_HEIGHT;+ const maxX = Math.max(0, window.innerWidth - metrics.width);+ const maxY = Math.max(0, window.innerHeight - totalHeight);+ const nextX = clientX - dragStartRef.current.x;+ const nextY = clientY - dragStartRef.current.y;++ positionRef.current = {+ x: Math.max(0, Math.min(nextX, maxX)),+ y: Math.max(0, Math.min(nextY, maxY)),+ };+ dragFrameRef.current ??= window.requestAnimationFrame(applyDragTransform);+ },+ [applyDragTransform],+ );++ const endDrag = useCallback(() => {+ if (!isDraggingRef.current) return;+ isDraggingRef.current = false;+ setIsDragging(false);+ if (dragFrameRef.current !== null) {+ window.cancelAnimationFrame(dragFrameRef.current);+ dragFrameRef.current = null;+ applyDragTransform();+ }+ const nextPosition = positionRef.current;+ if (!nextPosition) return;+ setPosition(nextPosition);+ void updateOverlayUiState((current) => ({+ ...current,+ webcamPosition: toOverlayPosition(nextPosition),+ }))+ .then((state) => setPersistedWebcamPosition(state.webcamPosition))+ .catch(() => undefined);+ }, [applyDragTransform]);++ const toPagePoint = useCallback((clientX: number, clientY: number) => {+ const rect = iframeRef.current?.getBoundingClientRect();+ if (rect) {+ return {+ x: rect.left + clientX,+ y: rect.top + clientY,+ };+ }+ const currentPosition = positionRef.current ?? { x: 0, y: 0 };+ return {+ x: clientX + currentPosition.x,+ y: clientY + currentPosition.y + BAR_HEIGHT,+ };+ }, []);++ useEffect(() => {+ const handlePreviewEvent = (message: unknown) => {+ if (!isPreviewEventRelay(message)) return false;+ const event = message.event;++ if (event.type === "ready") {+ setIframeReady(true);+ const current = webcamRef.current;+ if (current?.enabled && current.deviceId) {+ postPreviewMessage({+ source: "cap-extension-overlay",+ token: PREVIEW_TOKEN,+ type: "settings",+ settings: current,+ });+ }+ return false;+ }++ if (event.type === "metadata") {+ setVideoDimensions(event.dimensions);+ setPreviewError(null);+ setShowPreviewError(false);+ return false;+ }++ if (event.type === "session") {+ previewSessionIdRef.current = event.sessionId;+ return false;+ }++ if (event.type === "frame") {+ const wasLive = livePreviewReadyRef.current;+ lastFrameRef.current = event.frame;+ // The cached frame only paints while the live iframe is hidden,+ // so skip the full overlay re-render on every 700ms frame once+ // the preview is live; the freshest frame is flushed from the+ // ref when the preview stops.+ if (!wasLive) {+ setLastPreviewFrame(event.frame);+ setLivePreviewReady(true);+ livePreviewReadyRef.current = true;+ setPreviewError(null);+ setShowPreviewError(false);+ void sendServiceWorkerMessage({+ target: "service-worker",+ type: "webcam-preview-ready",+ }).catch(() => undefined);+ }+ persistPreviewFrame(event.frame, !wasLive);+ return false;+ }++ if (event.type === "drag-start") {+ const point = toPagePoint(event.clientX, event.clientY);+ beginDrag(point.x, point.y);+ return false;+ }++ if (event.type === "drag-move") {+ const point = toPagePoint(event.clientX, event.clientY);+ moveDrag(point.x, point.y);+ return false;+ }++ if (event.type === "drag-end") {+ endDrag();+ return false;+ }++ if (event.type === "error") {+ setVideoDimensions(null);+ setLivePreviewReady(false);+ setPreviewError(event.message);+ setShowPreviewError(false);+ void sendServiceWorkerMessage({+ target: "service-worker",+ type: "webcam-preview-error",+ reason: event.reason,+ message: event.message,+ }).catch(() => undefined);+ return false;+ }++ setPipSupported(event.supported);+ setFramePipActive(event.active);+ return false;+ };++ chrome.runtime.onMessage.addListener(handlePreviewEvent);+ return () => chrome.runtime.onMessage.removeListener(handlePreviewEvent);+ }, [+ beginDrag,+ endDrag,+ moveDrag,+ persistPreviewFrame,+ postPreviewMessage,+ toPagePoint,+ ]);++ useEffect(() => {+ if (!previewEnabled) {+ setIframeReady(false);+ setVideoDimensions(null);+ setLivePreviewReady(false);+ setPreviewError(null);+ setShowPreviewError(false);+ setPipSupported(false);+ setFramePipActive(false);+ postPreviewMessage({+ source: "cap-extension-overlay",+ token: PREVIEW_TOKEN,+ type: "stop",+ });+ }+ }, [postPreviewMessage, previewEnabled]);++ useEffect(() => {+ if (!previewError || !previewEnabled || livePreviewReady) return;+ const timeout = window.setTimeout(+ () => setShowPreviewError(true),+ PREVIEW_ERROR_DELAY_MS,+ );+ return () => window.clearTimeout(timeout);+ }, [livePreviewReady, previewEnabled, previewError]);++ useEffect(() => {+ if (!iframeReady || !previewEnabled || !webcam) return;+ postPreviewMessage({+ source: "cap-extension-overlay",+ token: PREVIEW_TOKEN,+ type: "settings",+ settings: webcam,+ });+ }, [iframeReady, postPreviewMessage, previewEnabled, webcam]);++ useEffect(() => {+ if (!previewEnabled || !previewTokenReady || !webcam) {+ disconnectParentPipPreview();+ return;+ }++ let disposed = false;+ const sessionId = `${PREVIEW_TOKEN}:pip`;++ const connect = async () => {+ const peerActive =+ pipPeerRef.current &&+ pipPeerRef.current.connectionState !== "closed" &&+ pipPeerRef.current.connectionState !== "failed" &&+ pipPeerRef.current.connectionState !== "disconnected";+ if (+ pipStreamRef.current &&+ peerActive &&+ pipSettingsRef.current &&+ isSameWebcamSettings(pipSettingsRef.current, webcam)+ ) {+ if (+ pipVideoRef.current &&+ pipVideoRef.current.srcObject !== pipStreamRef.current+ ) {+ pipVideoRef.current.srcObject = pipStreamRef.current;+ }+ await pipVideoRef.current?.play().catch(() => undefined);+ return;+ }++ disconnectParentPipPreview();++ try {+ const { peer, stream } = await connectCameraPreview(webcam, sessionId);+ if (disposed) {+ peer.close();+ stopStream(stream);+ void sendServiceWorkerMessage({+ target: "service-worker",+ type: "disconnect-camera-preview",+ sessionId,+ }).catch(() => undefined);+ return;+ }++ pipPeerRef.current = peer;+ pipStreamRef.current = stream;+ pipSettingsRef.current = webcam;+ if (pipVideoRef.current) {+ pipVideoRef.current.srcObject = stream;+ await pipVideoRef.current.play().catch(() => undefined);+ }+ } catch {+ if (!disposed) {+ disconnectParentPipPreview();+ }+ }+ };++ void connect();++ return () => {+ disposed = true;+ };+ }, [disconnectParentPipPreview, previewEnabled, previewTokenReady, webcam]);++ useEffect(() => {+ const video = pipVideoRef.current;+ if (!video || !parentPipSupported) return;++ const handleEnter = () => {+ setParentPipActive(true);+ };+ const handleLeave = () => {+ setParentPipActive(false);+ };++ video.addEventListener("enterpictureinpicture", handleEnter);+ video.addEventListener("leavepictureinpicture", handleLeave);+ return () => {+ video.removeEventListener("enterpictureinpicture", handleEnter);+ video.removeEventListener("leavepictureinpicture", handleLeave);+ };+ }, [parentPipSupported]);++ const clampPosition = useCallback(+ (previous: { x: number; y: number } | null) => {+ if (!previewEnabled || !webcam) return previous;+ const metrics = getPreviewMetrics(+ webcam.size,+ webcam.shape,+ videoDimensions,+ );+ const totalHeight = metrics.height + BAR_HEIGHT;+ const maxX = Math.max(0, window.innerWidth - metrics.width);+ const maxY = Math.max(0, window.innerHeight - totalHeight);+ const defaultX = webcam.position.includes("left")+ ? WINDOW_PADDING+ : window.innerWidth - metrics.width - WINDOW_PADDING;+ const defaultY = webcam.position.includes("top")+ ? WINDOW_PADDING+ : window.innerHeight - totalHeight - WINDOW_PADDING;+ const nextX = previous?.x ?? defaultX;+ const nextY = previous?.y ?? defaultY;++ return {+ x: Math.max(0, Math.min(nextX, maxX)),+ y: Math.max(0, Math.min(nextY, maxY)),+ };+ },+ [previewEnabled, webcam, videoDimensions],+ );++ const positionPrefRef = useRef<WebcamPosition | null>(null);++ useEffect(() => {+ const positionPref = webcam?.position ?? null;+ const prefChanged =+ positionPrefRef.current !== null &&+ positionPrefRef.current !== positionPref;+ positionPrefRef.current = positionPref;+ if (prefChanged) {+ void updateOverlayUiState((current) => ({+ ...current,+ webcamPosition: null,+ }))+ .then((state) => setPersistedWebcamPosition(state.webcamPosition))+ .catch(() => undefined);+ }+ setPosition((previous) => clampPosition(prefChanged ? null : previous));+ }, [clampPosition, webcam?.position]);++ useEffect(() => {+ if (!persistedWebcamPosition || isDragging) return;+ setPosition(+ clampPosition({+ x: persistedWebcamPosition.x,+ y: persistedWebcamPosition.y,+ }),+ );+ }, [clampPosition, isDragging, persistedWebcamPosition]);++ useEffect(() => {+ const handleResize = () => {+ setPosition(clampPosition);+ };+ window.addEventListener("resize", handleResize);+ return () => window.removeEventListener("resize", handleResize);+ }, [clampPosition]);++ const handlePointerDown = useCallback(+ (event: ReactPointerEvent<HTMLDivElement>) => {+ if ((event.target as HTMLElement).closest("[data-controls]")) {+ return;+ }+ event.stopPropagation();+ event.preventDefault();+ // Capture the pointer so the drag keeps receiving events even while+ // the cursor passes over the preview iframe (a separate document).+ event.currentTarget.setPointerCapture(event.pointerId);+ beginDrag(event.clientX, event.clientY);+ },+ [beginDrag],+ );++ const handlePointerMove = useCallback(+ (event: PointerEvent) => {+ moveDrag(event.clientX, event.clientY);+ },+ [moveDrag],+ );++ useEffect(() => {+ if (!isDragging) return;++ window.addEventListener("pointermove", handlePointerMove);+ window.addEventListener("pointerup", endDrag);+ window.addEventListener("pointercancel", endDrag);+ return () => {+ window.removeEventListener("pointermove", handlePointerMove);+ window.removeEventListener("pointerup", endDrag);+ window.removeEventListener("pointercancel", endDrag);+ };+ }, [endDrag, handlePointerMove, isDragging]);++ const handleClose = useCallback(() => {+ previewDismissedRef.current = true;+ stopLocalPreview();+ setRecordingPreviewActive(false);+ void sendServiceWorkerMessage({+ target: "service-worker",+ type: "close-webcam-preview",+ }).catch(() => undefined);+ }, [stopLocalPreview]);++ const updateShape = useCallback(() => {+ applyWebcamSettings((current) => ({+ ...current,+ shape:+ current.shape === "round"+ ? "square"+ : current.shape === "square"+ ? "full"+ : "round",+ }));+ }, [applyWebcamSettings]);++ const updateSize = useCallback(() => {+ applyWebcamSettings((current) => ({+ ...current,+ size: current.size <= 230 ? 400 : 230,+ }));+ }, [applyWebcamSettings]);++ const updateMirror = useCallback(() => {+ applyWebcamSettings((current) => ({+ ...current,+ mirror: !current.mirror,+ }));+ }, [applyWebcamSettings]);++ const handleTogglePictureInPicture = useCallback(() => {+ const video = pipVideoRef.current;+ if (video && document.pictureInPictureElement === video) {+ void document.exitPictureInPicture().catch(() => undefined);+ return;+ }++ const current = webcamRef.current;+ const frameLive =+ previewOpenRef.current &&+ livePreviewReadyRef.current &&+ Boolean(current?.enabled && current.deviceId);+ if (framePipActiveRef.current || frameLive) {+ postPreviewMessage({+ source: "cap-extension-overlay",+ token: PREVIEW_TOKEN,+ type: "toggle-pip",+ });+ return;+ }++ if (!video || !parentPipSupported) return;+ void (async () => {+ try {+ await video.play().catch(() => undefined);+ await video.requestPictureInPicture();+ } catch {}+ })();+ }, [parentPipSupported, postPreviewMessage]);++ const handlePreviewLoad = useCallback(() => {+ const current = webcamRef.current;+ if (!current?.enabled || !current.deviceId) return;++ postPreviewMessage({+ source: "cap-extension-overlay",+ token: PREVIEW_TOKEN,+ type: "settings",+ settings: current,+ });+ }, [postPreviewMessage]);++ const metricsDimensions =+ videoDimensions ?? lastPreviewFrame?.dimensions ?? null;+ const metrics = webcam+ ? getPreviewMetrics(webcam.size, webcam.shape, metricsDimensions)+ : null;+ const totalHeight = metrics ? metrics.height + BAR_HEIGHT : 0;+ const borderRadius = webcam+ ? getBorderRadius(webcam.size, webcam.shape)+ : "0";+ const renderPosition =+ (isDragging ? positionRef.current : position) ?? position;++ const cameraWindow = previewEnabled &&+ previewTokenReady &&+ webcam &&+ renderPosition &&+ metrics && (+ <div+ ref={windowRef}+ className={classNames(+ "cap-extension-camera-window",+ isDragging && "is-dragging",+ )}+ data-camera-preview+ role="dialog"+ style={{+ transform: `translate3d(${renderPosition.x}px, ${renderPosition.y}px, 0)`,+ width: `${metrics.width}px`,+ height: `${totalHeight}px`,+ borderRadius,+ }}+ onPointerDown={handlePointerDown}+ >+ <div className="cap-extension-camera-shell" style={{ borderRadius }}>+ <div className="cap-extension-camera-bar">+ <div+ data-controls+ className="cap-extension-camera-controls"+ role="toolbar"+ aria-label="Camera preview controls"+ onPointerDown={(event) => event.stopPropagation()}+ onClick={(event) => event.stopPropagation()}+ onKeyDown={(event) => {+ if (event.key === "Escape") {+ event.stopPropagation();+ handleClose();+ }+ }}+ >+ <button+ type="button"+ className="cap-extension-camera-control"+ aria-label="Close camera preview"+ title="Close"+ onClick={handleClose}+ >+ <X size={22} aria-hidden />+ </button>+ <button+ type="button"+ className={classNames(+ "cap-extension-camera-control",+ webcam.size > 230 && "is-active",+ )}+ aria-label="Resize camera preview"+ title="Resize"+ onClick={updateSize}+ >+ <Maximize2 size={22} aria-hidden />+ </button>+ <button+ type="button"+ className={classNames(+ "cap-extension-camera-control",+ webcam.shape !== "round" && "is-active",+ )}+ aria-label="Change camera preview shape"+ title="Shape"+ onClick={updateShape}+ >+ {webcam.shape === "round" ? (+ <Circle size={22} aria-hidden />+ ) : null}+ {webcam.shape === "square" ? (+ <Square size={22} aria-hidden />+ ) : null}+ {webcam.shape === "full" ? (+ <RectangleHorizontal size={22} aria-hidden />+ ) : null}+ </button>+ <button+ type="button"+ className={classNames(+ "cap-extension-camera-control",+ webcam.mirror && "is-active",+ )}+ aria-label="Mirror camera preview"+ title="Mirror"+ onClick={updateMirror}+ >+ <FlipHorizontal size={22} aria-hidden />+ </button>+ <button+ type="button"+ className={classNames(+ "cap-extension-camera-control",+ isInPictureInPicture && "is-active",+ )}+ aria-label="Toggle Picture in Picture"+ title={+ parentPipSupported || pipSupported+ ? "Picture in Picture"+ : "Picture in Picture is blocked on this page"+ }+ disabled={!parentPipSupported && !pipSupported}+ onClick={handleTogglePictureInPicture}+ >+ <PictureInPicture size={22} aria-hidden />+ </button>+ </div>+ </div>++ <div+ className={classNames(+ "cap-extension-camera-frame",+ webcam.shape === "round" ? "is-round" : "is-rounded",+ livePreviewReady && "is-live",+ isInPictureInPicture && "is-pip",+ )}+ style={{+ width: `${metrics.width}px`,+ height: `${metrics.height}px`,+ borderRadius,+ }}+ >+ {lastPreviewFrame ? (+ <img+ src={lastPreviewFrame.dataUrl}+ alt=""+ aria-hidden+ className={classNames(+ "cap-extension-camera-last-frame",+ livePreviewReady && "is-hidden",+ webcam.mirror && "is-mirrored",+ )}+ />+ ) : null}+ <iframe+ ref={iframeRef}+ src={PREVIEW_SRC}+ title="Cap camera preview"+ allow="camera; microphone; autoplay; picture-in-picture"+ className="cap-extension-camera-iframe"+ onLoad={handlePreviewLoad}+ />+ {!livePreviewReady && !showPreviewError ? (+ <div className="cap-extension-camera-loading" aria-hidden>+ <span />+ </div>+ ) : null}+ {previewError && showPreviewError ? (+ <div className="cap-extension-camera-error">{previewError}</div>+ ) : null}+ {isInPictureInPicture ? (+ <div className="cap-extension-camera-pip-active">+ <div>+ <span>Picture in Picture active</span>+ <button+ type="button"+ aria-label="Exit Picture in Picture"+ onClick={handleTogglePictureInPicture}+ >+ <X size={12} aria-hidden />+ </button>+ </div>+ </div>+ ) : null}+ </div>+ </div>+ </div>+ );++ return (+ <>+ {cameraWindow}+ <video+ ref={pipVideoRef}+ className="cap-extension-parent-pip-video"+ autoPlay+ playsInline+ muted+ disablePictureInPicture={false}+ controlsList="nodownload nofullscreen noremoteplayback"+ />+ <RecorderPanelOverlay onOpenChange={setRecorderPanelOpen} />+ <RecordingBarOverlay recorderPanelOpen={recorderPanelOpen} />+ {isDragging ? (+ <button+ type="button"+ className="cap-extension-drag-surface"+ tabIndex={-1}+ aria-label="Move camera preview"+ onPointerMove={(event) => moveDrag(event.clientX, event.clientY)}+ onPointerUp={endDrag}+ onPointerCancel={endDrag}+ />+ ) : null}+ </>+ );+}++const isExtensionContextValid = () => {+ try {+ return Boolean(chrome.runtime?.id);+ } catch {+ return false;+ }+};++const TEARDOWN_EVENT = "cap-extension-overlay-teardown";++const watchExtensionContext = (root: HTMLElement, teardown: () => void) => {+ const interval = window.setInterval(() => {+ if (!isExtensionContextValid()) {+ window.clearInterval(interval);+ teardown();+ root.remove();+ }+ }, 5000);+ return () => window.clearInterval(interval);+};++const mountOverlay = () => {+ // Re-initializing this module (extension reload loads a fresh copy in a+ // new isolated world) must fully unmount the previous React tree:+ // removing only its DOM node would leak the tree's chrome listeners,+ // polls, and clock timers for the rest of the page's life. The teardown+ // event reaches the prior execution's listener through the shared DOM.+ const existingRoot = document.getElementById(ROOT_ID);+ if (existingRoot) {+ existingRoot.dispatchEvent(new Event(TEARDOWN_EVENT));+ existingRoot.remove();+ }++ const root = document.createElement("div");+ root.id = ROOT_ID;+ root.dataset.capMounted = "true";+ const shadow = root.attachShadow({ mode: "closed" });+ const style = document.createElement("style");+ style.textContent = overlayCss;+ const app = document.createElement("div");+ shadow.append(style, app);+ document.documentElement.append(root);+ const reactRoot = createRoot(app);+ reactRoot.render(<OverlayApp />);+ const unmount = () => {+ try {+ reactRoot.unmount();+ } catch {}+ };+ const stopWatching = watchExtensionContext(root, unmount);+ root.addEventListener(+ TEARDOWN_EVENT,+ () => {+ stopWatching();+ unmount();+ },+ { once: true },+ );+};++let initialized = false;++// This module is lazily imported by the bootstrap content script, which is+// the only manifest-declared script; nothing mounts as an import side+// effect. The bootstrap passes along any service-worker messages it+// acknowledged while the module was downloading so they replay into the+// freshly mounted tree.+export const init = (pendingMessages: readonly unknown[] = []) => {+ if (initialized) return;+ initialized = true;+ startupMessages = pendingMessages;+ if (document.documentElement) {+ mountOverlay();+ } else {+ document.addEventListener("DOMContentLoaded", mountOverlay, { once: true });+ }+};diff --git a/apps/chrome-extension/src/content/recording-bar.tsx b/apps/chrome-extension/src/content/recording-bar.tsxnew file mode 100644index 000000000..8c219ef5b--- /dev/null+++ b/apps/chrome-extension/src/content/recording-bar.tsx@@ -0,0 +1,566 @@+import { Pause, Play, Square, X } from "lucide-react";+import {+ type PointerEvent as ReactPointerEvent,+ useCallback,+ useEffect,+ useRef,+ useState,+} from "react";+import { formatDuration } from "../shared/format-duration";+import {+ isOverlayMessage,+ isRecordingStatusBroadcast,+} from "../shared/messages";+import { sendServiceWorkerMessage } from "../shared/runtime";+import {+ AUTH_KEY,+ loadAuth,+ loadOverlayUiState,+ loadSettings,+ loadSharedRecordingState,+ loadSharedUiState,+ OVERLAY_UI_STATE_KEY,+ RECORDING_STATE_KEY,+ SHARED_UI_STATE_KEY,+ updateOverlayUiState,+ updateSharedUiState,+} from "../shared/storage";+import type {+ OverlayPosition,+ RecordingPlan,+ RecordingStatus,+ SharedRecordingState,+} from "../shared/types";++const EDGE_PADDING = 16;+const BOTTOM_OFFSET = 28;+// Live updates arrive via status broadcasts and the session-storage mirror;+// this poll only reconciles drift, so it can be slow instead of hammering the+// service worker (and through it the offscreen document) from every tab.+const POLL_INTERVAL_MS = 5000;+const WARNING_THRESHOLD_MS = 60_000;+const LOGO_URL = chrome.runtime.getURL("icons/icon-48.png");++type BarStatus = {+ phase: "creating" | "recording" | "paused";+ startedAt: number;+ durationMs: number;+ updatedAt: number;+};++type BarControl = "stop-recording" | "pause-recording" | "resume-recording";++type RecordingBarOverlayProps = {+ recorderPanelOpen: boolean;+};++const toBarStatus = (+ status: RecordingStatus | undefined,+ current: BarStatus | null,+): BarStatus | null => {+ if (!status) return null;+ if (status.phase === "creating") {+ if (current?.phase === "creating") return current;+ const now = Date.now();+ return {+ phase: "creating",+ startedAt: now,+ durationMs: 0,+ updatedAt: now,+ };+ }+ if (status.phase !== "recording" && status.phase !== "paused") return null;+ return {+ phase: status.phase,+ startedAt: status.startedAt,+ durationMs: status.durationMs,+ updatedAt: status.updatedAt ?? status.startedAt,+ };+};++const classNames = (...values: Array<string | false | null | undefined>) =>+ values.filter(Boolean).join(" ");++const toOverlayPosition = (position: {+ x: number;+ y: number;+}): OverlayPosition => ({+ ...position,+ viewportWidth: window.innerWidth,+ viewportHeight: window.innerHeight,+ updatedAt: Date.now(),+});++// The timer derives from wall-clock deltas against the shared status, so as+// long as every tab holds the same status object their clocks read the same.+const currentDurationMs = (status: BarStatus, now: number) =>+ status.phase === "creating"+ ? status.durationMs+ : status.phase === "recording"+ ? status.durationMs + Math.max(0, now - status.updatedAt)+ : status.durationMs;++export function RecordingBarOverlay({+ recorderPanelOpen,+}: RecordingBarOverlayProps) {+ const [status, setStatus] = useState<BarStatus | null>(null);+ const [plan, setPlan] = useState<RecordingPlan | null>(null);+ const [signedIn, setSignedIn] = useState(false);+ const [readyDismissed, setReadyDismissed] = useState(false);+ const [position, setPosition] = useState<{ x: number; y: number } | null>(+ null,+ );+ const [persistedBarPosition, setPersistedBarPosition] =+ useState<OverlayPosition | null>(null);+ const [isDragging, setIsDragging] = useState(false);+ const [busy, setBusy] = useState(false);+ const [now, setNow] = useState(() => Date.now());+ const dragOffsetRef = useRef({ x: 0, y: 0 });+ const barRef = useRef<HTMLDivElement>(null);+ const planRef = useRef<RecordingPlan | null>(null);+ const positionRef = useRef<{ x: number; y: number } | null>(null);+ const recorderPanelOpenRef = useRef(false);++ useEffect(() => {+ planRef.current = plan;+ }, [plan]);++ useEffect(() => {+ positionRef.current = position;+ }, [position]);++ const applyResponse = useCallback(+ (nextStatus: RecordingStatus | undefined, nextPlan?: RecordingPlan) => {+ if (nextPlan) setPlan(nextPlan);+ setStatus((current) => toBarStatus(nextStatus, current));+ setNow(Date.now());+ },+ [],+ );++ const applySharedState = useCallback(+ (state: SharedRecordingState | null) => {+ if (!state) return;+ applyResponse(state.status, state.plan ?? undefined);+ },+ [applyResponse],+ );++ const refresh = useCallback(() => {+ sendServiceWorkerMessage({+ target: "service-worker",+ type: "get-recording-status",+ })+ .then((response) => {+ if (response.ok) applyResponse(response.status, response.plan);+ })+ .catch(() => undefined);+ }, [applyResponse]);++ useEffect(() => {+ refresh();+ }, [refresh]);++ useEffect(() => {+ if (recorderPanelOpen && !recorderPanelOpenRef.current) {+ refresh();+ }+ recorderPanelOpenRef.current = recorderPanelOpen;+ }, [recorderPanelOpen, refresh]);++ // chrome.storage.session is the cross-tab source of truth for both the+ // recording state and the shared UI flags; the storage change events reach+ // every tab (including backgrounded ones), which keeps each bar in lockstep+ // without per-tab polling round-trips.+ useEffect(() => {+ let disposed = false;++ const syncOverlayUiState = () => {+ loadOverlayUiState()+ .then((state) => {+ if (!disposed) {+ setPersistedBarPosition(state.recordingBarPosition);+ }+ })+ .catch(() => undefined);+ };++ const syncSharedRecordingState = () => {+ loadSharedRecordingState()+ .then((state) => {+ if (!disposed) applySharedState(state);+ })+ .catch(() => undefined);+ };++ const syncSharedUiState = () => {+ loadSharedUiState()+ .then((state) => {+ if (!disposed) setReadyDismissed(state.readyBarDismissed);+ })+ .catch(() => undefined);+ };++ const syncAuthState = () => {+ loadAuth()+ .then((auth) => {+ if (!disposed) setSignedIn(auth !== null);+ })+ .catch(() => undefined);+ };++ const handleStorageChange = (+ changes: Record<string, chrome.storage.StorageChange>,+ areaName: string,+ ) => {+ if (areaName === "local" && changes[OVERLAY_UI_STATE_KEY]) {+ syncOverlayUiState();+ }+ if (areaName === "local" && changes[AUTH_KEY]) {+ syncAuthState();+ }+ if (areaName === "session" && changes[RECORDING_STATE_KEY]) {+ syncSharedRecordingState();+ }+ if (areaName === "session" && changes[SHARED_UI_STATE_KEY]) {+ syncSharedUiState();+ }+ };++ syncOverlayUiState();+ syncSharedRecordingState();+ syncSharedUiState();+ syncAuthState();+ chrome.storage.onChanged.addListener(handleStorageChange);+ return () => {+ disposed = true;+ chrome.storage.onChanged.removeListener(handleStorageChange);+ };+ }, [applySharedState]);++ useEffect(() => {+ const handleVisibility = () => {+ if (document.visibilityState !== "visible") return;+ // Recompute the clock immediately so the first painted frame after a+ // tab switch already shows the right time, then reconcile from the+ // session-storage mirror: reading storage does not wake the service+ // worker, unlike a get-recording-status round trip, and the slow+ // poll below still corrects any drift while the bar is active.+ setNow(Date.now());+ loadSharedRecordingState()+ .then((state) => applySharedState(state))+ .catch(() => undefined);+ };+ document.addEventListener("visibilitychange", handleVisibility);+ return () =>+ document.removeEventListener("visibilitychange", handleVisibility);+ }, [applySharedState]);++ useEffect(() => {+ const handleMessage = (message: unknown) => {+ if (isRecordingStatusBroadcast(message)) {+ applyResponse(message.status);+ if (!planRef.current) refresh();+ return false;+ }+ if (!isOverlayMessage(message)) return false;+ refresh();+ return false;+ };+ chrome.runtime.onMessage.addListener(handleMessage);+ return () => chrome.runtime.onMessage.removeListener(handleMessage);+ }, [applyResponse, refresh]);++ const active = status !== null;+ // The "Ready to record" bar only makes sense once the user can actually+ // start a recording, which requires being signed in.+ const ready = signedIn && !active && recorderPanelOpen && !readyDismissed;+ const visible = active || ready;++ useEffect(() => {+ if (!active) return;+ setNow(Date.now());+ const interval = window.setInterval(() => setNow(Date.now()), 500);+ return () => window.clearInterval(interval);+ }, [active]);++ useEffect(() => {+ if (!active) return;+ const interval = window.setInterval(() => {+ if (document.visibilityState === "visible") refresh();+ }, POLL_INTERVAL_MS);+ return () => window.clearInterval(interval);+ }, [active, refresh]);++ const clampToViewport = useCallback((value: { x: number; y: number }) => {+ const rect = barRef.current?.getBoundingClientRect();+ const width = rect?.width ?? 360;+ const height = rect?.height ?? 64;+ const maxX = Math.max(+ EDGE_PADDING,+ window.innerWidth - width - EDGE_PADDING,+ );+ const maxY = Math.max(+ EDGE_PADDING,+ window.innerHeight - height - EDGE_PADDING,+ );+ return {+ x: Math.min(Math.max(value.x, EDGE_PADDING), maxX),+ y: Math.min(Math.max(value.y, EDGE_PADDING), maxY),+ };+ }, []);++ useEffect(() => {+ if (!ready || isDragging) return;+ const bar = barRef.current;+ if (!bar) return;+ const reposition = () => {+ const rect = bar.getBoundingClientRect();+ if (rect.width === 0) return;+ const restored = persistedBarPosition+ ? {+ x: persistedBarPosition.x,+ y: persistedBarPosition.y,+ }+ : {+ x: (window.innerWidth - rect.width) / 2,+ y: window.innerHeight - rect.height - BOTTOM_OFFSET,+ };+ setPosition(clampToViewport(restored));+ };+ reposition();+ const observer = new ResizeObserver(reposition);+ observer.observe(bar);+ return () => observer.disconnect();+ }, [clampToViewport, persistedBarPosition, isDragging, ready]);++ useEffect(() => {+ if (!ready) return;+ const handleResize = () => {+ setPosition((previous) =>+ previous ? clampToViewport(previous) : previous,+ );+ };+ window.addEventListener("resize", handleResize);+ return () => window.removeEventListener("resize", handleResize);+ }, [clampToViewport, ready]);++ const handlePointerDown = useCallback(+ (event: ReactPointerEvent<HTMLDivElement>) => {+ if ((event.target as HTMLElement).closest("[data-controls]")) return;+ event.preventDefault();+ event.stopPropagation();+ // Capture the pointer so the drag survives the cursor crossing+ // iframes (for example the camera preview) on the page.+ event.currentTarget.setPointerCapture(event.pointerId);+ setIsDragging(true);+ dragOffsetRef.current = {+ x: event.clientX - (position?.x ?? EDGE_PADDING),+ y: event.clientY - (position?.y ?? EDGE_PADDING),+ };+ },+ [position],+ );++ useEffect(() => {+ if (!isDragging) return;+ const handlePointerMove = (event: PointerEvent) => {+ setPosition(+ clampToViewport({+ x: event.clientX - dragOffsetRef.current.x,+ y: event.clientY - dragOffsetRef.current.y,+ }),+ );+ };+ const handlePointerUp = () => {+ setIsDragging(false);+ const nextPosition = positionRef.current;+ if (!nextPosition) return;+ void updateOverlayUiState((current) => ({+ ...current,+ recordingBarPosition: toOverlayPosition(nextPosition),+ }))+ .then((state) => setPersistedBarPosition(state.recordingBarPosition))+ .catch(() => undefined);+ };+ window.addEventListener("pointermove", handlePointerMove);+ window.addEventListener("pointerup", handlePointerUp);+ window.addEventListener("pointercancel", handlePointerUp);+ return () => {+ window.removeEventListener("pointermove", handlePointerMove);+ window.removeEventListener("pointerup", handlePointerUp);+ window.removeEventListener("pointercancel", handlePointerUp);+ };+ }, [clampToViewport, isDragging]);++ const sendControl = useCallback(+ (type: BarControl) => {+ setBusy(true);+ sendServiceWorkerMessage({ target: "service-worker", type })+ .then((response) => {+ if (response.ok) applyResponse(response.status, response.plan);+ })+ .catch(() => undefined)+ .finally(() => setBusy(false));+ },+ [applyResponse],+ );++ const startRecording = useCallback(() => {+ setBusy(true);+ loadSettings()+ .then((settings) =>+ sendServiceWorkerMessage({+ target: "service-worker",+ type: "start-recording",+ mode: settings.capture.recordingMode,+ }),+ )+ .then((response) => {+ if (response.ok) applyResponse(response.status, response.plan);+ })+ .catch(() => undefined)+ .finally(() => setBusy(false));+ }, [applyResponse]);++ const dismissReadyBar = useCallback(() => {+ setReadyDismissed(true);+ void updateSharedUiState((current) => ({+ ...current,+ readyBarDismissed: true,+ updatedAt: Date.now(),+ })).catch(() => undefined);+ }, []);++ if (!visible) return null;++ if (status === null) {+ return (+ <div+ ref={barRef}+ className={classNames(+ "cap-extension-control-bar",+ isDragging && "is-dragging",+ )}+ role="toolbar"+ aria-label="Cap recording controls"+ style={{+ left: `${position?.x ?? EDGE_PADDING}px`,+ top: position ? `${position.y}px` : "50%",+ visibility: position ? "visible" : "hidden",+ }}+ onPointerDown={handlePointerDown}+ >+ <div className="cap-extension-control-bar-info">+ <img+ className="cap-extension-control-bar-logo"+ src={LOGO_URL}+ alt=""+ draggable={false}+ />+ <div className="cap-extension-control-bar-text">+ <span className="cap-extension-control-bar-title">+ Ready to record+ </span>+ <span className="cap-extension-control-bar-subtitle">+ <span+ className="cap-extension-control-bar-dot is-ready"+ aria-hidden+ />+ Cap+ </span>+ </div>+ </div>+ <div className="cap-extension-control-bar-divider" aria-hidden />+ <div className="cap-extension-control-bar-actions" data-controls>+ <button+ type="button"+ className="cap-extension-control-bar-pill is-start"+ disabled={busy}+ onClick={startRecording}+ >+ <Play size={14} fill="currentColor" strokeWidth={0} aria-hidden />+ Start recording+ </button>+ <button+ type="button"+ className="cap-extension-control-bar-icon-button is-quiet"+ aria-label="Hide recording bar"+ title="Hide bar"+ onClick={dismissReadyBar}+ >+ <X size={20} aria-hidden />+ </button>+ </div>+ </div>+ );+ }++ const isCreating = status.phase === "creating";+ const isPaused = status.phase === "paused";+ const maxMs =+ plan && !plan.isPro && plan.maxRecordingSeconds !== null+ ? plan.maxRecordingSeconds * 1000+ : null;+ const durationMs = currentDurationMs(status, now);+ const displayMs =+ maxMs !== null ? Math.max(0, maxMs - durationMs) : durationMs;+ const isWarning = maxMs !== null && displayMs <= WARNING_THRESHOLD_MS;++ // While recording the bar is a fixed rail pinned to the middle left of the+ // page: just the clock and Stop, identical in every tab.+ return (+ <div+ className="cap-extension-recording-rail"+ role="toolbar"+ aria-label="Cap recording controls"+ >+ <div+ className={classNames(+ "cap-extension-recording-rail-timer",+ isWarning && "is-warning",+ )}+ >+ <span+ className={classNames(+ "cap-extension-control-bar-dot",+ isCreating+ ? "is-creating"+ : isPaused+ ? "is-paused"+ : "is-recording",+ )}+ aria-hidden+ />+ <span className="cap-extension-recording-rail-time">+ {formatDuration(displayMs)}+ </span>+ </div>+ <button+ type="button"+ className="cap-extension-control-bar-icon-button"+ aria-label={isPaused ? "Resume recording" : "Pause recording"}+ title={isPaused ? "Resume" : "Pause"}+ disabled={busy || isCreating}+ onClick={() =>+ sendControl(isPaused ? "resume-recording" : "pause-recording")+ }+ >+ {isPaused ? (+ <Play size={19} fill="currentColor" strokeWidth={0} aria-hidden />+ ) : (+ <Pause size={19} fill="currentColor" strokeWidth={0} aria-hidden />+ )}+ </button>+ <button+ type="button"+ className="cap-extension-control-bar-pill is-stop"+ disabled={busy}+ onClick={() => sendControl("stop-recording")}+ >+ <Square size={13} fill="currentColor" strokeWidth={0} aria-hidden />+ Stop+ </button>+ </div>+ );+}