因爲是自動保存的形式如何工作沒有標準,我會成立一個計時器上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