那么下面这句话执行完的结果将会返回到JavaScriptInterface中getHTML方法里面。也就是说通过绑定,js代码调用了java代码,并将整个html作为返回值返回,执行的是saveWebViewUserData.saveUserDataWebView(webview, html);
得到了包含class的html之后,就需要依次分析了,通常来说,一般输入帐号密码的页面都含有 type=”password” 字样。先判断这个html页面是否含有这个字样,如果有,那么可能就是登录页面。
再判断这个页面的id,或者是classname是否包含password啦,pwd啦,或者什么其他和密码有关的了,这个元素肯定就是密码框了,再过滤掉页面中其他的button,hidden,submit,checkbox等等,剩下的那一个肯定就是用户名了;过滤代码如下:(这里使用jsoup解析html获取各个document,循环遍历剔除不需要的元素)
public void saveUserDataWebView(WebView webView, String html) {
Document document = Jsoup.parse(html);
Elements elements = document.select("input");
boolean isContainsPassword = false;
for (Element element : elements) {
String type = element.attr("type");
if ("password".equals(type)) {
isContainsPassword = true;
break;
}
}
if (!isContainsPassword) {
return;
}
for (Element element : elements) {
String className = element.className();
String type = element.attr("type");
webView.post(new Runnable() {
@Override
public void run() {
LogUtils.e("this element id is = " + element.attr("id") + " type = " + type);
String id = element.attr("id");
if (filterData(type, id)) {
int handType = handleType(type);
if (handType == NONE) {
handType = handleId(id);
if (handType == NONE) {
handleClassName(className);
}
}
switch (handType) {
case PASSWORD:
if (id==null){
}else {
savePasswordById(id, webView);
}
break;
case USERNAME:
if (id==null){
}else {
saveUsernameById(id, webView);
}
break;
case NONE:
break;
}
}
}
});
}
}
private int handleClassName(String className) {
if (className == null) {
return ERROR;
}
if (className.contains("password")) {
return PASSWORD;
}
if (className.contains("captcha")) {
return ERROR;
}
return USERNAME;
}
private boolean filterData(String type, String id) {
if ("captcha".equals(type)) {
return false;
} else if ("login_vcode".equals(type)) {
return false;
} else if ("button".equals(type)) {
return false;
} else if ("hidden".equals(type)) {
return false;
} else if ("submit".equals(type)) {
return false;
} else if ("checkbox".equals(type)) {
return false;
} else if ("captcha".equals(id)) {
return false;
} else if ("inp_ChkCode".equals(id)) {
return false;
} else {
return true;
}
}
private int handleId(String id) {
if (id == null) {
return NONE;
}
if (id.contains("captcha")) {
return ERROR;
}
if (id.contains("password")) {
return PASSWORD;
}
if (id.contains("Phone")) {
return USERNAME;
}
if (id.contains("username")) {
return USERNAME;
}
if (id.contains("code")) {
return ERROR;
}
return USERNAME;
}
private int handleType(String type) {
if (type == null) {
return NONE;
}
if (type.contains("tel")) {
return ERROR;
}
if (type.contains("pwd")) {
return PASSWORD;
}
if (type.contains("password")) {
return PASSWORD;
}
return NONE;
}










