Skip to content

Update popup content dynamically #2347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
"importScripts": "readonly",
"IMPROVED_DETECTION_PREDEFINED_SITELIST": "readonly",
"initColorTheme": "readonly",
"initializeHttpAuthLoginList": "readonly",
"initializeLoginList": "readonly",
"isEdge": "readonly",
"isFirefox": "readonly",
"keepass": "readonly",
Expand Down Expand Up @@ -164,9 +166,12 @@
"ORANGE_BUTTON": "readonly",
"page": "readonly",
"Pixels": "readonly",
"PublicKeyCredential": "readonly",
"PopupIcon": "readonly",
"PopupState": "readonly",
"PREDEFINED_SITELIST": "readonly",
"PublicKeyCredential": "readonly",
"RED_BUTTON": "readonly",
"removeAllChildren": "readonly",
"retrieveColorScheme": "readonly",
"sendMessage": "readonly",
"showNotification": "readonly",
Expand Down
2 changes: 1 addition & 1 deletion dist/manifest_chromium.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"choose_credential_fields": {
"description": "__MSG_popupChooseCredentialsText__"
},
"retrive_credentials_forced": {
"retrieve_credentials_forced": {
"description": "__MSG_popupReopenButton__"
},
"request_autotype": {
Expand Down
2 changes: 1 addition & 1 deletion dist/manifest_firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"choose_credential_fields": {
"description": "__MSG_popupChooseCredentialsText__"
},
"retrive_credentials_forced": {
"retrieve_credentials_forced": {
"description": "__MSG_popupReopenButton__"
},
"request_autotype": {
Expand Down
39 changes: 21 additions & 18 deletions keepassxc-browser/background/browserAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const browserActionWrapper = browser.action || browser.browserAction;
const browserAction = {};

browserAction.show = async function(tab, popupData) {
browserAction.updatePopupIcon = async function(tab, popupData) {
popupData ??= page.popupData;
page.popupData = popupData;

Expand All @@ -12,38 +12,33 @@ browserAction.show = async function(tab, popupData) {
});

if (popupData.popup && tab?.id) {
browserActionWrapper.setPopup({
tabId: tab.id,
popup: `popups/${popupData.popup}.html`
});

let badgeText = '';
if (popupData.popup === 'popup_login') {
if (popupData.popup === PopupState.LOGIN) {
badgeText = page.tabs[tab.id]?.loginList?.length;
} else if (popupData.popup === 'popup_httpauth') {
} else if (popupData.popup === PopupState.HTTP_AUTH) {
badgeText = page.tabs[tab.id]?.loginList?.logins?.length;
}

browserAction.setBadgeText(tab?.id, badgeText);
}
};

browserAction.showDefault = async function(tab) {
browserAction.updatePopup = async function(tab) {
const popupData = {
iconType: 'normal',
popup: 'popup'
iconType: PopupIcon.NORMAL,
popup: PopupState.DEFAULT
};

const response = await keepass.isConfigured().catch((err) => {
logError('Cannot show default popup: ' + err);
});

if (!response && !keepass.isKeePassXCAvailable) {
popupData.iconType = 'cross';
popupData.iconType = PopupIcon.CROSS;
} else if (!keepass.isAssociated() && !keepass.isDatabaseClosed) {
popupData.iconType = 'bang';
popupData.iconType = PopupIcon.BANG;
} else if (keepass.isKeePassXCAvailable && keepass.isDatabaseClosed) {
popupData.iconType = 'locked';
popupData.iconType = PopupIcon.LOCKED;
}

// Get the current tab if no tab given
Expand All @@ -52,13 +47,21 @@ browserAction.showDefault = async function(tab) {
return;
}

// Credentials are available
if (page?.tabs[tab.id]?.loginList.length > 0) {
popupData.iconType = 'normal';
popupData.popup = 'popup_login';
popupData.iconType = PopupIcon.NORMAL;
popupData.popup = PopupState.LOGIN;
browserAction.setBadgeText(tab?.id, page.tabs[tab.id]?.loginList.length);
}

await browserAction.show(tab, popupData);
// HTTP Basic Auth credentials are available
if (page.tabs[tab.id]?.loginList?.logins?.length > 0) {
popupData.iconType = PopupIcon.NORMAL;
popupData.popup = PopupState.HTTP_AUTH;
browserAction.setBadgeText(tab?.id, page.tabs[tab.id]?.loginList?.logins?.length);
}

await browserAction.updatePopupIcon(tab, popupData);
};

browserAction.setBadgeText = function(tabId, badgeText) {
Expand All @@ -73,7 +76,7 @@ browserAction.setBadgeText = function(tabId, badgeText) {
browserAction.generateIconName = async function(iconType) {
let name = 'icon_';
name += (await keepass.keePassXCUpdateAvailable()) ? 'new_' : '';
name += (!iconType || iconType === 'normal') ? 'normal' : iconType;
name += (!iconType || iconType === PopupIcon.NORMAL) ? PopupIcon.NORMAL : iconType;

let style = 'colored';
if (page?.settings?.useMonochromeToolbarIcon) {
Expand Down
2 changes: 1 addition & 1 deletion keepassxc-browser/background/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ function onDisconnected() {
keepass.databaseHash = '';

page.clearAllLogins();
keepass.updatePopup('cross');
keepass.updatePopup(PopupIcon.CROSS);
keepass.updateDatabaseHashToContent();
logError(`Failed to connect: ${(browser.runtime.lastError === null ? 'Unknown error' : browser.runtime.lastError.message)}`);
}
Expand Down
20 changes: 11 additions & 9 deletions keepassxc-browser/background/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ kpxcEvent.showStatus = async function(tab, configured, internalPoll) {
}

if (!internalPoll) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
}

const errorMessage = page.tabs[tab.id]?.errorMessage ?? undefined;
Expand All @@ -30,14 +30,14 @@ kpxcEvent.showStatus = async function(tab, configured, internalPoll) {

return {
associated: keepass.isAssociated(),

configured: configured,
databaseClosed: keepass.isDatabaseClosed,
encryptionKeyUnrecognized: keepass.isEncryptionKeyUnrecognized,
error: errorMessage,
iframeDetected: iframeDetected,
identifier: keyId,
keePassXCAvailable: keepass.isKeePassXCAvailable,
popupData: page.popupData,
showGettingStartedGuideAlert: page.settings.showGettingStartedGuideAlert,
showTroubleshootingGuideAlert: page.settings.showTroubleshootingGuideAlert,
usernameFieldDetected: usernameFieldDetected
Expand Down Expand Up @@ -154,13 +154,13 @@ kpxcEvent.onRemoveCredentialsFromTabInformation = async function(tab) {

kpxcEvent.onLoginPopup = async function(tab, logins) {
const popupData = {
iconType: 'normal',
popup: 'popup_login'
iconType: PopupIcon.NORMAL,
popup: PopupState.LOGIN
};

if (tab?.id) {
page.tabs[tab.id].loginList = logins;
await browserAction.show(tab, popupData);
await browserAction.updatePopupIcon(tab, popupData);
}
};

Expand All @@ -170,12 +170,14 @@ kpxcEvent.initHttpAuth = async function() {

kpxcEvent.onHTTPAuthPopup = async function(tab, data) {
const popupData = {
iconType: 'normal',
popup: 'popup_httpauth'
iconType: PopupIcon.NORMAL,
popup: PopupState.HTTP_AUTH
};

page.tabs[tab.id].loginList = data;
await browserAction.show(tab, popupData);
if (tab?.id) {
page.tabs[tab.id].loginList = data;
await browserAction.updatePopupIcon(tab, popupData);
}
};

kpxcEvent.onUsernameFieldDetected = async function(tab, detected) {
Expand Down
4 changes: 2 additions & 2 deletions keepassxc-browser/background/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const initListeners = async function() {
}

if (changeInfo.status === 'complete' && tab?.id) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
if (!page.tabs[tab.id]) {
page.createTabEntry(tab.id);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ const initListeners = async function() {
if (contextMenuItems.some(e => e.action === command)
|| command === 'redetect_fields'
|| command === 'choose_credential_fields'
|| command === 'retrive_credentials_forced'
|| command === 'retrieve_credentials_forced'
|| command === 'reload_extension') {
const tab = await getCurrentTab();
if (tab?.id) {
Expand Down
36 changes: 20 additions & 16 deletions keepassxc-browser/background/keepass.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ keepass.updateCredentials = async function(tab, args = []) {
const [ entryId, username, password, url, group, groupUuid ] = args;
const taResponse = await keepass.testAssociation(tab);
if (!taResponse) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
}

Expand Down Expand Up @@ -108,7 +108,7 @@ keepass.retrieveCredentials = async function(tab, args = []) {
const [ url, submiturl, triggerUnlock = false, httpAuth = false ] = args;
const taResponse = await keepass.testAssociation(tab, [ false, triggerUnlock ]);
if (!taResponse) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
}

Expand Down Expand Up @@ -145,14 +145,14 @@ keepass.retrieveCredentials = async function(tab, args = []) {

if (entries.length === 0) {
// Questionmark-icon is not triggered, so we have to trigger for the normal symbol
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
}

logDebug(`Found ${entries.length} entries for url ${url}`);
return entries;
}

browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
} catch (err) {
logError(`retrieveCredentials failed: ${err}`);
Expand All @@ -168,7 +168,7 @@ keepass.generatePassword = async function(tab) {
try {
const taResponse = await keepass.testAssociation(tab);
if (!taResponse) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return '';
}

Expand Down Expand Up @@ -235,7 +235,7 @@ keepass.associate = async function(tab) {
keepass.associated.value = true;
keepass.associated.hash = response.hash || 0;

browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return AssociatedAction.NEW_ASSOCIATION;
}

Expand Down Expand Up @@ -468,7 +468,7 @@ keepass.getDatabaseGroups = async function(tab) {
try {
const taResponse = await keepass.testAssociation(tab, [ false ]);
if (!taResponse) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
}

Expand All @@ -495,7 +495,7 @@ keepass.getDatabaseGroups = async function(tab) {
return groups;
}

browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
} catch (err) {
logError(`getDatabaseGroups failed: ${err}`);
Expand All @@ -508,7 +508,7 @@ keepass.createNewGroup = async function(tab, args = []) {
const [ groupName ] = args;
const taResponse = await keepass.testAssociation(tab, [ false ]);
if (!taResponse) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
}

Expand All @@ -534,7 +534,7 @@ keepass.createNewGroup = async function(tab, args = []) {
logError('getDatabaseGroups rejected');
}

browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
} catch (err) {
logError(`createNewGroup failed: ${err}`);
Expand Down Expand Up @@ -602,7 +602,7 @@ keepass.passkeysRegister = async function(tab, args = []) {
try {
const taResponse = await keepass.testAssociation(tab, [ false ]);
if (!taResponse || !keepass.isConnected || args.length < 2) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
}

Expand All @@ -623,7 +623,7 @@ keepass.passkeysRegister = async function(tab, args = []) {
return response;
}

browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
} catch (err) {
logError(`passkeysRegister failed: ${err}`);
Expand All @@ -635,7 +635,7 @@ keepass.passkeysGet = async function(tab, args = []) {
try {
const taResponse = await keepass.testAssociation(tab, [ false ]);
if (!taResponse || !keepass.isConnected || args.length < 2) {
browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
}

Expand All @@ -656,7 +656,7 @@ keepass.passkeysGet = async function(tab, args = []) {
return response;
}

browserAction.showDefault(tab);
browserAction.updatePopup(tab);
return [];
} catch (err) {
logError(`passkeysGet failed: ${err}`);
Expand Down Expand Up @@ -851,7 +851,11 @@ keepass.checkDatabaseHash = async function(tab) {
};

keepass.isAssociated = function() {
return (keepass.associated.value && keepass.associated.hash && keepass.associated.hash === keepass.databaseHash);
if (!keepass.associated.value || keepass.associated.hash === '') {
return false;
}

return keepass.associated.hash === keepass.databaseHash;
};

keepass.setcurrentKeePassXCVersion = function(version) {
Expand Down Expand Up @@ -910,7 +914,7 @@ keepass.handleError = function(tab, errorCode, errorMessage = '') {

keepass.updatePopup = function() {
if (page && page.tabs.length > 0) {
browserAction.showDefault();
browserAction.updatePopup();
}
};

Expand Down
Loading