最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 0
Add a ment  | 

2 Answers 2

Reset to default 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.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论