Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get html content from XWalkView?

I have tried I did in WebView using JavaScriptInterface. But it is not working. How to get HTML content from XWalkView?

In this way I could get HTML from WebView:

    public class MainActivity extends AppCompatActivity {

    WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = myWebView.getSettings();

        webSettings.setJavaScriptEnabled(true);

        myWebView.loadUrl("http://joerichard.net/api/test/generate.htm");
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");

        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                if(url.endsWith("generate.htm")){
                    myWebView.loadUrl("javascript:window.HtmlViewer.showHTML" +
                            "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
                }
            }
        });

    }

    class MyJavaScriptInterface {

        private Context ctx;

        MyJavaScriptInterface(Context ctx) {
            this.ctx = ctx;
        }

        public void showHTML(String html) {
            Document doc = Jsoup.parse(html);
        }

    }

}

I tried the same for XWalkView but have not succeded. How to get HTML source code from XWalkView?

like image 833
Joe Richard Avatar asked Mar 07 '16 13:03

Joe Richard


1 Answers

Add JavaScript Interface:

 webview.addJavascriptInterface(new test(this),"bla");

Set Client

  webview.setResourceClient(new XWalkResourceClient(webview) {

                @Override
                public void onProgressChanged(XWalkView view, int progressInPercent) {
                    super.onProgressChanged(view, progressInPercent);
                    pbLoading.setProgress(progressInPercent);
                }

                @Override
                public void onLoadFinished(XWalkView view, String url) {
                    super.onLoadFinished(view, url);
                    webview.load("javascript:window.bla.showHTML" +
                            "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"
                            ,null);
                }
            });

implement html function

class test{
        private Context ctx;

        test(Context ctx) {
            this.ctx = ctx;
        }
         @JavascriptInterface
        public void showHTML(String html) {
            new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
                    .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
        }

    }
like image 115
Ucdemir Avatar answered Oct 12 '22 16:10

Ucdemir