Created
June 12, 2017 07:01
-
-
Save NoahZu/f691c0b64c9fe17e5a35451ef940f4c2 to your computer and use it in GitHub Desktop.
获取js执行后的网页源码的一种方式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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