From 2d06cc6c147d54621b2fa6650094a16a9da736f8 Mon Sep 17 00:00:00 2001 From: varjolintu Date: Tue, 10 Dec 2024 18:34:29 +0200 Subject: [PATCH] Show error notification when database is not connected --- keepassxc-browser/_locales/en/messages.json | 6 +++++- keepassxc-browser/content/fill.js | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/keepassxc-browser/_locales/en/messages.json b/keepassxc-browser/_locales/en/messages.json index 27347e4e..dd598ee6 100644 --- a/keepassxc-browser/_locales/en/messages.json +++ b/keepassxc-browser/_locales/en/messages.json @@ -237,7 +237,11 @@ }, "errorNotConnected": { "message": "Not connected to KeePassXC.", - "description": "Error notification shown when not connected to KeePassXC" + "description": "Error notification shown when not connected to KeePassXC." + }, + "errorCurrentDatabaseNotConnected": { + "message": "Current database is not connected.", + "description": "Error notification shown when current database is not connected during action." }, "passwordGeneratorErrorTooLong": { "message": "Error: The generated password is longer than the allowed length!", diff --git a/keepassxc-browser/content/fill.js b/keepassxc-browser/content/fill.js index 7bfe8d80..b7846cb1 100644 --- a/keepassxc-browser/content/fill.js +++ b/keepassxc-browser/content/fill.js @@ -28,7 +28,7 @@ kpxcFill.fillInFromActiveElement = async function(passOnly = false) { await kpxc.receiveCredentialsIfNecessary(); if (kpxc.credentials.length === 0) { logDebug(`Error: Credential list is empty for: ${document.location.origin}`); - kpxcUI.createNotification('error', `${tr('credentialsNoLoginsFound')} ${document.location.origin}`); + showErrorNotification(`${tr('credentialsNoLoginsFound')} ${document.location.origin}`); return; } @@ -119,7 +119,7 @@ kpxcFill.fillFromTOTP = async function(target) { const credentialList = await kpxc.updateTOTPList(); if (!credentialList || credentialList?.length === 0) { - kpxcUI.createNotification('warning', tr('credentialsNoTOTPFound')); + showErrorNotification(tr('credentialsNoTOTPFound'), 'warning'); return; } @@ -197,7 +197,7 @@ kpxcFill.fillFromUsernameIcon = async function(combination) { await kpxc.receiveCredentialsIfNecessary(); if (kpxc.credentials.length === 0) { logDebug(`Error: Credential list is empty for: ${document.location.origin}`); - kpxcUI.createNotification('error', `${tr('credentialsNoLoginsFound')} ${document.location.origin}`); + showErrorNotification(`${tr('credentialsNoLoginsFound')} ${document.location.origin}`); return; } else if (kpxc.credentials.length > 1 && kpxc.settings.autoCompleteUsernames) { kpxcUserAutocomplete.showList(combination.username || combination.password); @@ -352,3 +352,13 @@ const passwordFillIsAllowed = function(elem) { return elem?.getLowerCaseAttribute('type') === 'password'; }; + +// Show a specific error notification if current database is not connected +const showErrorNotification = async function(errorMessage, notificationType = 'error') { + const connectedDatabase = await sendMessage('get_connected_database'); + if (!connectedDatabase?.identifier) { + kpxcUI.createNotification('error', tr('errorCurrentDatabaseNotConnected')); + } else { + kpxcUI.createNotification(notificationType, errorMessage); + } +};