gpt4 book ai didi

javascript - 输入类型=密码值返回空,除非我在 chrome 控制台中使用断点

转载 作者:行者123 更新时间:2023-11-30 16:54:24 27 4
gpt4 key购买 nike

我试图在 window.onload 上获取密码输入框的值,但它一直返回空字符串。如果我设置一个断点并单步执行我的代码,那么它会返回正确的字符串。如果我在它之前的任何地方设置一个断点,然后继续运行代码,它也会起作用。但是在某处没有断点的情况下运行它在某个点停止代码,甚至使用最长的 setTimeout() 似乎都不起作用。

一点背景知识:我正在编写一个解决方案来摆脱我设计的 chrome 自动完成黄色背景。我找到了一个很好的解决方案,但它删除了密码值 Solution Link .所以我决定先获取密码值,然后在一切完成后重置它。这是我的代码:

window.onload = function() {
setTimeout(function() {
var documentForms = document.forms;

//First cycle through all the forms
for (var i = 0; i < documentForms.length; i++) {
var password = null;

// Now find the password and store its value
for (var k = 0; k < documentForms[i].elements.length; k++) {
var passwordInput = documentForms[i].elements[k];
if (passwordInput.type == "password") {
password = passwordInput.value; //This keeps returning ""
}
}

// Now using this solution remove the autocomplete yellow:
for (var j = 0; j < documentForms[i].elements.length; j++) {
var input = documentForms[i].elements[j];
if (input.type == "text" || input.type == "password" || input.type == null) {
var text = input.value;
input.focus();
var event = document.createEvent('TextEvent');
event.initTextEvent('textInput', true, true, window, 'a');
input.dispatchEvent(event);
input.value = text;
input.blur();
}

// Now that it has removed the password, if this is the password input, reset its value correctly
if (input.type == "password") {
input.value = password;
input.blur();
}
}

}
}, 300);
};
<form method="post" class="login">

<input type="text" class="input-text" name="username" id="username" value="" placeholder="Email Address" />

<input class="input-text" type="password" name="password" id="password" placeholder="Password" />

<input type="submit" class="button" name="login" value="Start" />

<a href="/forgot-password">Forgotten password?</a>

</form>

最佳答案

Chrome 假装它没有该字段的值作为安全措施。没有办法解决这个问题。

您可以根据以下问题的答案更改样式:Google Chrome form autofill and its yellow background

关于javascript - 输入类型=密码值返回空,除非我在 chrome 控制台中使用断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29964035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com