5

I am trying to inject JavaScript for reading on specific value while loading webView.

These are the properties i used for my webView.

    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setCacheMode(2);
    webView.getSettings().setDomStorageEnabled(true);
    webView.clearHistory();
    webView.clearCache(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setUseWideViewPort(false);
    webView.getSettings().setLoadWithOverviewMode(false);
    webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

and i am injecting javacript in my onPageFInished() method.

        @Override
        public void onPageFinished(final WebView view, final String url) {
            webView.post(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('input')[0].value+'</head>');");   
                }
            });
            super.onPageFinished(view, url);
        }

Below code is MyJavaScriptInterface.

public  class MyJavaScriptInterface{

    @JavascriptInterface
    public void showHTML(String html_data) {
        if(html_data.contains("response_code")){
            Log.e(TAG, " ======>  HTML Data : "+  html_data);
            new MakeQueryPayment().execute();
        }        
    }
}

Error i captured from the Logcat.

01-08 17:56:43.701 I/chromium(27026): [INFO:CONSOLE(1)] "Uncaught TypeError: window.HTMLOUT.showHTML is not a function", source:  (1)

I m facing this problem only in Samsung Galaxy Tab A, Model Number is SM-T550 , Android Version is 5.0.2. In other devices which we have it's working fine. Can any one please help me out from this. Thanks in advance.

Mykola
  • 3,343
  • 6
  • 23
  • 39

2 Answers2

4

I try on Galaxy Tab 4, this code running well on this device.

WebView properties

WebView webView = new WebView(this);
setContentView(webView);
webView.clearHistory();
webView.clearCache(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(2);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewMode(false);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.loadUrl("http://stackoverflow.com/questions/34746626/uncaught-typeerror-window-htmlout-showhtml-is-not-a-function");

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
    }              

    @Override
    public void onPageStarted(WebView view, String url,
                    Bitmap favicon) {
    }

    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:window.HTMLOUT.showHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
    }
});

JavaScript Interface

public  class MyJavaScriptInterface{

    @JavascriptInterface
    public void showHTML(String html_data) {
        Log.e("", " ======>  HTML Data : "+  html_data);
    }
}
nurisezgin
  • 1,530
  • 12
  • 19
  • At my place also it's working fine in other samsung tabs. – Manikanta andydev Jan 12 '16 at 16:34
  • @nurisezgin i tried to send the value of html_data to UI but i got empty data on textarea using mWebView.loadUrl("javascript:MyFunction('" + html_data + "');"); the toast shows the value but i can't pass it to javascript! could you help me fix it?Thanks – user1788736 Dec 16 '17 at 07:09
  • Hi! What is your problem exactly, do you try to call javascript method from java? – nurisezgin Dec 16 '17 at 19:07
1

For me, as it was working fine before upgrading to the latest targetSDK, I just had to add the annotation @JavascriptInterface on all my functions.

Example:

@JavascriptInterface
public void eventDragStart() {
    // do somthing
}
thiagolr
  • 6,909
  • 6
  • 44
  • 64
  • that is the actual solution. because android studio cannot get funtion name from string so mark them! – MbPCM Mar 06 '22 at 10:36