我已经尝试过在 WebView 中使用 JavaScriptInterface。但它不起作用。如何从 XWalkView 获取 HTML 内容?
通过这种方式,我可以从 WebView 获取 HTML:
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);
}
}
}
我对 XWalkView 进行了同样的尝试,但没有成功。如何从 XWalkView 获取 HTML 源代码?