File tree Expand file tree Collapse file tree
keepassxc-browser/content Expand file tree Collapse file tree Original file line number Diff line number Diff line change 145145 }
146146 } ;
147147
148+ /**
149+ * @returns {Promise<void> }
150+ */
151+ const waitForFocus = function ( ) {
152+ /*
153+ Some browsers (Firefox, Safari) reject requests to original `navigator.credentials.create/get` if the page
154+ is out of focus (when the user selects a passkey in KeePassXC-desktop).
155+
156+ `document.visibilityState` is not suitable: if the page is visible, but the focus is on another application
157+ (or DevTools), the request will be rejected.
158+ */
159+ return new Promise ( ( resolve ) => {
160+ if ( document . hasFocus ( ) ) {
161+ return resolve ( ) ;
162+ }
163+ document . addEventListener (
164+ 'focus' ,
165+ ( ) => resolve ( ) ,
166+ { capture : true , passive : true , once : true }
167+ ) ;
168+ } ) ;
169+ } ;
170+
148171 // Throws errors to a correct exceptions
149172 const throwError = function ( errorCode , errorMessage ) {
150173 if ( ( ! errorCode && ! errorMessage ) || errorCode === PASSKEYS_REQUEST_CANCELED ) {
205228 throwError ( response ?. errorCode , response ?. errorMessage ) ;
206229 return null ;
207230 }
231+ await waitForFocus ( ) ;
208232 return originalCredentials . create ( options ) ;
209233 }
210234
231255 throwError ( response ?. errorCode , response ?. errorMessage ) ;
232256 return null ;
233257 }
258+ await waitForFocus ( ) ;
234259 return originalCredentials . get ( options ) ;
235260 }
236261
You can’t perform that action at this time.
0 commit comments