-1

I am trying to create a Tampermonkey script to automatically fill a login form with my username and password and then I would like to click the login button. However, with the code below, it appears to wait the three seconds, and THEN it fills the text into the boxes. How can I "flush" the changes so that they appear before the wait time occurs?

Thanks!

(function() {
    'use strict';
     function wait(ms){
           var start = new Date().getTime();
           var end = start;
           while(end < start + ms) {
                end = new Date().getTime();
           }
     }
     window.onbeforeunload = function (){
     return "Leaving page...";
     }
     $(document).ready(function() {
         document.getElementById("username").value = "userValue";
         document.getElementById("password").value = "passwordValue";
         wait(3000);
         document.getElementsByClassName("btn-large").click();
     });
})();
Norman Percy
  • 858
  • 1
  • 8
  • 13

1 Answers1

0

That wait function is locking up the browser tab (and probably the browser, and potentially the whole computer). Don't code like that!

Also:

  1. (function() {... is completely superfluous in a Tampermonkey script.
  2. $(document).ready(function()... is not helpful except in rare cases that do not apply here.
  3. Lots,   lots   more, etc...

Here's that code refactored:

window.onbeforeunload = function () {
    return "Leaving page...";
}
//-- Hacker easter egg!  See linked questions.
document.getElementById ("username").value = "userValue";
document.getElementById ("password").value = "passwordValue";

setTimeout (clickBtnAfterDelay, 3000);

function () {
    //-- Brittle!  See linked questions.
    document.getElementsByClassName ("btn-large")[0].click ();
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Ok, thanks! Sorry about the code; I’m not a JavaScript guy. – Norman Percy Jun 14 '18 at 00:57
  • Thanks are good, but did you [find the post useful](https://stackoverflow.com/help/privileges/vote-up)? Do you agree that it [solves the issue outlined in the question](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)? (Based on the information given at the time; it does.) – Brock Adams Jun 14 '18 at 01:21
  • Sorry - totally missed accepting the answer! That's fixed now. – Norman Percy Aug 31 '18 at 22:09