1

Im using the JavaScriptInterface for a web view and trying to retrieve the number of elements with Tag 'a'. But im not getting any result when i tried with the google page. Guys help me out.

The following snippet i've used.

    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new WebViewJSObject(), "jsInjectedObject");
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            if(url.equalsIgnoreCase("http://www.google.com")){
            view.loadUrl("javascript:(function(){var b = document.getElementsByTagName('a');jsInjectedObject.printInfo(b.length);})()");
            }
        }
    });
    webView.loadUrl("http://www.google.com");


    class WebViewJSObject{

        @JavascriptInterface
        public void printInfo(String count){
            Log.e(TAG, "entered PrintInfo");
            Log.e(TAG, "A tags count :" + count);
        }
    }
Naresh Gunda
  • 332
  • 1
  • 2
  • 12

2 Answers2

0
    data = data.replace("%", "%25"); //Data here is the javascript or html data
    data = data.replace("\\", "%27");
    data = data.replace("?", "%3f");
    WebSettings browserSettings = webView.getSettings();
    browserSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    browserSettings.setJavaScriptEnabled(true);
    browserSettings.setSupportMultipleWindows(true);
    browserSettings.setBuiltInZoomControls(true);
    browserSettings.setSupportZoom(true); 

I hope this will be helpful. Also refer https://stackoverflow.com/a/19341180/2106338

Community
  • 1
  • 1
Droidee
  • 94
  • 5
0

This may help you.....In on create do dis

super.onCreate(savedInstanceState);
    super.init();

         JavaScriptInterface myJavaScriptInterface  = new  JavaScriptInterface(this,appView);         

        appView.addJavascriptInterface(myJavaScriptInterface, "keyword");

        super.loadUrl("file:///....../index.html", 1000);

and dn create a method after on create

public class JavaScriptInterface {
            private WebView mAppView;

             private DroidGap mGap;

                public JavaScriptInterface (DroidGap gap, WebView view)
                {
                    mAppView = view;
                    mGap = gap;
                }
 public void methodname(){
}
}

and do the functionality in methodname method by calling in html using above mentioned keyword

lalith
  • 55
  • 7