Skip to content

Commit d502ca2

Browse files
committed
Waiting for the focus to return from KeePassXC-desktop back to the page
1 parent d114a10 commit d502ca2

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

keepassxc-browser/content/passkeys.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,33 @@
185185
throw new DOMException(errorMessage, 'UnknownError');
186186
};
187187

188+
/**
189+
* @template T
190+
* @param {() => T} callback
191+
* @returns {Promise<T>}
192+
*/
193+
const callOnFocus = function (callback) {
194+
return new Promise((resolve) => {
195+
// TODO: maybe need `window.top.document` -- for iframes?
196+
197+
if (document.hasFocus()) {
198+
return resolve(callback());
199+
}
200+
201+
const controller = new AbortController();
202+
203+
/** @param {Event} e */
204+
function listener(e) {
205+
if (!e.isTrusted) return;
206+
207+
controller.abort();
208+
resolve(callback());
209+
}
210+
211+
window.addEventListener('focus', listener, { capture: true, passive: true, signal: controller.signal });
212+
});
213+
};
214+
188215
const originalCredentials = navigator.credentials;
189216

190217
const passkeysCredentials = {
@@ -205,7 +232,7 @@
205232
throwError(response?.errorCode, response?.errorMessage);
206233
return null;
207234
}
208-
return originalCredentials.create(options);
235+
return callOnFocus(() => originalCredentials.create(options));
209236
}
210237

211238
return createPublicKeyCredential(response.publicKey);
@@ -231,7 +258,7 @@
231258
throwError(response?.errorCode, response?.errorMessage);
232259
return null;
233260
}
234-
return originalCredentials.get(options);
261+
return callOnFocus(() => originalCredentials.get(options));
235262
}
236263

237264
return createPublicKeyCredential(response.publicKey);

0 commit comments

Comments
 (0)