vmg
vmg

Reputation: 10576

Is there any way to get access to DOM structure in Android's WebView?

Is there any way to get access to DOM structure in Android's WebView? I need it to check if some DOM object exists on page and then fire some events.

Thank you!

Upvotes: 3

Views: 5720

Answers (2)

Shilpi
Shilpi

Reputation: 508

   addJavascriptInterface(new WebviewJSInterface(), JS_INTERFACE);

   private class WebviewJSInterface
    {
        @JavascriptInterface
        public void processHTML(String output)
        {
            Log.d("log", "hello: " +output);
        }
    }

      @Override
        public void onPageFinished(WebView view, String url)
        {
            loadUrl("javascript:window. " + JS_INTERFACE + ".processHTML(document.getElementsByTagName('body')[0].innerHTML);");
            super.onPageFinished(view, url);
        }

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1007399

Not easily.

You can call loadUrl("javascript:..."), where the ... is a hunk of JavaScript code to be executed in the current Web page. If your "fire some events" are in the context of the Web page, perhaps this will suffice. If you need Java code to "fire some events", you will also need to use addJavaScriptInterface() to create a JavaScript->Java bridge object and have your loadUrl("javascript:...") code invoke it, to get your data back out into Java.

Upvotes: 6

Related Questions