The Android WebView control captures information entered by the user _android

Source: Internet
Author: User

WebView can be called one of the most powerful controls on Android, omnipotent.
So there is a need, the user in the app embedded in the WebView input account password, the app needs to capture the account password entered.

When the user input account password, the general will be the page to jump, in the page before the jump to execute the JS script, through the JS script to get the value of this account password. To get the class value of each element first, you need to parse the entire HTML page, so we can rewrite the Onloadresource method with the following code:

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) {

  }
});

The above method is invoked when the page resource is loaded, and every resource (such as a picture) is called once. So we can execute the injected JS script in this method.
First executes the Addjavascriptinterface method, binds a Java object to a JS object, the code is as follows:

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") ('  

The result of the following sentence will be returned to the Gethtml method in Javascriptinterface. That is, by binding, the JS code invokes the Java code and returns the entire HTML as the return value, executing the Savewebviewuserdata.saveuserdatawebview (WebView, HTML);

After you get the HTML that contains class, you need to analyze it in turn, usually, the page that normally enters account password contains type= "password" words. To determine whether this HTML page contains this typeface, if so, it may be the login page.

To determine the ID of the page, or whether the classname contains password, PWD, or what other password-related, this element must be the password box, and then filter out the other Button,hidden,submit,checkbox in the page and so on , the remaining one must be the username; the filter code is as follows: (here use Jsoup parse HTML to get individual document, loop traversal to remove unwanted elements)

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;
    The 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;

 }

Record their class ID, again through the JS code to obtain the value of the page, invoke the Java code to save. The code is as follows:

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)");
 

Through the above simple processing, has been generally able to obtain the user entered the account password, after testing, the simple page of the account password can be obtained, and other complex (such as the password in the jump empty, and the transfer value to other places to perform operations) need to be based on different scenarios to deal with.

Before the jump to get the entire page of HTML, using Jsoup to get all the class name of the page, traversing each node, eliminating useless content (verification code button, etc.), to determine the password box, the remaining may be the account number, the implementation of JS code to get value.

The above is the entire content of this article, I hope you like.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.