0

I have read several topics here on SO about this, at first all of them seem related but than when i read the "solution" code i cannot understand where the String that keeps the code is.

What i want to do is load a local HTML file, than modify it with some javascript. And after i have modified it i would like to either replace the unmodified HTML file with the modified HTML file, or create a new HTML file from the modified HTML. So after this process i would have the modified HTML file saved on the users SD card.

I would have loved it if there where a functions like this:

String htmlContent = myWebView.getSouce();

Than i could just create an HTML file from that String and save it to the sd card.

This is my code so far.

        final WebView webview = (WebView)rootView.findViewById(R.id.webView);
        webview.getSettings().setJavaScriptEnabled(true);

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url)
            {
                /* This loads a javascript to the Html file and changes its design*/
                view.loadUrl("javascript:(function() { "+
                        "Some Modifications to the test.html file"+
                        "})()");
                //Now when i have modified the above test.html i would like 
//to get the modified HTML (The HTML now displaying in my webview).
//So i was hoping i could write something like this:
// String htmlContent = view.getSouce(); 

            }
        });

        webview.loadUrl("file:///android_asset/test.html");
user3711421
  • 1,658
  • 3
  • 20
  • 37
  • possible duplicate of [how to get html content from a webview?](http://stackoverflow.com/questions/8200945/how-to-get-html-content-from-a-webview) – tachyonflux Dec 12 '14 at 17:04
  • I have read that one and the answers as well, and none of the answers gives the webview content back as a string. – user3711421 Dec 12 '14 at 17:11
  • Basically, it's a security risk, so you have to get the html in js and pass it back to android via a js interface. – tachyonflux Dec 12 '14 at 17:18

1 Answers1

0

If you don't understand javascript interfaces:

tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • I still dont understand how to get the modified source to a string. How should i use the interface to retrive the source? – user3711421 Dec 12 '14 at 17:55
  • Aha, I think i got it. Under where i load the script that makes the modifications i need to load another script: webview.loadUrl("javascript:window.HtmlViewer.showHTML" + "(''+document.getElementsByTagName('html')[0].innerHTML+'');"); .. But wouldnt that cause the a double load. hmm. Anyway its seems like it works. – user3711421 Dec 12 '14 at 18:00
  • yeah, you can probably do `document.documentElement.outerHTML` instead. It doesn't reload the page, you can do this on most any browser to execute js. – tachyonflux Dec 12 '14 at 18:03