WIll window.addEventListener("load", function(), false); wait for fields to be auto populated by the browser?

15,044

Solution 1

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

Solution 2

Did you mean to reference Login instead of immediately executing it?

window.addEventListener("load", Login, false);

Your way executes Login before the window loads.

Share:
15,044

Related videos on Youtube

RandomPrecision
Author by

RandomPrecision

Updated on October 02, 2022

Comments

  • RandomPrecision
    RandomPrecision over 1 year

    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?

  • Jeremy J Starcher
    Jeremy J Starcher over 11 years
    Good point -- but still leaves wiggle room for order of events handling. It sure seems like there have been enough times it took the browser ages to autopopulate the forms.
  • RandomPrecision
    RandomPrecision over 11 years
    Hmm.. I thought window.addEventListener("load", Login(), false); would wait for the page to load then run the function specified.
  • Jeremy J Starcher
    Jeremy J Starcher over 11 years
    @RandomPrecision -- Login() will run the function, then return a value. Login is a function reference that will be called later.
  • RandomPrecision
    RandomPrecision over 11 years
    I've debated doing it this way but what if for some reason the user forgot to save their username and password in the browser. Would this not create an infinite loop that locks the browser?
  • Jeremy J Starcher
    Jeremy J Starcher over 11 years
    @RandomPrecision -- Because of the setTimeout it won't lock up the browser. It may burn off a few cycles while it sits on the page, but assuming that you aren't sitting on that page forever it should't be too bad. If you are worried about burning through resources of a mobile device, you could put in a counter and only check X number of times. You could also disable the code when the document looses focus. I left that stuff out for the sake of simplicity.
  • RandomPrecision
    RandomPrecision over 11 years
    Ahh i see what you're saying. So by putting the () it immediately runs the function but leaving it with just the function name it calls the function when it's done?
  • Jeremy J Starcher
    Jeremy J Starcher over 11 years
    @RandomPrecision - Exactly. Function references are a key part of the Javascript mindset. This gets confusing because this code will also work: window.addEventListener("load", "Login()", false); By sending a string instead of a function reference, the Javascript engine actually runs all of this through eval and runs it through a Javascript COMPILER to make it all work. A lot of cruddy examples use code like this and it makes it very confusing for beginners.
  • RandomPrecision
    RandomPrecision over 11 years
    Thank you for the explanation and code. This will work. Thank you.
  • Jeremy J Starcher
    Jeremy J Starcher over 11 years
    If that doesn't work, let me know. Userscripts are one of my specialties.