index.ts

988 B · raw

1import { setupMessageHandler } from "./messages";2import { setupAutoLock } from "./alarms";3import { getSettings, isUnlocked, lock } from "./vault";4import { isRecoveryConfirmPending } from "./recovery-pending";56setupMessageHandler();7void setupAutoLock();89// Lock-on-close via port disconnect. chrome.runtime.sendMessage in `pagehide`10// is unreliable in MV3 — the popup context tears down before the message lands.11// A persistent port survives the unload; the SW locks when it sees the disconnect.12chrome.runtime.onConnect.addListener((port) => {13  if (port.name !== "popup") return;14  port.onDisconnect.addListener(async () => {15    if (!isUnlocked() || getSettings().lockOnClose === false) return;16    // Skip lock while the user is mid-recovery-confirmation; locking here17    // would discard the in-memory key needed to re-derive the recovery18    // words on the next popup open, permanently losing them.19    if (await isRecoveryConfirmPending()) return;20    lock();21  });22});