Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill form in WebView with Javascript

I'm trying to fill Webforms from a Webview in Android. I've already found this piece of code here: Fill fields in webview automatically

String username = "cristian";
webview.loadUrl("javascript:document.getElementById('username').value = '"+username+"';");

Unfortunatly I dont understand where I have to open the page I want to fill in.

setContentView(R.layout.web);
final WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
String user="u";
String pwd="p";
mWebView.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';");

When I try it this way, the site gets displayed but without any values in the forms.

Thanks in advance for helping

like image 383
Jay Avatar asked Apr 17 '12 08:04

Jay


3 Answers

Android 4.4 new WebView have the issue with use loadUrl("javascript:") method, the paramter string will be url-decode before execution. You can try use evaluateJavascript() for API >= 19 and loadUrl() for API < 19. The code below is work for me.

    mWebView.loadUrl(url)
    WebSettings settings = mWebView.getSettings();
    settings.setJavaScriptEnabled(true);

    String js = "javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';";
    mWebView.setWebViewClient(new WebViewClient() {

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

            if (Build.VERSION.SDK_INT >= 19) {
                view.evaluateJavascript(js, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {

                    }
                });
            } else {
                view.loadUrl(js);
            }
       });
    }

please refer: loadUrl("javascript:....") behaviour changed incompatibly in Android 4.4

like image 92
awind Avatar answered Nov 15 '22 03:11

awind


You should fill the values after the page has been loaded. This is an example using your code:

mWebView.loadUrl(url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        String user="u";
        String pwd="p";
        view.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';");
    }
});
like image 22
gnpaolo Avatar answered Nov 15 '22 05:11

gnpaolo


Just Enable DomStorage and write "var x=" to string:

webview.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);

webview.loadUrl(urlString);
webview.setWebViewClient(new WebViewClient(){

public void onPageFinished(WebView view, String url){  
super.onPageFinished(view, url);
String js = "javascript:var x=document.getElementById('username').value = '"+user+"';var y=document.getElementById('password').value='"+pass+"';";

if (Build.VERSION.SDK_INT >= 19) {
        view.evaluateJavascript(js, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {

            }
        });
    } else {
        view.loadUrl(js);
    }

view.loadUrl(js);
}
});
like image 2
Denish Rana Avatar answered Nov 15 '22 05:11

Denish Rana