4

I know, you can inject javascript into an Android WebView. I already did that. However, I load a html5/javascript web app (not my own, so I have no access to the code) and I need to inject additional javascript. But this web app does not work properly in the standard WebView. The reason for that is probably:

"By default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored."
http://developer.android.com/reference/android/webkit/WebView.html

So, I enabled Javascript, but maybe this is due to the fact that it ignores webpage errors. However in the normal Chrome browser everythings works fine without any problems.

The webview does not work although I already enabled a lot of stuff:

webView = (WebView) findViewById( R.id.webView );
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(PluginState.ON);
    webView.getSettings().setDomStorageEnabled(true);
    webView.setBackgroundColor(0x00000000);
    webView.setWebChromeClient( new WebChromeClient() );
    webView.setWebViewClient( new WebViewListener() );
    CookieManager.getInstance().setAcceptCookie(true);

So, is there some possibility to inject javascript into a normal WebChromeClient? Or do you have any other guess what else I could enable or inject into the webview?

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
toom
  • 12,864
  • 27
  • 89
  • 128

2 Answers2

3

First, you need to setWebViewClient with a class that's derived form WebViewClient:

WebView webview = new WebView();
webview.setWebViewClient(new WebClient());
webview.loadUrl("stackoverflow.com");

then in WebClient, you wait for the page to load (onPageFinished). Then you loadUrl("javascript:[your javascript here]").

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

    @Override
    public void onPageFinished(WebView view, String url) 
    {       
        // Obvious next step is: document.forms[0].submit()
        view.loadUrl("javascript:document.forms[0].q.value='[android]'");       
    }
}
sfinja
  • 400
  • 4
  • 11
  • 2
    Thanks. If you have a look into my code above you'll discover that I already did extactly that. However, it does not work, since the webview is not a fully compatible browser. Also my question is: "Inject javascript into WebChromeClient" not a WebViewClient. Maybee you should read my question first. Therefore -1 – toom Nov 02 '13 at 11:57
0

It is remembered wanting to do different JavaScript related tasks and so needing to enable a bunch of options and even set the browser type...here is those config options:

        webView.setWebChromeClient(webChromeClient);

        webView.setWebViewClient(new InsideWebViewClient(getBaseContext(), webView));

        WebSettings settings = webView.getSettings();
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setBuiltInZoomControls(false);
        settings.setUseWideViewPort(true);
        settings.setJavaScriptEnabled(true);
        settings.setSupportMultipleWindows(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setDomStorageEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setMediaPlaybackRequiresUserGesture(false);
        // Call private class InsideWebViewClient
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setAllowFileAccess(true);
//        settings.setUserAgentString("Mozilla/5.0");
        settings.setUserAgentString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36");
//        webView.setInitialScale(50);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (0 != (getApplicationContext().getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE))
            { webView.setWebContentsDebuggingEnabled(true); }
        }

for reference that was used with the same basic following: https://github.com/cprcrack/VideoEnabledWebView/blob/01c7f758a409fabbc501cdf24efdf5b77400280f/app/src/main/java/name/cpr/ExampleActivity.java

CrandellWS
  • 2,708
  • 5
  • 49
  • 111