WebView可所谓是Android中最强大的控件之一,无所不能。
于是有这么一个需求,用户在app之中内嵌的WebView中输入帐号密码的时候,App需要捕获已经输入的帐号密码。
当用户输入帐号密码,一般情况下会进行页面转跳,在页面转跳之前执行js脚本,通过js脚本来获取这个帐号密码的value值。要先获取各个元素的class值,需要解析整个html页面,那么我们可以重写 onLoadResource 这个方法,代码如下:
webview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return false; } @Override public void onLoadResource(WebView view, String url) { getHtml(); Log.e(\"log-->\", \"onLoadResource-->>\" + url); } @Override public void onPageFinished(WebView view, String url) { } });
上面这个方法在加载页面资源时会调用,每一个资源(比如图片)的加载都会调用一次。那么我们可以在这个方法里面执行注入的js脚本
先执行addJavascriptInterface方法,将一个java对象绑定到一个js对象中,代码如下:
public class JavaScriptInterface { String mPasswrod; String mUsername; @JavascriptInterface public void getHTML(final String html) { if (!TextUtils.isEmpty(html)) { saveWebViewUserData.saveUserDataWebView(webview, html); } } @JavascriptInterface public void save_password(final String password) { if (!TextUtils.isEmpty(password)){ LogUtils.e(\"received from js. password = \" + password); this.mPasswrod = password; checkData(mUsername, mPasswrod); } } @JavascriptInterface public void save_username(final String username) { if (!TextUtils.isEmpty(username)) { LogUtils.e(\"received from js. username = \" + username); this.mUsername = username; checkData(mUsername, mPasswrod); } } } webview.addJavascriptInterface(new JavaScriptInterface(), \"android\"); private void getHtml() { webview.loadUrl(\"javascript:window.android.getHTML(\'<html>\'+document.body.innerHTML+\'</html>\');\"); }
那么下面这句话执行完的结果将会返回到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; }
将他们俩的class id记录下来,再次通过js代码获取到页面的value值,调用java代码保存下来。代码如下:
private void saveUsernameById(String id, WebView webView) { webView.loadUrl(\"javascript:window.android.save_username(document.getElementById(\'\" + id + \"\').value)\"); } private void savePasswordById(String id, WebView webView) { webView.loadUrl(\"javascript:window.android.save_password(document.getElementById(\'\" + id + \"\').value)\"); }
经过上面简单的处理,已经大致可以获取到用户输入的帐号密码了,经过测试,简单的页面中的帐号密码是可以获取到的,其他复杂的(如密码在转跳时清空了,又传值到其他地方进行运算的)需要再根据不同的方案来对付了。
转跳前先获取整个页面的html,用jsoup获取页面的所有class name,遍历各个节点,剔除无用内容(验证码按钮等),判断密码框在哪,剩下的可能就是帐号了,执行js代码获取value值。
以上就是本文的全部内容,希望大家喜欢。
本文地址:https://www.stayed.cn/item/14929
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我