private PageDetails.Field FindUsernameField(PageDetails pageDetails, PageDetails.Field passwordField, bool canBeHidden, bool checkForm) { PageDetails.Field usernameField = null; foreach (var f in pageDetails.Fields) { if (f.ElementNumber >= passwordField.ElementNumber) { break; } if ((!checkForm || f.Form == passwordField.Form) && (canBeHidden || f.Viewable) && f.ElementNumber < passwordField.ElementNumber && (f.Type == "text" || f.Type == "email" || f.Type == "tel")) { usernameField = f; if (FindMatchingFieldIndex(f, _usernameFieldNames) > -1) { // We found an exact match. No need to keep looking. break; } } } return(usernameField); }
private PageDetails.Field FindUsernameField(PageDetails pageDetails, PageDetails.Field passwordField, bool canBeHidden, bool checkForm) { return(pageDetails.Fields.LastOrDefault(f => (!checkForm || f.Form == passwordField.Form) && (canBeHidden || f.Viewable) && f.ElementNumber < passwordField.ElementNumber && (f.Type == "text" || f.Type == "email" || f.Type == "tel"))); }
public FillScript(PageDetails pageDetails, string fillUsername, string fillPassword) { if (pageDetails == null) { return; } 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" && f.Viewable).ToArray(); if (!passwordFields.Any()) { // not able to find any viewable password fields. maybe there are some "hidden" ones? passwordFields = pageDetails.Fields.Where(f => f.Type == "password").ToArray(); } foreach (var form in pageDetails.Forms) { var passwordFieldsForForm = passwordFields.Where(f => f.Form == form.Key).ToArray(); passwords.AddRange(passwordFieldsForForm); if (string.IsNullOrWhiteSpace(fillUsername)) { continue; } foreach (var pf in passwordFieldsForForm) { var username = FindUsernameField(pageDetails, pf, false, true); if (username == null) { // not able to find any viewable username fields. maybe there are some "hidden" ones? username = FindUsernameField(pageDetails, pf, true, true); } if (username != null) { usernames.Add(username); } } } if (passwordFields.Any() && !passwords.Any()) { // 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. var pf = passwordFields.First(); passwords.Add(pf); if (!string.IsNullOrWhiteSpace(fillUsername) && pf.ElementNumber > 0) { var username = FindUsernameField(pageDetails, pf, false, false); if (username == null) { // not able to find any viewable username fields. maybe there are some "hidden" ones? username = FindUsernameField(pageDetails, pf, true, false); } 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 }); } 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 }; } }
public FillScript(PageDetails pageDetails, string fillUsername, string fillPassword, List <Tuple <string, string> > fillFields) { if (pageDetails == null) { return; } DocumentUUID = pageDetails.DocumentUUID; var filledFields = new Dictionary <string, PageDetails.Field>(); if (fillFields?.Any() ?? false) { var fieldNames = fillFields.Select(f => f.Item1?.ToLower()).ToArray(); foreach (var field in pageDetails.Fields.Where(f => f.Viewable)) { if (filledFields.ContainsKey(field.OpId)) { continue; } var matchingIndex = FindMatchingFieldIndex(field, fieldNames); if (matchingIndex > -1) { filledFields.Add(field.OpId, field); Script.Add(new List <string> { "click_on_opid", field.OpId }); Script.Add(new List <string> { "fill_by_opid", field.OpId, fillFields[matchingIndex].Item2 }); } } } if (string.IsNullOrWhiteSpace(fillPassword)) { // No password for this login. Maybe they just wanted to auto-fill some custom fields? SetFillScriptForFocus(filledFields); return; } 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" && f.Viewable).ToArray(); if (!passwordFields.Any()) { // not able to find any viewable password fields. maybe there are some "hidden" ones? passwordFields = pageDetails.Fields.Where(f => f.Type == "password").ToArray(); } foreach (var form in pageDetails.Forms) { var passwordFieldsForForm = passwordFields.Where(f => f.Form == form.Key).ToArray(); passwords.AddRange(passwordFieldsForForm); if (string.IsNullOrWhiteSpace(fillUsername)) { continue; } foreach (var pf in passwordFieldsForForm) { var username = FindUsernameField(pageDetails, pf, false, true); if (username == null) { // not able to find any viewable username fields. maybe there are some "hidden" ones? username = FindUsernameField(pageDetails, pf, true, true); } if (username != null) { usernames.Add(username); } } } if (passwordFields.Any() && !passwords.Any()) { // 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. var pf = passwordFields.First(); passwords.Add(pf); if (!string.IsNullOrWhiteSpace(fillUsername) && pf.ElementNumber > 0) { var username = FindUsernameField(pageDetails, pf, false, false); if (username == null) { // not able to find any viewable username fields. maybe there are some "hidden" ones? username = FindUsernameField(pageDetails, pf, true, false); } if (username != null) { usernames.Add(username); } } } if (!passwordFields.Any()) { // No password fields on this page. Let's try to just fuzzy fill the username. var usernameFieldNamesList = _usernameFieldNames.ToList(); foreach (var f in pageDetails.Fields) { if (f.Viewable && (f.Type == "text" || f.Type == "email" || f.Type == "tel") && FieldIsFuzzyMatch(f, usernameFieldNamesList)) { usernames.Add(f); } } } foreach (var username in usernames.Where(u => !filledFields.ContainsKey(u.OpId))) { filledFields.Add(username.OpId, username); Script.Add(new List <string> { "click_on_opid", username.OpId }); Script.Add(new List <string> { "fill_by_opid", username.OpId, fillUsername }); } foreach (var password in passwords.Where(p => !filledFields.ContainsKey(p.OpId))) { filledFields.Add(password.OpId, password); Script.Add(new List <string> { "click_on_opid", password.OpId }); Script.Add(new List <string> { "fill_by_opid", password.OpId, fillPassword }); } SetFillScriptForFocus(filledFields); }
public FillScript(PageDetails pageDetails, string fillUsername, string fillPassword) { if (pageDetails == null) { return; } 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(); foreach (var form in pageDetails.Forms) { var passwordFieldsForForm = passwordFields.Where(f => f.Form == form.Key).ToArray(); passwords.AddRange(passwordFieldsForForm); if (string.IsNullOrWhiteSpace(fillUsername)) { continue; } foreach (var pf in passwordFieldsForForm) { 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) { usernames.Add(username); } } } if (passwordFields.Any() && !passwords.Any()) { // 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. var pf = passwordFields.First(); passwords.Add(pf); if (!string.IsNullOrWhiteSpace(fillUsername) && pf.ElementNumber > 0) { 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 }); } 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 }; } }