html - Auto fill webpage username password using Javascript - Stack Overflow

I'm currently working on an application with a built in web browser. To make life easier for our c

I'm currently working on an application with a built in web browser. To make life easier for our clients, we have the option for them to store passwords for their websites in their database. Currently, we are using Javascript to pull the information into the browser after the page is loaded.

Here is the script we run on the page that needs user/password loaded....

javascript:

      var pwd="{0}";   //get password from vb app
      var usr="{1}";   //get username from vb app
      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 whatever 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}
            }
       }};
undefined;

One of the pages I'm looking at is / -- the code for the form I'm looking at is this.

<div class="row">
    <label for="username">Username:</label>
    <input name="username" type="text" maxlength="75">
</div>
<div class="row">
    <label for="password">Password:</label>
    <input name="password" type="password" maxlength="50">
</div>

And again, this works fine. It populates the password/username field just fine.

My question is why/how, specifically in relation to .indexOf("whatever"). From my understanding, the current script looks for an input, checks if it's a password field or what have you and then sets that field...

But it's never actually looking for those fields. For example on the pagepluscellular website listed above, the input names are "username" and "password", but in our script, it never actually looks for the name "username" or "password".... so how does it know how to fill in those specific inputs?

What I'm trying to do is this. We found a website that this DOESN'T work for. Namely .jsp . The code for the form on this page is...

<input id="loginform:username" type="text" name="loginform:username" "class="outputtext" size="20">
<input id="loginform:password" type="password" name="loginform:password" value="" size="20" class="inputSecret">

So I'm 99.99% sure that it's because the name is "loginform:whatever". What I DON'T know is how to tell my script to include this if the name is actually "loginform:whatever". I tried changing my script to this... (just checked if it equaled "loginform:whatever")

javascript:

      var pwd="{0}";   //get password from vb app
      var usr="{1}";   //get username from vb app
      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 whatever input

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

but instead of giving me the desired behavior, it ended up breaking the original behavior so that none of it worked (pageplus website stopped pulling username/password). When I reverted back to the original script, it started working again.

Could someone please explain to me the logic behind this line of code?

input.name.toLowerCase().indexOf("auth") == -1 // or !=-1 for "login", etc

My understanding is that you are looking at the input name in lowercase, and seeing if it contains the words 'auth'. If it contains 'auth' in the first space, it should fill in the password.... right?

Sorry for the long post, I just wanted to get my thoughts down somewhere and explain all I had tried so far.

I'm currently working on an application with a built in web browser. To make life easier for our clients, we have the option for them to store passwords for their websites in their database. Currently, we are using Javascript to pull the information into the browser after the page is loaded.

Here is the script we run on the page that needs user/password loaded....

javascript:

      var pwd="{0}";   //get password from vb app
      var usr="{1}";   //get username from vb app
      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 whatever 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}
            }
       }};
undefined;

One of the pages I'm looking at is https://www.pagepluscellular./login/ -- the code for the form I'm looking at is this.

<div class="row">
    <label for="username">Username:</label>
    <input name="username" type="text" maxlength="75">
</div>
<div class="row">
    <label for="password">Password:</label>
    <input name="password" type="password" maxlength="50">
</div>

And again, this works fine. It populates the password/username field just fine.

My question is why/how, specifically in relation to .indexOf("whatever"). From my understanding, the current script looks for an input, checks if it's a password field or what have you and then sets that field...

But it's never actually looking for those fields. For example on the pagepluscellular website listed above, the input names are "username" and "password", but in our script, it never actually looks for the name "username" or "password".... so how does it know how to fill in those specific inputs?

What I'm trying to do is this. We found a website that this DOESN'T work for. Namely https://www.boostmobilesales./boost-sales-portal/faces/login.jsp . The code for the form on this page is...

<input id="loginform:username" type="text" name="loginform:username" "class="outputtext" size="20">
<input id="loginform:password" type="password" name="loginform:password" value="" size="20" class="inputSecret">

So I'm 99.99% sure that it's because the name is "loginform:whatever". What I DON'T know is how to tell my script to include this if the name is actually "loginform:whatever". I tried changing my script to this... (just checked if it equaled "loginform:whatever")

javascript:

      var pwd="{0}";   //get password from vb app
      var usr="{1}";   //get username from vb app
      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 whatever input

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

but instead of giving me the desired behavior, it ended up breaking the original behavior so that none of it worked (pageplus website stopped pulling username/password). When I reverted back to the original script, it started working again.

Could someone please explain to me the logic behind this line of code?

input.name.toLowerCase().indexOf("auth") == -1 // or !=-1 for "login", etc

My understanding is that you are looking at the input name in lowercase, and seeing if it contains the words 'auth'. If it contains 'auth' in the first space, it should fill in the password.... right?

Sorry for the long post, I just wanted to get my thoughts down somewhere and explain all I had tried so far.

Share Improve this question edited Aug 6, 2014 at 18:35 mlibby 6,7441 gold badge34 silver badges41 bronze badges asked Aug 6, 2014 at 18:12 Chris HobbsChris Hobbs 8172 gold badges11 silver badges31 bronze badges 2
  • What browser are you testing in? Have you tried putting break points in the debugger to trace the execution? Does the console report any errors or anything? – mlibby Commented Aug 6, 2014 at 18:36
  • Thanks for your answer below @mlibby -- that made me realize that I had originally checked that the index of the substring I WANTED was -1, meaning I was actually telling it to only include the strings if the name WASN'T there --- thanks a ton! – Chris Hobbs Commented Aug 6, 2014 at 18:48
Add a ment  | 

1 Answer 1

Reset to default 3

input.name.toLowerCase().indexOf("auth")==-1 means that the input name does not include the character sequence auth at all (otherwise the index would be a non-negative number).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744135933a4560058.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信