我遇到了chrome自动填充行为的几个形式的问题。
表单中的字段都有非常常见和准确的名称,例如“email”、“name”或“password”,并且它们还设置了autocomplete=“off”。
自动完成标志已经成功禁用了自动完成行为,当你开始输入时,会出现一个下拉的值,但没有改变Chrome自动填充字段的值。
这种行为是可以的,除了chrome填充输入不正确,例如填充电话输入与电子邮件地址。客户抱怨过这个问题,所以它被证实在很多情况下都发生了,而不是我在我的机器上本地操作的某种结果。
目前我能想到的唯一解决方案是动态生成自定义输入名称,然后在后端提取值,但这似乎是一种相当笨拙的解决这个问题的方法。是否有任何标签或怪癖,改变自动填充行为,可以用来解决这个问题?
1月20日chrome更新
上面的解决方案都不起作用,包括添加假字段或设置autocomplete=new-password。是的,这些chrome浏览器不会自动完成,但当你输入密码字段时,它会再次建议密码。
我发现,如果你删除密码字段,然后添加它再次没有id,然后chrome不会自动填充它,也不建议输入密码。
使用类来获取密码值而不是id。
对于firefox来说,仍然需要添加一个虚拟元素。
这个解决方案还允许/禁止基于标志的自动补全:
html:
<!-- form is initially hidden, password field has a dummy id and a class 'id' -->
<form id="loginForm" style="display:none;">
<span>email:</span><input type="text" placeholder="Email" id="loginEmailInputID"/>
<span>Password:</span><input class="loginPasswordInput" type="password" placeholder="Password" id="justForAutocomplete"/>
</form>
页面加载:
function hideAutoComplete(formId) {
let passwordElem=$('#'+formId+' input:password'), prev=passwordElem.prev();
passwordElem.remove();
prev.after($('<input type="password" style="display:none">'), passwordElem.clone().val('').removeAttr('id'));
}
if (!allowAutoComplete) hideAutoComplete('loginForm');
$('#loginForm').show();
当你需要密码时:
$('.loginPasswordInput').val();
Boom,谷歌Chrome和其他人尝试击败这然后。
我能够得到这个实现今天2017年9月7日,但使用在我的MVC视图中生成的随机字符串的FormCollection。
我会首先在我的登录页面的索引控制器中获得一个新的随机密钥,加密并创建一个新的完全唯一的随机字符串(我实际上使用了一个256位的cypher来执行此操作,以及一个唯一的cypher和认证密钥),然后在每个字符串的末尾附加纯文本“用户名”和“密码”,以帮助我从响应视图控制器识别用户名和密码。你也可以把这个普通字符串改成任何东西,只要你知道它的模式并且它是唯一的。
在我看来,我然后适当地使用这些变量,然后在响应控制器中—通过FormCollection搜索并找到匹配的变量—然后使用该项的Key-Value作为相应的用户名和密码进行适当的处理。
The other issue i had which i think is sneaky of Chrome, is that any
Any thoughts ?
<style>
@@font-face {
font-family: 'password';
font-style: normal;
font-weight: 400;
src: url(https://jsbin-user-assets.s3.amazonaws.com/rafaelcastrocouto/password.ttf);
}
</style>
<input type="text" name="@Model.UsernameRandomNameString" />
<input style="font-family: 'password'" type="text" name="@Model.PasswordRandomNameString" />
LogOnModel model = new LogOnModel()
{
UsernameRandomNameString = Cryptography.SimpleEncrypt("Username", UniqueGeneratorKey, UniqueGeneratorKey) + "Username",
PasswordRandomNameString = Cryptography.SimpleEncrypt("Password", UniqueGeneratorKey, UniqueGeneratorKey) + "Password",
};
I think it a hell of a workaround, but hey it works, and i think it could also be future proof unless google determines the URL of the page has key words in it, then appropriately just adds its stupid extensions on top on any input field - but that's a little drastic.
我的黑客测试在Chrome 48:
由于Chrome试图找出字段的类型,它是通过查看诸如id或名称属性的<输入>,但也在相关的<标签>内容,你只需要找到这些无意义的名字。
对于id和名称,很容易选择这里没有列出的其他内容。
对于标签,我在中间插入了一个不可见的<span>,例如对于一个城市(它会打乱我的位置自动完成):
<span>Ci<span style="display:none">*</span>ty</span>
完整的工作示例:
<!DOCTYPE html>
<html>
<body>
<form method="post" action="/register">
<div>
<label for="name">Name</label>
<input id="name" type="text" name="name" />
</div>
<div>
<label for="email">Email</label>
<input id="email" type="text" name="email" />
</div>
<div>
<label for="id1">City</label>
<input id="id1" type="text" name="id1" /> <-- STILL ON :(
</div>
<div>
<label for="id2">Ci<span style="display:none">*</span>ty</label>
<input id="id2" type="text" name="id2" /> <-- NOW OFF :)
</div>
</form>
</body>
</html>