Skip to content

Commit 7c6b3bf

Browse files
committed
Waiting for the focus to return from KeePassXC-desktop back to the page
1 parent 9e5a936 commit 7c6b3bf

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

keepassxc-browser/content/passkeys.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@
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) {
@@ -205,6 +228,7 @@
205228
throwError(response?.errorCode, response?.errorMessage);
206229
return null;
207230
}
231+
await waitForFocus();
208232
return originalCredentials.create(options);
209233
}
210234

@@ -231,6 +255,7 @@
231255
throwError(response?.errorCode, response?.errorMessage);
232256
return null;
233257
}
258+
await waitForFocus();
234259
return originalCredentials.get(options);
235260
}
236261

0 commit comments

Comments
 (0)