1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-08 09:32:07 +01:00

handle autofill for pages with no forms, but still has fields

This commit is contained in:
Kyle Spearrin 2016-09-17 09:35:54 -04:00
parent 3bd24a15fc
commit 790d705d40

View File

@ -17,14 +17,15 @@ function initAutofill() {
metadata: {}
};
var passwordFields = [];
var passwordFields = [],
passwordForms = [];
for (var i = 0; i < pageDetails.fields.length; i++) {
if (pageDetails.fields[i].type === 'password') {
passwordFields.push(pageDetails.fields[i]);
}
}
var passwordForms = [];
for (var formKey in pageDetails.forms) {
for (var j = 0; j < passwordFields.length; j++) {
if (formKey === passwordFields[j].form) {
@ -34,69 +35,78 @@ function initAutofill() {
}
}
if (!passwordForms.length) {
return null;
}
var password = null,
username = null,
loginForm = null;
var loginForm = null;
if (passwordForms.length > 1) {
// More than one form with a password field is on the page.
// This usually occurs when a website has a login and signup form on the same page.
// Let's try to guess which one is the login form.
if (passwordForms.length) {
if (passwordForms.length > 1) {
// More than one form with a password field is on the page.
// This usually occurs when a website has a login and signup form on the same page.
// Let's try to guess which one is the login form.
// First let's try to guess the correct login form by examining the form attribute strings
// for common login form attribute.
for (i = 0; i < passwordForms.length; i++) {
var formDescriptor = (passwordForms[i].htmlName + '~' + passwordForms[i].htmlId +
'~' + passwordForms[i].htmlAction).toLowerCase();
if (formDescriptor.indexOf('login') !== -1 || formDescriptor.indexOf('log-in') !== -1 ||
formDescriptor.indexOf('signin') !== -1 || formDescriptor.indexOf('sign-in') !== -1 ||
formDescriptor.indexOf('logon') !== -1 || formDescriptor.indexOf('log-on') !== -1) {
loginForm = passwordForms[i];
break;
}
}
if (!loginForm) {
// Next we can try to find the login form that only has one password field. Typically
// a registration form may have two password fields for password confirmation.
// First let's try to guess the correct login form by examining the form attribute strings
// for common login form attribute.
for (i = 0; i < passwordForms.length; i++) {
var passwordFieldCount = 0;
var formDescriptor = (passwordForms[i].htmlName + '~' + passwordForms[i].htmlId +
'~' + passwordForms[i].htmlAction).toLowerCase();
for (j = 0; j < passwordFields.length; j++) {
if (passwordForms[i].opid === passwordFields[j].form) {
passwordFieldCount++;
}
}
if (passwordFieldCount === 1) {
if (formDescriptor.indexOf('login') !== -1 || formDescriptor.indexOf('log-in') !== -1 ||
formDescriptor.indexOf('signin') !== -1 || formDescriptor.indexOf('sign-in') !== -1 ||
formDescriptor.indexOf('logon') !== -1 || formDescriptor.indexOf('log-on') !== -1) {
loginForm = passwordForms[i];
break;
}
}
if (!loginForm) {
// Next we can try to find the login form that only has one password field. Typically
// a registration form may have two password fields for password confirmation.
for (i = 0; i < passwordForms.length; i++) {
var passwordFieldCount = 0;
for (j = 0; j < passwordFields.length; j++) {
if (passwordForms[i].opid === passwordFields[j].form) {
passwordFieldCount++;
}
}
if (passwordFieldCount === 1) {
loginForm = passwordForms[i];
break;
}
}
}
}
if (!loginForm) {
loginForm = passwordForms[0];
}
for (i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
if (f.form === loginForm.opid && f.type === 'password') {
password = f;
break;
}
}
for (i = 0; i < pageDetails.fields.length; i++) {
f = pageDetails.fields[i];
if (f.form === loginForm.opid && (f.type === 'text' || f.type === 'email')
&& f.elementNumber < password.elementNumber) {
username = f;
}
}
if (loginForm.htmlAction) {
fillScript.autosubmit = { focusOpid: password.opid };
}
}
if (!loginForm) {
loginForm = passwordForms[0];
}
var password = null;
for (i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
if (f.form === loginForm.opid && f.type === 'password') {
password = f;
break;
}
}
var username = null;
for (i = 0; i < pageDetails.fields.length; i++) {
f = pageDetails.fields[i];
if (f.form === loginForm.opid && (f.type === 'text' || f.type === 'email')
&& f.elementNumber < password.elementNumber) {
username = f;
else if (passwordFields.length == 1) {
password = passwordFields[0];
if (password.elementNumber > 0) {
username = pageDetails.fields[password.elementNumber - 1];
}
}
@ -105,11 +115,9 @@ function initAutofill() {
fillScript.script.push(['fill_by_opid', username.opid, fillUsername]);
}
fillScript.script.push(['click_on_opid', password.opid]);
fillScript.script.push(['fill_by_opid', password.opid, fillPassword]);
if (loginForm.htmlAction) {
fillScript.autosubmit = { focusOpid: password.opid };
if (password) {
fillScript.script.push(['click_on_opid', password.opid]);
fillScript.script.push(['fill_by_opid', password.opid, fillPassword]);
}
return fillScript;