Skip to content

Instantly share code, notes, and snippets.

@NoahZu
Created June 12, 2017 07:01
Show Gist options
  • Select an option

  • Save NoahZu/f691c0b64c9fe17e5a35451ef940f4c2 to your computer and use it in GitHub Desktop.

Select an option

Save NoahZu/f691c0b64c9fe17e5a35451ef940f4c2 to your computer and use it in GitHub Desktop.
获取js执行后的网页源码的一种方式
public class RunJsBaseFragment extends Fragment {
private WebView webView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(getContext());
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
}
class MyJavaScriptInterface
{
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String html)
{
// 注意啦,此处就是执行了js以后 的网页源码
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment