使用JavaScript在UIWebView中查找和自动填充HTML登录表单 [英] Finding and auto-filling HTML login forms in a UIWebView using JavaScript

查看:234
本文介绍了使用JavaScript在UIWebView中查找和自动填充HTML登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIWebView,它像一个互联网浏览器,加载它所在的网页的HTML。

在webViewController中,方法webViewDidFinishLoad当网页在UIWebView上加载完成时,从网页加载HTML。

从HTML中我想 sieve out textfields以方便自动填充该文本字段的值,并将值存储在我的数据库中。



有什么方法可以做到这一点?该方法应该能够在所有网站上工作。



设置文本UIWebView中的textfield 几乎可以帮助我,但我已经尝试过了,并且文本字段永远不会被填充。



在登录页面中会有两个文本字段
所以我尝试使用

  document.getElementsByTagName('INPUT')[0] .value 
document.getElementsByTagName('INPUT')[1] .value

输入值但不是魔术。



编辑:
i已经尝试过阵列中的其他位置。 Facebook的用户名和密码是[3]和[4],而亚马逊则是[11]和[14]。
,所以字段使用上述方法的位置是随机的。任何其他建议,将适用于所有网站?



编辑2:
i可以尝试

  document.getElementsById('id_name')。value 

是不会为我工作,因为我需要一个通用的方法,可以识别任何网站上的文本字段(所有网站使用不同的ID名称)



另外,在我看来,一些的网站一直设置这个tabindex =1为用户名和tabindex =2为密码。



例如在Facebook这样的网页中:

 < input type =textclass =inputtextname =emailid =emailvalue =tabindex =1/> 
< input type =passwordclass =inputtextname =passid =passtabindex =2/>

亚马逊:

 < input id =ap_emailname =emailvalue =type =emailsize =30maxlength =128tabindex =1autocorrect =offautocapitalize =关闭/> 

dbs bank:

 < input type =texttabindex =1maxlength =20size =32name =UIDid =UID> 
< input type =passwordonkeyup =keyUp(event)onkeydown =return onlyNumerics(event)tabindex =2maxlength =9size =32name =PINid =PINautocomplete =off>

但我没有在google中看到这个tabindex:

 < input type =textspellcheck =falsename =Emailid =Emailvalue => 
< input type =passwordname =Passwdid =Passwd>

任何建议?



终极目标是为了能够筛选所有/任何网站的用户名和密码文本字段。 =)

解决方案

好的,所以没有一个适合所有人的方式。你可以非常接近,但它永远不会在每个 网站上工作,因为一个网站可能有多个登录表单或多个字段组成用户名(有些银行有这个)。



但这会让你朝正确的方向发展。要获得所有的密码字段(主要是一个),使用这个:

  document.querySelectorAll(input [type ='password '])

要获得所有文本输入字段,请使用以下命令:

  document.querySelectorAll(input [type ='text'])

对于文本输入字段,您很可能会得到多个结果(可能是搜索字段和内容)。因此,您必须迭代它们并查找常见的ID或名称,例如用户名,用户,用户名,UID等。



   - (void)webViewDidFinishLoad:(UIWebView *)webView { 
NSString * savedUsername = @peter;
NSString * savedPassword = @Pan123;
$ b $ if(savedUsername.length!= 0& amp; savePassword.length!= 0){
//创建js字符串
NSString * loadUsernameJS = [NSString stringWithFormat:@ var inputFields = document.querySelectorAll(\input [type ='text'] \); \
for(var i = inputFields.length>>> 0; i--; ){inputFields [i] .value ='%@';},savedUsername];
NSString * loadPasswordJS = [NSString stringWithFormat:@document.querySelectorAll(\input [type ='password'] \)。value ='%@',savedPassword];

//自动填写表单
[self.webView stringByEvaluatingJavaScriptFromString:loadUsernameJS];
[self.webView stringByEvaluatingJavaScriptFromString:loadPasswordJS];


$ / code $ / pre
$ b $ p请注意: em> textfield用户名,只有第一个密码字段与密码。



享受。


i have a UIWebView which acts like an internet browser and loads the HTML of the webpages that it is at.

in the webViewController, the method webViewDidFinishLoad, would have loaded the HTML from the webpage when the webpage finish loading on the UIWebView.

From the HTML i would like to sieve out textfields to facilitate the auto population of that textfield with values stored in my database.

Any methods to do that? The method should be able to work on all websites.

Set text for a textfield in UIWebView has almost what might help me, but i have tried it and the text field never got filled.

In a login page there will be two text fields so i tried using

document.getElementsByTagName('INPUT')[0].value
document.getElementsByTagName('INPUT')[1].value 

to input in the values but no magic.

Edit: i have tried other position in the array. the username and password for Facebook is [3] and[4] whereas for amazon it is [11] and [14]. so the position of where the fields are using the above method is kinda random. Any other suggestion that will work for all website?

Edit2: i could try

document.getElementsById('id_name').value

but the ID method is not going to work for me as i need a universal method that will identify textfields on any websites(all websites uses different ID names)

Also it seems to me that some of the websites have consistently set this tabindex="1" for username and tabindex="2" for password.

for instance in the webpage like Facebook:

<input type="text" class="inputtext" name="email" id="email" value="" tabindex="1" />
<input type="password" class="inputtext" name="pass" id="pass" tabindex="2" /> 

amazon:

<input id="ap_email" name="email" value="" type="email" size="30" maxlength="128" tabindex="1" autocorrect="off" autocapitalize="off" />
<input id="ap_password" name="password" type="password" maxlength="1024" size="20"  tabindex="2" onkeypress="displayCapsWarning(event,'ap_caps_warning', this);" class="password"/> 

dbs bank:

<input type="text" tabindex="1" maxlength="20" size="32" name="UID" id="UID">
<input type="password" onkeyup="keyUp(event)" onkeydown="return onlyNumerics(event)" tabindex="2" maxlength="9" size="32" name="PIN" id="PIN" autocomplete="off">

but i didn't see this tabindex in google:

<input type="text" spellcheck="false" name="Email" id="Email" value="">
<input type="password" name="Passwd" id="Passwd">

any suggestion?

The Ultimate goal is to be able to sieve out Username and Password Text field for all/any websites. =)

解决方案

OK, so there's no one-size-fits-all for this. You can get pretty close, but it will never work on every website, since a website could possibly have multiple login forms, or multiple fields that make up the username (some banks have that).

But this will get you in the right direction. To get all the password fields (mostly just one), use this:

document.querySelectorAll("input[type='password']")

To get all the text input fields, use this:

document.querySelectorAll("input[type='text']")

For the text input fields, you'll most likely get multiple results (maybe search fields and stuff). So you'll have to iterate over them and look for common IDs or names, like "username", "user", "user_name", "UID" and so on.

This is how you could use it in Objective-C:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *savedUsername = @"peter";
    NSString *savedPassword = @"Pan123";

    if (savedUsername.length != 0 && savedPassword.length != 0) { 
        //create js strings
        NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
        for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
        NSString *loadPasswordJS = [NSString stringWithFormat:@"document.querySelectorAll(\"input[type='password']\").value ='%@'", savedPassword];

        //autofill the form
        [self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
        [self.webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
    }
}

Please note: It fills every textfield with the username and only the first password field with the password.

Enjoy.

这篇关于使用JavaScript在UIWebView中查找和自动填充HTML登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆