WebView does not provide a direct API and needs to be adapted with JavaScript.
This article attempts to summarize one of the simplest and most elegant codes.
1. Create a JavaScript interface class first:
class myjavascriptinterface { @JavascriptInterface @SuppressWarnings ("unused") publicvoid processhtml(String html) { // Handle HTML source here }}
Note the bold function name processhtml, which is used in the 2nd step.
For a detailed description of the @JavascriptInterface , see: http://blog.csdn.net/z82367825/article/details/52187921
2. Register the JavaScript interface just now:
WebSettings settings =webview.getsettings (); //This method requires JavaScript to be enabledSettings.setjavascriptenabled (true); //register the interface class you just had with the JavaScript interface named htmlout Webview.addjavascriptinterface (NewMyjavascriptinterface (), "Htmlout"); //The webviewclient must be set before LoadurlWebview.setwebviewclient (Newwebviewclient () {@Override Public voidonpagefinished (WebView view, String URL) {//Here you can filter the URLWebview.loadurl ("javascript:Htmlout.processhtml(Document.documentElement.outerHTML); "); } }); //Start loading URLsWebview.loadurl (URL);
Source: Https://stackoverflow.com/questions/2376471/how-do-i-get-the-web-page-contents-from-a-webview
Android: Get webview HTML source of the current page