Monday, June 3, 2024
145
rated 0 times [  148] [ 3]  / answers: 1 / hits: 15412  / 12 Years ago, fri, september 7, 2012, 12:00:00

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?


More From » greasemonkey

 Answers
23

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);
}
}

[#83190] Thursday, September 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mahogany

Total Points: 645
Total Questions: 107
Total Answers: 98

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
;