1
0
mirror of https://github.com/bitwarden/browser synced 2024-12-26 09:54:35 +01:00

autofill improvements to fallback when no "viewable" password/username fields are detected.

This commit is contained in:
Kyle Spearrin 2016-11-26 12:06:39 -05:00
parent 7900596380
commit 3e2663e9d4

View File

@ -21,18 +21,38 @@ function initAutofill() {
passwords = [],
usernames = [],
pf = null,
f = null,
username = null;
for (var i = 0; i < pageDetails.fields.length; i++) {
if (pageDetails.fields[i].type === 'password' && pageDetails.fields[i].viewable) {
passwordFields.push(pageDetails.fields[i]);
function loadPasswordFields(canBeHidden) {
for (var i = 0; i < pageDetails.fields.length; i++) {
if (pageDetails.fields[i].type === 'password' && (canBeHidden || pageDetails.fields[i].viewable)) {
passwordFields.push(pageDetails.fields[i]);
}
}
}
loadPasswordFields(false);
if (!passwordFields.length) {
// not able to find any visible password fields. maybe there are some "hidden" ones?
loadPasswordFields(true);
}
function findUsernameField(passwordField, canBeHidden) {
for (var i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
if (f.form === passwordField.form && (canBeHidden || f.viewable)
&& (f.type === 'text' || f.type === 'email' || f.type === 'tel')
&& f.elementNumber < passwordField.elementNumber) {
return f;
}
}
return null;
}
for (var formKey in pageDetails.forms) {
var passwordFieldsForForm = [];
for (i = 0; i < passwordFields.length; i++) {
for (var i = 0; i < passwordFields.length; i++) {
if (formKey === passwordFields[i].form) {
passwordFieldsForForm.push(passwordFields[i]);
}
@ -43,12 +63,11 @@ function initAutofill() {
passwords.push(pf);
if (fillUsername) {
for (var j = 0; j < pageDetails.fields.length; j++) {
f = pageDetails.fields[j];
if (f.form === pf.form && f.viewable && (f.type === 'text' || f.type === 'email' || f.type === 'tel')
&& f.elementNumber < pf.elementNumber) {
username = f;
}
username = findUsernameField(pf, false);
if (!username) {
// not able to find any visible username fields. maybe there are some "hidden" ones?
username = findUsernameField(pf, true);
}
if (username) {
@ -58,6 +77,22 @@ function initAutofill() {
}
}
function findUsernameFieldWithoutForm(passwordField, canBeHidden) {
var usernameField = null;
for (var i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
if (f.elementNumber > passwordField.elementNumber) {
break;
}
if ((canBeHidden || f.viewable) && (f.type === 'text' || f.type === 'email' || f.type === 'tel')) {
usernameField = f;
}
}
return usernameField;
}
if (passwordFields.length && !passwords.length) {
// The page does not have any forms with password fields. Use the first password field on the page and the
// input field just before it as the username.
@ -66,16 +101,10 @@ function initAutofill() {
passwords.push(pf);
if (fillUsername && pf.elementNumber > 0) {
username = null;
for (i = 0; i < pageDetails.fields.length; i++) {
f = pageDetails.fields[i];
if (f.elementNumber > pf.elementNumber) {
break;
}
username = findUsernameFieldWithoutForm(pf, false);
if (f.viewable && (f.type === 'text' || f.type === 'email' || f.type === 'tel')) {
username = f;
}
if (!username) {
username = findUsernameFieldWithoutForm(pf, true);
}
if (username) {