开发者

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 ch开发者_如何学Ceck if some DOM object exists on page and then fire some events.

Thank you!


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.


   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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜