Skip to content

Commit e6831a3

Browse files
authored
Merge pull request #2531 from keepassxreboot/fix/better_submit_button_identification
Improve submit button detection
2 parents 003feb7 + a2d35a2 commit e6831a3

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

keepassxc-browser/common/sites.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ kpxcSites.formSubmitButtonExceptionFound = function(form) {
212212
return $('.button[slot=primary-button]');
213213
} else if (form?.action === 'https://auth.openai.com/log-in/password') {
214214
return form.querySelector('button[class*=_primary_]');
215+
} else if (!form && document.location.origin === 'https://www.reddit.com') {
216+
return $('button.login');
215217
}
216218

217219
return undefined;

keepassxc-browser/content/form.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ kpxcForm.savedCustomInputs = [];
1010
kpxcForm.savedForms = [];
1111
kpxcForm.submitTriggered = false;
1212

13-
// Activate the Credential Banner if existing credentials are not found
13+
// Activate the Credential Banner if credentials are found from form submit
1414
kpxcForm.activateCredentialBanner = async function(usernameValue, passwordInputs, passwordField) {
1515
let passwordValue = '';
1616
// Check if the form has three password fields -> a possible password change form
@@ -163,7 +163,7 @@ kpxcForm.initForm = function(form, credentialFields) {
163163
form.addEventListener('submit', kpxcForm.onSubmit);
164164

165165
const submitButton = kpxcForm.getFormSubmitButton(form);
166-
if (submitButton !== undefined) {
166+
if (submitButton) {
167167
submitButton.addEventListener('click', kpxcForm.onSubmit);
168168
}
169169
}
@@ -181,6 +181,15 @@ kpxcForm.initCustomForm = function(combinations) {
181181
}
182182
};
183183

184+
// Identifies a submit button from the page outside any form
185+
kpxcForm.initSubmitButtonFromPage = function() {
186+
let submitButton = kpxcSites.formSubmitButtonExceptionFound();
187+
submitButton ??= $('button[type=submit], button.login');
188+
if (submitButton) {
189+
submitButton.addEventListener('click', kpxcForm.onSubmit);
190+
}
191+
};
192+
184193
// Triggers when a custom form has been identified with a specific form submit button
185194
kpxcForm.onCustomFormSubmit = async function(e) {
186195
if (!e.isTrusted || kpxcForm.savedCustomInputs?.length === 0) {
@@ -227,14 +236,25 @@ kpxcForm.onSubmit = async function(e) {
227236
form = kpxcForm.savedForms[0].form;
228237
}
229238

230-
if (!form) {
239+
// Try choosing inputs from the last combination detected.
240+
// Needed if initSubmitButtonFromPage() has been used.
241+
let usernameField;
242+
let passwordField;
243+
let passwordInputs = [];
244+
if (!form && kpxc.combinations.length > 0) {
245+
usernameField = kpxc.combinations.at(-1)?.username;
246+
passwordField = kpxc.combinations.at(-1)?.password;
247+
passwordInputs = kpxc.combinations.at(-1)?.passwordInputs;
248+
} else {
249+
[ usernameField, passwordField, passwordInputs ] = kpxcForm.getCredentialFieldsFromForm(form);
250+
}
251+
252+
if (!form && !usernameField && !passwordField) {
231253
logDebug('Error: No form found for submit detection.');
232254
kpxcForm.submitTriggered = false;
233255
return;
234256
}
235257

236-
const [ usernameField, passwordField, passwordInputs ] = kpxcForm.getCredentialFieldsFromForm(form);
237-
238258
// Use the first text field in the form if only username input is missing
239259
const usernameValue = await kpxcForm.getUsernameValue(!usernameField && passwordField
240260
? form?.querySelector('input[type=text]')

keepassxc-browser/content/keepassxc-browser.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,14 @@ kpxc.initCombinations = async function(inputs = []) {
321321
for (const c of combinations) {
322322
// If no username field is found, handle the single password field as such
323323
const field = c.username || c.password;
324-
if (field && c.form) {
325-
// Initialize form-submit for remembering credentials
326-
kpxcForm.initForm(c.form, c);
324+
if (field) {
325+
if (c.form) {
326+
// Initialize form-submit for remembering credentials
327+
kpxcForm.initForm(c.form, c);
328+
} else {
329+
// Try to search a submit button
330+
kpxcForm.initSubmitButtonFromPage();
331+
}
327332
}
328333

329334
// Don't allow duplicates

0 commit comments

Comments
 (0)