xur17
xur17

Reputation: 516

Android Webview Javascript Injection

I'm attempting to inject javascript into a page I load in a webview. For example, I am placing a value into a given text box by override 'onPageFinished' within a custom WebViewClient class:

public class MyAppWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url)
    {
        super.onPageFinished(view, url);
        view.loadUrl("javascript:" +
            "document.getElementById('elid').value = 'texthere';");
    }
}

This works correctly when browsing the website, and clicking links, but when I press the back button, the java/javascript does not appear to be correctly executed (the text box is not properly filled out.

The onPageFinished function is called when the back button is pressed, but it seems to be executing the javascript on the current page, not on the page being returned to.

Upvotes: 2

Views: 11850

Answers (2)

BuvinJ
BuvinJ

Reputation: 11046

I don't know the specifics of an Android WebView (I am just learning about them right now), but this might help... In general web development you can force the onload to fire when a page is reached via a back button like so:

//This Ensures onLoad() Functions Fire When 
//This Page Is Left & Then Returned To Via The Back Button
window.onunload = function(){};

NOTE: window.onunload NOT window.onload

Upvotes: 0

HenryChuang
HenryChuang

Reputation: 1459

https://github.com/henrychuangtw/Android-Javascript-Injection

Step 1 : create a class which called by javascript

class MyJavaScriptInterface
{
    @JavascriptInterface
    public void processHTML(String html)
    {
        //called by javascript
    }
}


Step 2 : register interface for javascript

webview1.addJavascriptInterface(new MyJavaScriptInterface(), "MYOBJECT");


Step 3 : inject javascript to page

webview1.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        StringBuilder sb = new StringBuilder();
        sb.append("document.getElementsByTagName('form')[0].onsubmit = function () {");
        sb.append("var objPWD, objAccount;var str = '';");
        sb.append("var inputs = document.getElementsByTagName('input');");
        sb.append("for (var i = 0; i < inputs.length; i++) {");
        sb.append("if (inputs[i].type.toLowerCase() === 'password') {objPWD = inputs[i];}");
        sb.append("else if (inputs[i].name.toLowerCase() === 'email') {objAccount = inputs[i];}");
        sb.append("}");
        sb.append("if (objAccount != null) {str += objAccount.value;}");
        sb.append("if (objPWD != null) { str += ' , ' + objPWD.value;}");
        sb.append("window.MYOBJECT.processHTML(str);");
        sb.append("return true;");
        sb.append("};");

        view.loadUrl("javascript:" + sb.toString());
    }

});

Upvotes: 5

Related Questions