2f841825d4c1
fix lock-on-close with port disconnect instead of pagehide sendMessage (v1.9.2)
- time
- 2026-04-24 23:51 UTC · 6 months ago
- machine
- koh2
- parent
- cfa0b3390c64
↓ peach-browser-2f841825.tar.gzatlas at this save
files changed5 files
manifest.firefox.json+1 −1manifest.json+1 −1package.json+1 −1src/background/index.ts+13 −0src/popup/App.tsx+8 −7manifest.firefox.json+1 −1
@@ -1,7 +1,7 @@11 {22 "manifest_version": 3,33 "name": "Peach",4- "version": "1.9.1",4+ "version": "1.9.2",55 "description": "Zero-knowledge password manager",66 "browser_specific_settings": {77 "gecko": {manifest.json+1 −1
@@ -1,7 +1,7 @@11 {22 "manifest_version": 3,33 "name": "Peach",4- "version": "1.9.1",4+ "version": "1.9.2",55 "description": "Zero-knowledge password manager",66 "action": {77 "default_popup": "src/popup/index.html",package.json+1 −1
@@ -1,6 +1,6 @@11 {22 "name": "peach",3- "version": "1.9.1",3+ "version": "1.9.2",44 "description": "Zero-knowledge password manager",55 "type": "module",66 "scripts": {src/background/index.ts+13 −0
@@ -1,5 +1,18 @@11 import { setupMessageHandler } from "./messages";22 import { setupAutoLock } from "./alarms";3+import { getSettings, isUnlocked, lock } from "./vault";34 45 setupMessageHandler();56 void setupAutoLock();7+8+// Lock-on-close via port disconnect. chrome.runtime.sendMessage in `pagehide`9+// is unreliable in MV3 — the popup context tears down before the message lands.10+// A persistent port survives the unload; the SW locks when it sees the disconnect.11+chrome.runtime.onConnect.addListener((port) => {12+ if (port.name !== "popup") return;13+ port.onDisconnect.addListener(() => {14+ if (isUnlocked() && getSettings().lockOnClose !== false) {15+ lock();16+ }17+ });18+});src/popup/App.tsx+8 −7
@@ -296,13 +296,14 @@296296 import("./views/CJKImport").catch(() => {}),297297 ]);298298 299- const handlePageHide = () => {300- if (!lockOnClose() || view() === "lock") return;301- chrome.runtime.sendMessage({ type: "LOCK" });302- };303-304- window.addEventListener("pagehide", handlePageHide);305- onCleanup(() => window.removeEventListener("pagehide", handlePageHide));299+ // Lock-on-close via a persistent port. sendMessage in pagehide is300+ // unreliable — the popup context tears down before the message lands.301+ // The SW's onConnect listener locks when this port disconnects.302+ let popupPort: chrome.runtime.Port | null = null;303+ try {304+ popupPort = chrome.runtime.connect({ name: "popup" });305+ } catch { /* extension context invalidated — ignore */ }306+ onCleanup(() => { try { popupPort?.disconnect(); } catch {} });306307 307308 // Background auto-lock broadcasts VAULT_LOCKED so the popup drops stale308309 // entries instead of waiting for the next GET_ENTRIES round-trip.