From 634a8702cda933607ef0a66d72d454ec8a43b823 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 29 Sep 2017 15:12:33 -0400 Subject: [PATCH] autofill adjustments --- src/iOS.Extension/Models/FillScript.cs | 57 ++++++++++++++++++-------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/iOS.Extension/Models/FillScript.cs b/src/iOS.Extension/Models/FillScript.cs index 7c5c916e0..0364d14dd 100644 --- a/src/iOS.Extension/Models/FillScript.cs +++ b/src/iOS.Extension/Models/FillScript.cs @@ -21,14 +21,14 @@ namespace Bit.iOS.Extension.Models DocumentUUID = pageDetails.DocumentUUID; - var filledOpIds = new HashSet(); + var filledFields = new Dictionary(); if(fillFields?.Any() ?? false) { var fieldNames = fillFields.Select(f => f.Item1?.ToLower()).ToArray(); foreach(var field in pageDetails.Fields.Where(f => f.Viewable)) { - if(filledOpIds.Contains(field.OpId)) + if(filledFields.ContainsKey(field.OpId)) { continue; } @@ -36,7 +36,7 @@ namespace Bit.iOS.Extension.Models var matchingIndex = FindMatchingFieldIndex(field, fieldNames); if(matchingIndex > -1) { - filledOpIds.Add(field.OpId); + filledFields.Add(field.OpId, field); Script.Add(new List { "click_on_opid", field.OpId }); Script.Add(new List { "fill_by_opid", field.OpId, fillFields[matchingIndex].Item2 }); } @@ -46,10 +46,7 @@ namespace Bit.iOS.Extension.Models if(string.IsNullOrWhiteSpace(fillPassword)) { // No password for this login. Maybe they just wanted to auto-fill some custom fields? - if(filledOpIds.Any()) - { - Script.Add(new List { "focus_by_opid", filledOpIds.Last() }); - } + SetFillScriptForFocus(filledFields); return; } @@ -119,7 +116,7 @@ namespace Bit.iOS.Extension.Models var usernameFieldNamesList = _usernameFieldNames.ToList(); foreach(var f in pageDetails.Fields) { - if((f.Type == "text" || f.Type == "email" || f.Type == "tel") && + if(f.Viewable && (f.Type == "text" || f.Type == "email" || f.Type == "tel") && FieldIsFuzzyMatch(f, usernameFieldNamesList)) { usernames.Add(f); @@ -127,24 +124,21 @@ namespace Bit.iOS.Extension.Models } } - foreach(var username in usernames.Where(u => !filledOpIds.Contains(u.OpId))) + foreach(var username in usernames.Where(u => !filledFields.ContainsKey(u.OpId))) { - filledOpIds.Add(username.OpId); + filledFields.Add(username.OpId, username); Script.Add(new List { "click_on_opid", username.OpId }); Script.Add(new List { "fill_by_opid", username.OpId, fillUsername }); } - foreach(var password in passwords.Where(p => !filledOpIds.Contains(p.OpId))) + foreach(var password in passwords.Where(p => !filledFields.ContainsKey(p.OpId))) { - filledOpIds.Add(password.OpId); + filledFields.Add(password.OpId, password); Script.Add(new List { "click_on_opid", password.OpId }); Script.Add(new List { "fill_by_opid", password.OpId, fillPassword }); } - if(filledOpIds.Any()) - { - Script.Add(new List { "focus_by_opid", filledOpIds.Last() }); - } + SetFillScriptForFocus(filledFields); } private PageDetails.Field FindUsernameField(PageDetails pageDetails, PageDetails.Field passwordField, bool canBeHidden, @@ -232,6 +226,37 @@ namespace Bit.iOS.Extension.Models return options.Any(o => value.Contains(o)); } + private void SetFillScriptForFocus(IDictionary filledFields) + { + if(!filledFields.Any()) + { + return; + } + + PageDetails.Field lastField = null, lastPasswordField = null; + foreach(var field in filledFields) + { + if(field.Value.Viewable) + { + lastField = field.Value; + if(field.Value.Type == "password") + { + lastPasswordField = field.Value; + } + } + } + + // Prioritize password field over others. + if(lastPasswordField != null) + { + Script.Add(new List { "focus_by_opid", lastPasswordField.OpId }); + } + else if(lastField != null) + { + Script.Add(new List { "focus_by_opid", lastField.OpId }); + } + } + private string CleanLabel(string label) { return Regex.Replace(label, @"(?:\r\n|\r|\n)", string.Empty).Trim().ToLower();