如何以编程方式在Chrome中隐藏密码建议列表?

时间:2019-01-23 08:08:31

标签: javascript jquery google-chrome

当用户单击密码字段时,保存的密码将在字段下方列出,如下所示。有什么可能的方法可以隐藏此列表?

enter image description here

注意:我尝试了各种方法,例如设置输入字段属性autocomplete="new-password"和添加一个假密码字段。但是仍然没有运气。 :(

1 个答案:

答案 0 :(得分:0)

我找到了解决此问题的方法,那就是使用JQuery进行密码屏蔽。

提示:仅当输入框的类型为密码时,才会提示密码和填写表格。

所以我要解决的方法如下,

01)。将输入字段设置为“文本”

<input type="text" />

02)。用'•'

掩盖输入字符

var actualPassword = [],
    temperoryPassword,
    currentCursorPosition = 0,
    passwordChar = '•';

$("#password-text").bind("input", function () {
  temperoryPassword = $(this).val();
  var passwordLength = temperoryPassword.length;

  for (var i = 0; i < passwordLength; i++) {
    if (temperoryPassword[i] != passwordChar) {
      actualPassword[i] = temperoryPassword[i];
    }
  }
  
  // Get current cursor position.
  currentCursorPosition = this.selectionStart;
  $(this).val(temperoryPassword.replace(/./g, passwordChar));
});

$("#password-text").bind("keyup", function () {
  var passwordLength = $(this).val().length;

  if (passwordLength < actualPassword.length) {

    var difference = actualPassword.length - passwordLength,
		key = event.keyCode || event.charCode;

    // Check if last keypress was backspace or delete
    if (key == 8 || key == 46) {
      actualPassword.splice(currentCursorPosition, difference);
    }
    // User highlighted and overwrite part of the password
    else {
      actualPassword.splice(currentCursorPosition - 1, difference + 1);
      actualPassword.splice(currentCursorPosition - 1, 0, temperoryPassword[currentCursorPosition - 1]);
    }
  }
});

$("#btnSubmit").click(function(){
    $("#passwordTxt").text(bindactualPassword(actualPassword));
});

// disable password cut, copy and paste function in password field 
 $('#password-text').bind("cut copy paste",function(e) {
     e.preventDefault();
 });

function bindactualPassword(){
   return actualPassword.join(""); 
}

我就此案写了一篇中篇文章。您也可以在这里阅读

https://medium.com/@shamique/programatically-prevent-password-suggestion-and-auto-fill-in-browsers-6661537a3e46

希望这对其他人有用:)