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
1 changed files with 49 additions and 20 deletions

View File

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