diff --git a/src/background/service-worker.js b/src/background/service-worker.js index f7c0963..b8d5a4b 100644 --- a/src/background/service-worker.js +++ b/src/background/service-worker.js @@ -184,6 +184,23 @@ const messageHandlers = { sendResponse({ locked }); }, + // Content script requests decrypted data on page load when it sees + // encrypted storage but the key is already cached (e.g. after a sync import). + async 'vault:request-unlock'(_message, sender) { + const locked = await Storage.isLocked(); + if (locked) return; // key not available — user must unlock via popup + + const mappings = await Storage.getMappings(); + const identity = await Storage.getIdentity(); + const settings = await Storage.getSettings(); + api.tabs.sendMessage(sender.tab.id, { + type: 'vault:unlocked', + mappings, + identity, + settings, + }).catch(() => {}); + }, + async 'vault:unlocked'() { // User unlocked the vault — clear the LOCK badge and refresh icon api.action.setBadgeText({ text: '' }); diff --git a/src/content/injector.js b/src/content/injector.js index 03c9dc7..9ec3f47 100644 --- a/src/content/injector.js +++ b/src/content/injector.js @@ -60,13 +60,19 @@ const result = await api.storage.local.get(['ss_mappings', 'ss_identity', 'ss_settings']); const settings = result.ss_settings || { enabled: true }; - // Check if data is encrypted (locked) — pass empty config + // Check if data is encrypted — pass empty config and request decryption // The background will send decrypted data via vault:unlocked when ready const isLocked = result.ss_mappings?._ssLocalEncrypted || result.ss_identity?._ssLocalEncrypted; const mappings = isLocked ? [] : (result.ss_mappings || []); const identityData = isLocked ? {} : (result.ss_identity || {}); + // If data is encrypted but the vault key may already be cached + // (e.g. after a sync import), ask the background to push decrypted data + if (isLocked) { + api.runtime.sendMessage({ type: 'vault:request-unlock' }).catch(() => {}); + } + // Merge active profiles into a flat identity object for the content script const identity = mergeProfiles(identityData); diff --git a/src/options/options.js b/src/options/options.js index 8d81e9e..9123539 100644 --- a/src/options/options.js +++ b/src/options/options.js @@ -201,6 +201,7 @@ document.addEventListener('DOMContentLoaded', async () => { setGistSyncStatus('Pull failed: ' + r2.reason, 'error'); } else if (r2.imported) { setGistSyncStatus(`Pulled (${r2.time}). Refreshing…`, 'ok'); + api.runtime.sendMessage({ type: 'vault:unlocked' }).catch(() => {}); mappings = await Storage.getMappings(); settings = await Storage.getSettings(); renderMappings(); @@ -215,6 +216,7 @@ document.addEventListener('DOMContentLoaded', async () => { setGistSyncStatus('Pull failed: ' + r.reason, 'error'); } else if (r.imported) { setGistSyncStatus(`Pulled (${r.time}). Refreshing…`, 'ok'); + api.runtime.sendMessage({ type: 'vault:unlocked' }).catch(() => {}); mappings = await Storage.getMappings(); settings = await Storage.getSettings(); renderMappings(); @@ -258,6 +260,7 @@ document.addEventListener('DOMContentLoaded', async () => { setUrlSyncStatus('Pull failed: ' + r2.reason, 'error'); } else if (r2.imported) { setUrlSyncStatus(`Pulled (${r2.time}). Refreshing…`, 'ok'); + api.runtime.sendMessage({ type: 'vault:unlocked' }).catch(() => {}); mappings = await Storage.getMappings(); settings = await Storage.getSettings(); renderMappings(); @@ -272,6 +275,7 @@ document.addEventListener('DOMContentLoaded', async () => { setUrlSyncStatus('Pull failed: ' + r.reason, 'error'); } else if (r.imported) { setUrlSyncStatus(`Pulled (${r.time}). Refreshing…`, 'ok'); + api.runtime.sendMessage({ type: 'vault:unlocked' }).catch(() => {}); mappings = await Storage.getMappings(); settings = await Storage.getSettings(); renderMappings();