Some help with Javascript

Hi everyone!

I am having some issues creating a small JavaScript program.
I need to download a report from a website every 10 minutes and I would like to do this automatically.

Running below command work and start the download:

window.open(“DownloadURL”,"_self") ;

The only problem is I need to login with user name and password before being able to access the file.

Now I have 2 questions:

  1. Can I keep running a script if the first if the first command will open a link?

ex:

window.open(“URL to authentication”, “_top”);

   .....rest of the script.
  1. I am trying to use below script to log in automatically on the browser console:
var pwd="password";   //set password
      var usr="user name";   //set username
            var inputs=document.getElementsByTagName("input");    //look for all inputs

            for(var i=0;i<inputs.length;i++){{    //for each input on document

                  var input=inputs[i];     //look at input

                  if(input.type=="password"&&(input.name.toLowerCase().indexOf("auth")==-1)){
                          {input.value=pwd}
                  }
                  if(input.type=="text"&&(input.name.toLowerCase().indexOf("login")!=-1||input.name.toLowerCase().indexOf("user")!=-1||input.name=="AgentAccount")){
                          {input.value=usr}
                  }
             }};

document.querySelector('.__progress-button-content').click(); // click on login button

running this, the user name and the password field are populated with the correct variables, but I get an error message as if I insert the wrong password and username (which are correct for sure).

Can anyone help me with this issue or suggest me a different approach?
I really hope my explanation was not too chaotic!

Thanks!!!

1 Like

Hi @Luca_C,

Can you explain why you had brackets over here –

Because the if condition curly brackets already exist.

Secondly, you can declare your variable outside of the for loop instead of multiple declarations inside the for loop. So basically –

var input= "";
for(var i=0;i<inputs.length;i++){{    //for each input on document

                  input=inputs[i];     //look at input

                  if(input.type=="password"&&(input.name.toLowerCase().indexOf("auth")==-1)){
                          {input.value=pwd}
                  }
                  if(input.type=="text"&&(input.name.toLowerCase().indexOf("login")!=-1||input.name.toLowerCase().indexOf("user")!=-1||input.name=="AgentAccount")){
                          {input.value=usr}
                  }
             }};

Thirdly, this appproach is kind of confusing. The other way I would probably do is to attach “id” attribute to all the input fields and then do a query selector to extract the values. Probably like this –

//HTML PART----
<input type="password" id="pass">
<input type="text" id="username">


//JS PART----
var inputPassword = document.querySelector("#pass").value;
var inputUserName = document.querySelector("#username").value;

Hope this helps. Happy Learning!

1 Like

Hi Malik,

Thank you very much for your answer. Your explanation clarified a lot.
the brackets on input.value=pwd were actually an error on the copy paste of the code.
Thanks for pointing it out. Unfortunately trying and doing some reading online I understood
I can’ keep running a script from a page after the new webpage has been loaded so I will have to find a different approach for my issue. Thank you very much for your help clarifying the code :slight_smile:

1 Like

No problem. We are here to assist you at all times. :slight_smile:

1 Like