因为是自动保存的形式如何工作没有标准,我会成立一个计时器上setTimeout()
呃..我是哑巴。当前代码尝试输入其用户信息时会造成不好的结果。
未经测试,快速编写的代码:
function logMeIn() {
var el = document.getElementById("username");
if (el && el.value !== "") {
// Finish the login
} else {
window.setTimeout(logMeIn, 200);
}
}
logMeIn();
尝试2:
// This is a User Script -- runs in its own encloser, won't pollute names back to the main document.
var loginTimer;
function logMeIn() {
var el = document.getElementById("username");
if (el && el.value !== "") {
// Finish the login
} else {
loginTimer = window.setTimeout(logMeIn, 200);
}
}
logMeIn();
// can't use ".onfocus" -- its a userscript.
// Cancel the timer if the username field gets focus -- if the user tries to enter things.
document.getElementById("username").addEventListner("focus") = function(e) {
if (loginTimer) {
window.clearTimeout(loginTimer);
}
}
我一直在讨论这样做,但如果由于某种原因用户忘记在浏览器中保存他们的用户名和密码会怎么样。这不会创建一个锁定浏览器的无限循环吗? – RandomPrecision
@RandomPrecision - 由于setTimeout的原因,它不会锁定浏览器。它可能会在页面上消失几个周期,但假设你不是永远坐在那页上,那应该不会太糟糕。如果您担心通过移动设备的资源烧毁,您可以放入柜台并只检查X次。当文档失去焦点时,您也可以禁用代码。为了简单起见,我放弃了这些东西。 –
谢谢你的解释和代码。这将工作。谢谢。 – RandomPrecision