1

I am attempting to auto fill a field in a WebView. The page source of the webpage is here: http://pastebin.com/WKbj7XJh

I have javascript enabled in my WebView and I have the following code in my WebViewClient

public void onPageFinished(final WebView view, String url) {
        super.onPageFinished(view, url);
        String website = "https://www.example.com/oauth_callback";
        view.loadUrl("javascript:document.getElementById(\"application_callback_url\").value = '"+website+"';");
    }

Instead of autofilling the field, I get a blank WebView that simply displays: "https://www.example.com/oauth_callback"

I used this question as a guide: Fill fields in webview automatically

All help is greatly appreciated.

Community
  • 1
  • 1
Dick Lucas
  • 12,289
  • 14
  • 49
  • 76
  • 1
    Can you share more details about the platform version you are runnign and SDK you are building with? – ksasq Jun 23 '14 at 11:45
  • I am building with Android 4.4.2 (API 19) and running it on a Android 4.4.3 device. – Dick Lucas Jun 23 '14 at 18:51
  • 1
    Can you try using the WebView.evaluateJavaScript API instead? – ksasq Jun 23 '14 at 23:27
  • @ksasq Thank you so much for your help. This worked perfectly. Maybe on pre API 19 devices the code in my question will work, because that evaluateJavaScript API is only available in API 19. I will test this when I have some time. – Dick Lucas Jun 24 '14 at 13:43
  • 1
    Thanks for the update - glad it works for you. Yes, this is a change in the chromium webview that was introduced in Android 4.4. You should be able to use the loadUrl approach when your app is running on older devices. I'll formalise into an answer :) – ksasq Jun 24 '14 at 19:36

1 Answers1

3

When you start to target API 19 and above, this is the behavior of WebView.loadUrl when it's passed a javascript: URL that returns a value.

You should update your app so that when running on API 19 or above, it uses evaluateJavaScript, and falls back to loadUrl when running on older devices. Something like:

if (android.os.Build.Version.SDK_INT >= 19) {
    webview.evaluateJavaScript("...", null);
} else {
    webview.loadUrl("javascript:...");
}

Alternatively, you could mangle your javascript into a JS function that doesn't return a value, and use that with loadUrl on all platform versions.

ksasq
  • 4,424
  • 2
  • 18
  • 10