mirror of
https://github.com/bitwarden/android.git
synced 2024-12-21 00:31:58 +03:00
updated fill script to support multiple forms on the same page and no form at all (only fields)
This commit is contained in:
parent
23274ef914
commit
b60be97b1f
1 changed files with 35 additions and 74 deletions
|
@ -16,109 +16,70 @@ namespace Bit.iOS.Extension.Models
|
|||
|
||||
DocumentUUID = pageDetails.DocumentUUID;
|
||||
|
||||
List<PageDetails.Field> usernames = new List<PageDetails.Field>();
|
||||
List<PageDetails.Field> passwords = new List<PageDetails.Field>();
|
||||
|
||||
var passwordFields = pageDetails.Fields.Where(f => f.Type == "password").ToArray();
|
||||
var passwordForms = pageDetails.Forms.Where(form => passwordFields.Any(f => f.Form == form.Key)).ToArray();
|
||||
|
||||
PageDetails.Form loginForm = null;
|
||||
PageDetails.Field username = null, password = null;
|
||||
|
||||
if(passwordForms.Any())
|
||||
foreach(var form in pageDetails.Forms)
|
||||
{
|
||||
if(passwordForms.Count() > 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.
|
||||
var passwordFieldsForForm = passwordFields.Where(f => f.Form == form.Key).ToArray();
|
||||
passwords.AddRange(passwordFieldsForForm);
|
||||
|
||||
// First let's try to guess the correct login form by examining the form attribute strings
|
||||
// for common login form attribute.
|
||||
foreach(var form in passwordForms)
|
||||
if(string.IsNullOrWhiteSpace(fillUsername))
|
||||
{
|
||||
var formDescriptor = string.Format("{0}~{1}~{2}",
|
||||
form.Value?.HtmlName, form.Value?.HtmlId, form.Value?.HtmlAction)
|
||||
?.ToLowerInvariant()?.Replace('_', '-');
|
||||
|
||||
if(formDescriptor.Contains("login") || formDescriptor.Contains("log-in")
|
||||
|| formDescriptor.Contains("signin") || formDescriptor.Contains("sign-in")
|
||||
|| formDescriptor.Contains("logon") || formDescriptor.Contains("log-on"))
|
||||
{
|
||||
loginForm = form.Value;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if(loginForm == null)
|
||||
foreach(var pf in passwordFieldsForForm)
|
||||
{
|
||||
// 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.
|
||||
var fieldGroups = passwordFields.GroupBy(f => f.Form);
|
||||
var singleFields = fieldGroups.FirstOrDefault(f => f.Count() == 1);
|
||||
if(singleFields.Any())
|
||||
var username = pageDetails.Fields.LastOrDefault(f => f.Form == pf.Form
|
||||
&& f.ElementNumber < pf.ElementNumber && (f.Type == "text" || f.Type == "email" || f.Type == "tel"));
|
||||
if(username != null)
|
||||
{
|
||||
var singlePasswordForms = passwordForms.Where(f => f.Key == singleFields.Key);
|
||||
if(singlePasswordForms.Any())
|
||||
{
|
||||
loginForm = singlePasswordForms.First().Value;
|
||||
}
|
||||
usernames.Add(username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(loginForm == null)
|
||||
if(passwordFields.Any() && !passwords.Any())
|
||||
{
|
||||
loginForm = passwordForms.FirstOrDefault().Value;
|
||||
}
|
||||
|
||||
password = pageDetails.Fields.FirstOrDefault(f =>
|
||||
f.Form == loginForm.OpId
|
||||
&& f.Type == "password");
|
||||
|
||||
username = pageDetails.Fields.LastOrDefault(f =>
|
||||
f.Form == loginForm.OpId
|
||||
&& (f.Type == "text" || f.Type == "email")
|
||||
&& f.ElementNumber < password.ElementNumber);
|
||||
|
||||
if(loginForm.HtmlAction != null)
|
||||
{
|
||||
AutoSubmit = new Submit { FocusOpId = password.OpId };
|
||||
}
|
||||
}
|
||||
else if(passwordFields.Count() == 1)
|
||||
{
|
||||
// The page does not have any forms with password fields. Use the one 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.
|
||||
|
||||
password = passwordFields.First();
|
||||
if(password.ElementNumber > 0)
|
||||
var pf = passwordFields.First();
|
||||
passwords.Add(pf);
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(fillUsername) && pf.ElementNumber > 0)
|
||||
{
|
||||
username = pageDetails.Fields.LastOrDefault(f =>
|
||||
(f.Type == "text" || f.Type == "email")
|
||||
&& f.ElementNumber < password.ElementNumber);
|
||||
|
||||
if(username == null)
|
||||
{
|
||||
username = pageDetails.Fields[password.ElementNumber - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Script = new List<List<string>>();
|
||||
|
||||
var username = pageDetails.Fields.LastOrDefault(f => f.ElementNumber < pf.ElementNumber
|
||||
&& (f.Type == "text" || f.Type == "email" || f.Type == "tel"));
|
||||
if(username != null)
|
||||
{
|
||||
usernames.Add(username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach(var username in usernames)
|
||||
{
|
||||
Script.Add(new List<string> { "click_on_opid", username.OpId });
|
||||
Script.Add(new List<string> { "fill_by_opid", username.OpId, fillUsername });
|
||||
}
|
||||
|
||||
if(password != null)
|
||||
foreach(var password in passwords)
|
||||
{
|
||||
Script.Add(new List<string> { "click_on_opid", password.OpId });
|
||||
Script.Add(new List<string> { "fill_by_opid", password.OpId, fillPassword });
|
||||
}
|
||||
|
||||
if(passwords.Any())
|
||||
{
|
||||
AutoSubmit = new Submit { FocusOpId = passwords.First().OpId };
|
||||
}
|
||||
}
|
||||
|
||||
[JsonProperty(PropertyName = "script")]
|
||||
public List<List<string>> Script { get; set; }
|
||||
public List<List<string>> Script { get; set; } = new List<List<string>>();
|
||||
[JsonProperty(PropertyName = "autosubmit")]
|
||||
public Submit AutoSubmit { get; set; }
|
||||
[JsonProperty(PropertyName = "documentUUID")]
|
||||
|
|
Loading…
Reference in a new issue