Extracting HTML from a WebView


Here’s another Android WebView tutorial for those of you who are looking for a way to get the source code of a page loaded in a WebView instance.

This example is a bit more complicated than previous ones, so let me explain it step by step:

  • First, a class called MyJavaScriptInterface is defined. It implements a single public method showHTML() which displays a dialog with the HTML it receives as a parameter.
  • Then, an instance of this class is registered as a JavaScript interface called HTMLOUT. The showHTML() method can now be accessed from JavaScript like this: window.HTMLOUT.showHTML(‘…’)
  • In order to call showHTML() when the page finishes loading, a WebViewClient instance which overrides onPageFinished() is added to the WebView. When the page finises loading, this method will inject a piece of JavaScript code into the page, usingthe method I described in an earlier post.
  • Finally, a web page is loaded.
  1. final Context myApp = this;  
  3. /* An instance of this class will be registered as a JavaScript interface */  
  4. class MyJavaScriptInterface  
  5. {  
  6.     @SuppressWarnings("unused")  
  7.     public void showHTML(String html)  
  8.     {  
  9.         new AlertDialog.Builder(myApp)  
  10.             .setTitle("HTML")  
  11.             .setMessage(html)  
  12.             .setPositiveButton(android.R.string.ok, null)  
  13.         .setCancelable(false)  
  14.         .create()  
  15.         .show();  
  16.     }  
  17. }  
  19. final WebView browser = (WebView)findViewById(R.id.browser);  
  20. /* JavaScript must be enabled if you want it to work, obviously */  
  21. browser.getSettings().setJavaScriptEnabled(true);  
  23. /* Register a new JavaScript interface called HTMLOUT */  
  24. browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");  
  26. /* WebViewClient must be set BEFORE calling loadUrl! */  
  27. browser.setWebViewClient(new WebViewClient() {  
  28.     @Override  
  29.     public void onPageFinished(WebView view, String url)  
  30.     {  
  31.         /* This call inject JavaScript into the page which just finished loading. */  
  32.         browser.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");  
  33.     }  
  34. });  
  36. /* load a web page */  
  37. browser.loadUrl("https://lexandera.com/files/jsexamples/gethtml.html");  

Unfortunately, this approach suffers from a major security hole: if your JavaScript can call showHTML(), then so can JavaScript from every other page that might get loaded into the WebView. Use with care.




Список тегов пуст.


В этой рубрике нет статей.