时间:2021-05-21
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对象中,代码如下:
那么下面这句话执行完的结果将会返回到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值。
以上就是本文的全部内容,希望大家喜欢。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言最近代码里和WebView有很多的交互,webview是android中的浏览器控件,这里主要介绍一下webview如何重载WebViewClient类来控
webview实现的作用是: WebView是android中一个非常重要的控件,它的作用是显示和渲染Web页面,直接使用html文件(网络上或本地asset
在Android中,WebView是用来loadhttp和https网页到本地应用的控件。在默认情况下,通过loadUrl(Stringurl)方法,可以顺利l
本文实例为大家分享了Android控件WebView实现保存阅读进度的具体代码,供大家参考,具体内容如下用户提了一个要求,要求保存他的阅读进度,然后在他下次阅读
本文实例总结了Android开发之TextView控件用法。分享给大家供大家参考,具体如下:TextView控件可以向用户展现文本信息,我们可以设置该文本信息是