简体   繁体   English

使用Javascript自动填充网页用户名密码

[英]Auto fill webpage username password using Javascript

I'm currently working on an application with a built in web browser. 我目前正在使用内置的Web浏览器开发应用程序。 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. 目前,我们正在使用Javascript在加载页面后将信息提取到浏览器中。

Here is the script we run on the page that needs user/password loaded.... 这是我们在需要加载用户/密码的页面上运行的脚本....

javascript: JavaScript的:

      var pwd="{0}";   //get password from vb.net app
      var usr="{1}";   //get username from vb.net 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.com/login/ -- the code for the form I'm looking at is this. 我正在查看的其中一个页面是https://www.pagepluscellular.com/login/ - 我正在查看的表单的代码就是这个。

<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"). 我的问题是为什么/如何,特别是关于.indexOf(“无论如何”)。 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? 例如,在上面列出的pagepluscellular网站上,输入名称是“用户名”和“密码”,但在我们的脚本中,它实际上从未查找名称“用户名”或“密码”....所以它如何知道如何填写那些具体的输入?

What I'm trying to do is this. 我想要做的就是这个。 We found a website that this DOESN'T work for. 我们找到了一个不适用的网站。 Namely https://www.boostmobilesales.com/boost-sales-portal/faces/login.jsp . https://www.boostmobilesales.com/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". 所以我99.99%肯定是因为这个名字是“loginform:whatever”。 What I DON'T know is how to tell my script to include this if the name is actually "loginform:whatever". 我不知道的是如果名称实际上是“loginform:whatever”,如何告诉我的脚本包含这个。 I tried changing my script to this... (just checked if it equaled "loginform:whatever") 我尝试将我的脚本改为此...(只是检查它是否等于“loginform:whatever”)

javascript: JavaScript的:

      var pwd="{0}";   //get password from vb.net app
      var usr="{1}";   //get username from vb.net 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). 但它没有给我所希望的行为,而是最终破坏了原来的行为,因此没有任何工作(pageplus网站停止提取用户名/密码)。 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'. 我的理解是,您正在查看小写的输入名称,并查看它是否包含单词'auth'。 If it contains 'auth' in the first space, it should fill in the password.... right? 如果它在第一个空格中包含'auth',它应该填写密码....对吗?

Sorry for the long post, I just wanted to get my thoughts down somewhere and explain all I had tried so far. 对不起,很长的帖子,我只是想把我的想法放到某个地方并解释我到目前为止所做的一切。

input.name.toLowerCase().indexOf("auth")==-1表示输入名称根本包括字符序列auth (否则索引将是非负数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM