@@ -76,7 +76,6 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
7676 const tab = sender . tab ;
7777
7878 // helpers
79- const async = ( ) => true ;
8079 const respondNoTab = ( payload ) => {
8180 try { sendResponse ( payload ) ; } catch ( _ ) { }
8281 } ;
@@ -175,20 +174,28 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
175174 } , { ok : false } ) ;
176175 }
177176 case 'get_status' : {
178- const tId = tab && tab . id ;
179- const error = ( tId && page . tabs [ tId ] && page . tabs [ tId ] . errorMessage ) || undefined ;
180- sendResponse ( {
181- configured : keepass . isConfigured ( ) ,
182- associated : keepass . isAssociated ( ) ,
183- identifier : ( keepass . databaseHash in keepass . keyRing )
184- ? keepass . keyRing [ keepass . databaseHash ] . id
185- : null ,
186- keePassHttpAvailable : keepass . isKeePassHttpAvailable ,
187- databaseClosed : keepass . isDatabaseClosed ,
188- encryptionKeyUnrecognized : keepass . isEncryptionKeyUnrecognized ,
189- error
177+ // Clear any stale error messages before computing status
178+ if ( typeof page !== 'undefined' && page . tabs ) {
179+ for ( const t of Object . values ( page . tabs ) ) {
180+ if ( t && 'errorMessage' in t ) delete t . errorMessage ;
181+ }
182+ }
183+ refreshStatus ( async statusData => {
184+ if ( ! statusData . associated && statusData . configured && ! statusData . databaseClosed ) {
185+ // Attempt lightweight bootstrap / reassociation
186+ try {
187+ await keepass . bootstrapAssociation ( ) ;
188+ } catch ( e ) {
189+ console . warn ( 'Reassociation attempt failed' , e ) ;
190+ }
191+ // Refresh after attempt
192+ return refreshStatus ( newStatus => {
193+ try { sendResponse ( newStatus ) ; } catch ( _ ) { }
194+ } ) ;
195+ }
196+ try { sendResponse ( statusData ) ; } catch ( _ ) { }
190197 } ) ;
191- return ;
198+ return true ;
192199 }
193200 case 'check_update_keepasshttp' : {
194201 keepass . checkForNewKeePassHttpVersion ( )
@@ -290,4 +297,22 @@ chrome.runtime.onInstalled.addListener(details => {
290297} ) ;
291298
292299// Lazy on-demand init (in case worker was cold-started)
293- chrome . runtime . onStartup && chrome . runtime . onStartup . addListener ( ( ) => safeInit ( { reason : 'startup' } ) ) ;
300+ chrome . runtime . onStartup && chrome . runtime . onStartup . addListener ( ( ) => safeInit ( { reason : 'startup' } ) ) ;
301+
302+ function buildStatus ( ) {
303+ return {
304+ configured : keepass . isConfigured ( ) ,
305+ associated : keepass . isAssociated ( ) ,
306+ identifier : ( keepass . databaseHash in keepass . keyRing )
307+ ? keepass . keyRing [ keepass . databaseHash ] . id
308+ : null ,
309+ keePassHttpAvailable : keepass . isKeePassHttpAvailable ,
310+ databaseClosed : keepass . isDatabaseClosed ,
311+ encryptionKeyUnrecognized : keepass . isEncryptionKeyUnrecognized ,
312+ error : ( page . tabs && Object . values ( page . tabs ) . find ( t => t . errorMessage ) || { } ) . errorMessage
313+ } ;
314+ }
315+
316+ function refreshStatus ( cb ) {
317+ keepass . refreshStatus ( ( ) => cb ( buildStatus ( ) ) ) ;
318+ }
0 commit comments