I'm creating a GreaseMonkey script that will auto login to a page as long as the user has saved their username and password in the browser. It's pretty simple, it's just checks to make sure that the username field and the password field are not blank and then it clicks the login button automatically.
Every now and then I was running in to an issue to where it didn't login. The page loaded and just sat there. I assumed it was simply due to the page not being fully loaded when the check of the username and password fields were done.
Because of this, I added this to my script.
window.addEventListener("load", Login(), false);
My question is... Will this actually wait for the browser to auto-fill those fields before attempting to login or is the page loading and the browser populating those fields 2 different actions?
I'm creating a GreaseMonkey script that will auto login to a page as long as the user has saved their username and password in the browser. It's pretty simple, it's just checks to make sure that the username field and the password field are not blank and then it clicks the login button automatically.
Every now and then I was running in to an issue to where it didn't login. The page loaded and just sat there. I assumed it was simply due to the page not being fully loaded when the check of the username and password fields were done.
Because of this, I added this to my script.
window.addEventListener("load", Login(), false);
My question is... Will this actually wait for the browser to auto-fill those fields before attempting to login or is the page loading and the browser populating those fields 2 different actions?
Share Improve this question asked Sep 7, 2012 at 17:34 RandomPrecisionRandomPrecision 351 gold badge1 silver badge3 bronze badges 02 Answers
Reset to default 2Did you mean to reference Login
instead of immediately executing it?
window.addEventListener("load", Login, false);
Your way executes Login
before the window loads.
Because there is no standard on how the auto-saved forms work, I would set up a timer on setTimeout()
Er.. I was dumb. Current code does bad things if the person tries to enter their user information.
Untested, quick written code:
function logMeIn() {
var el = document.getElementById("username");
if (el && el.value !== "") {
// Finish the login
} else {
window.setTimeout(logMeIn, 200);
}
}
logMeIn();
Try 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);
}
}