Rectangle 27 87

Actually this question has many answers. Here are 2 of them :

  • This first is almost the same as yours, I guess we got it from the same tutorial.
public class TestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        final WebView webview = (WebView) findViewById(R.id.browser);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
                        "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
            }
        });

        webview.loadUrl("http://android-in-action.com/index.php?post/" +
                "Common-errors-and-bugs-and-how-to-solve-avoid-them");
    }

    class MyJavaScriptInterface {

        private Context ctx;

        MyJavaScriptInterface(Context ctx) {
            this.ctx = ctx;
        }

        public void showHTML(String html) {
            new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
                    .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
        }

    }
}

This way your grab the html through javascript. Not the prettiest way but when you have your javascript interface, you can add other methods to tinker it.

  • An other way is using an HttpClient like there.

The option you choose also depends, I think, on what you intend to do with the retrieved html...

webview.loadUrl("javascript:window.HtmlViewer.showHTML" +                         "('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");

the above methods not working.please give another method.

webview.addJavascriptInterface Only works on Jelly Beans and lower version.

Two important changes to the above code for Jellybean and later: 1. Remove "window." from the webview.loadUrl line - the javascript interface is attached differently when targeting Jellybean. 2. Put @JavascriptInterface before "public void showHTML" - this is necessary since it's a security risk not to only allow certain methods to be called.

android - how to get html content from a webview? - Stack Overflow

android android-widget webview
Rectangle 27 33

Will it work from android 2.3.3?

It works for 2.3 as well.

Works for android 4.2 and ABOVE.

android - how to get html content from a webview? - Stack Overflow

android android-widget webview
Rectangle 27 21

In KitKat and above, you could use evaluateJavascript method on webview

wvbrowser.evaluateJavascript(
        "(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",
         new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String html) {
                Log.d("HTML", html); 
                // code here
            }
    });

this is by far the easiest solution to use here

android - how to get html content from a webview? - Stack Overflow

android android-widget webview
Rectangle 27 129

I know this is a late answer, but I found this question because I had the same problem. I think I found the answer in this post on lexandera.com. The code below is basically a cut-and-paste from the site. It seems to do the trick.

final Context myApp = this;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
    @JavascriptInterface
    @SuppressWarnings("unused")
    public void processHTML(String html)
    {
        // process the html as needed by the app
    }
}

final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);

/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url)
    {
        /* This call inject JavaScript into the page which just finished loading. */
        browser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
    }
});

/* load a web page */
browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");

Beware that this might not be the raw HTML of the page; the page content may have changed dynamically through JavaScript before onPageFinished() was executed.

It's great, but calling the method browser.loadUrl in onPageFinished will cause onPageFinished to be called again. You might want to check whether it is the first call of onPageFinished or not before calling browser.loadUrl.

Thanks @Blundell It worked to me. I'd like to know how could this be implemented as a service . Since is a service without a layout and webview to store the results. Is there a way to put the data in some other object different from the webView so we can put the javascript to get the resulting html code?

String html = new Scanner(new DefaultHttpClient().execute(new HttpGet("www.the url")).getEntity().getContent(), "UTF-8").useDelimiter("\\A").next();

android - How do I get the web page contents from a WebView? - Stack O...

android android-webview
Rectangle 27 8

Android WebView is just another render engine that render HTML contents downloaded from a HTTP server, much like Chrome or FireFox. I don't know the reason why you need get the rendered page (or screenshot) from WebView. For most of situation, this is not necessary. You can always get the raw HTML content from HTTP server directly.

There are already answers posted talking about getting the raw stream using HttpUrlConnection or HttpClient. Alternatively, there is a very handy library when dealing with HTML content parse/process on Android: JSoup, it provide very simple API to get HTML contents form HTTP server, and provide an abstract representation of HTML document to help us manage HTML parsing not only in a more OO style but also much easily:

// Single line of statement to get HTML document from HTTP server.
Document doc = Jsoup.connect("http://en.wikipedia.org/").get();

It is handy when, for example, you want to download HTML document first then add some custom css or javascript to it before passing it to WebView for rendering. Much more on their official web site, worth to check it out.

android - how to get html content from a webview? - Stack Overflow

android android-widget webview
Rectangle 27 31

// intercept calls to console.log
web.setWebChromeClient(new WebChromeClient() {
    public boolean onConsoleMessage(ConsoleMessage cmsg)
    {
        // check secret prefix
        if (cmsg.message().startsWith("MAGIC"))
        {
            String msg = cmsg.message().substring(5); // strip off prefix

            /* process HTML */

            return true;
        }

        return false;
    }
});

// inject the JavaScript on page load
web.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String address)
    {
        // have the page spill its guts, with a secret prefix
        view.loadUrl("javascript:console.log('MAGIC'+document.getElementsByTagName('html')[0].innerHTML);");
    }
});

web.loadUrl("http://www.google.com");

android - How do I get the web page contents from a WebView? - Stack O...

android android-webview
Rectangle 27 5

One touch point I found that needs to be put in place is "hidden" away in the Proguard configuration. While the HTML reader invokes through the javascript interface just fine when debugging the app, this works no longer as soon as the app was run through Proguard, unless the HTML reader function is declared in the Proguard config file, like so:

-keepclassmembers class <your.fully.qualified.HTML.reader.classname.here> {
    public *; 
}

Tested and confirmed on Android 2.3.6, 4.1.1 and 4.2.1.

android - how to get html content from a webview? - Stack Overflow

android android-widget webview
Rectangle 27 4

Android will not let you do this for security concerns. An evil developer could very easily steal user-entered login information.

Instead, you have to catch the text being displayed in the webview before it is displayed. If you don't want to set up a response handler (as per the other answers), I found this fix with some googling:

URL url = new URL("https://stackoverflow.com/questions/1381617");
URLConnection con = url.openConnection();
Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
Matcher m = p.matcher(con.getContentType());
/* If Content-Type doesn't match this pre-conception, choose default and 
 * hope for the best. */
String charset = m.matches() ? m.group(1) : "ISO-8859-1";
Reader r = new InputStreamReader(con.getInputStream(), charset);
StringBuilder buf = new StringBuilder();
while (true) {
  int ch = r.read();
  if (ch < 0)
    break;
  buf.append((char) ch);
}
String str = buf.toString();

This is a lot of code, and you should be able to copy/paster it, and at the end of it str will contain the same html drawn in the webview. This answer is from Simplest way to correctly load html from web page into a string in Java and it should work on Android as well. I have not tested this and did not write it myself, but it might help you out.

Also, the URL this is pulling is hardcoded, so you'll have to change that.

android - how to get html content from a webview? - Stack Overflow

android android-widget webview
Rectangle 27 17

This is an answer based on jluckyiv's, but I think it is better and simpler to change Javascript as follows.

browser.loadUrl("javascript:HTMLOUT.processHTML(document.documentElement.outerHTML);");

android - How do I get the web page contents from a WebView? - Stack O...

android android-webview
Rectangle 27 6

Have you considered fetching the HTML separately, and then loading it into a webview?

String fetchContent(WebView view, String url) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    HttpResponse response = httpClient.execute(get);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    HttpEntity entity = response.getEntity();
    String html = EntityUtils.toString(entity); // assume html for simplicity
    view.loadDataWithBaseURL(url, html, "text/html", "utf-8", url); // todo: get mime, charset from entity
    if (statusCode != 200) {
        // handle fail
    }
    return html;
}

android - How do I get the web page contents from a WebView? - Stack O...

android android-webview
Rectangle 27 4

I managed to get this working using the code from @jluckyiv's answer but I had to add in @JavascriptInterface annotation to the processHTML method in the MyJavaScriptInterface.

class MyJavaScriptInterface
{
    @SuppressWarnings("unused")
    @JavascriptInterface
    public void processHTML(String html)
    {
        // process the html as needed by the app
    }
}

android - How do I get the web page contents from a WebView? - Stack O...

android android-webview
Rectangle 27 1

I would suggest instead of trying to extract the HTML from the WebView, you extract the HTML from the URL. By this, I mean using a third party library such as JSoup to traverse the HTML for you. The following code will get the HTML from a specific URL for you

public static String getHtml(String url) throws ClientProtocolException, IOException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpGet, localContext);
        String result = "";

        BufferedReader reader = new BufferedReader(
            new InputStreamReader(
                response.getEntity().getContent()
            )
        );

        String line = null;
        while ((line = reader.readLine()) != null){
            result += line + "\n";
        }
        return result;
    }

suppose the url obtain is reached by posting data . this method will fail.

Also what about cookies?

android - how to get html content from a webview? - Stack Overflow

android android-widget webview
Rectangle 27 1

Why not get the html first then pass it to the web view?

private String getHtml(String url){
    HttpGet pageGet = new HttpGet(url);

    ResponseHandler<String> handler = new ResponseHandler<String>() {
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            HttpEntity entity = response.getEntity();
            String html; 

            if (entity != null) {
                html = EntityUtils.toString(entity);
                return html;
            } else {
                return null;
            }
        }
    };

    pageHTML = null;
    try {
        while (pageHTML==null){
            pageHTML = client.execute(pageGet, handler);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return pageHTML;
}

@Override
public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
    mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
    webview.getSettings().setJavaScriptEnabled(true);
    WebViewClient anchorWebViewClient = new WebViewClient()
    {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            //Do what you want to with the html
            String html = getHTML(url);

            if( html!=null && !url.equals(lastLoadedURL)){
                lastLoadedURL = url;
                webview.loadDataWithBaseURL(url, html, null, "utf-8", url);
            }
}

HttpClient was deprecated in API Level 22 and removed in API Level 23. So the classes mentioned in your code cannot be imported in the java files.

android - how to get html content from a webview? - Stack Overflow

android android-widget webview
Rectangle 27 34

String html = (String) webEngine.executeScript("document.documentElement.outerHTML");

this one liner wont work if not in a worker. Will return empty html. It also won't work for a site like google.com. Won't return live DOM, only underlying html/javascript.

java - get the contents from the webview using javafx - Stack Overflow

java webview javafx-2 javafx
Rectangle 27 18

WebEngine.getDocument returns org.w3c.dom.Document, not JavaScript document which you expect judging by your code.

org.w3c.dom.Document

Note that you need to wait until the document is loaded before working with Document. This is why LoadWorker is used here:

public void start(Stage primaryStage) {
    WebView webview = new WebView();
    final WebEngine webengine = webview.getEngine();
    webengine.getLoadWorker().stateProperty().addListener(
            new ChangeListener<State>() {
                public void changed(ObservableValue ov, State oldState, State newState) {
                    if (newState == Worker.State.SUCCEEDED) {
                        Document doc = webengine.getDocument();
                        try {
                            Transformer transformer = TransformerFactory.newInstance().newTransformer();
                            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
                            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

                            transformer.transform(new DOMSource(doc),
                                    new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            });
    webengine.load("http://stackoverflow.com");
    primaryStage.setScene(new Scene(webview, 800, 800));
    primaryStage.show();
}

Your method will retrieve missing content if the site has errors html ex : I am very concerned about this topic if you have a fix please please help

java - get the contents from the webview using javafx - Stack Overflow

java webview javafx-2 javafx
Rectangle 27 19

You missed the part in the tutorial where he adds

webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);

Sets the chrome handler. This is an implementation of WebChromeClient for use in handling Javascript dialogs, favicons, titles, and the progress. This will replace the current handler.

One thing not mentioned in the tutorial are the imports: import android.webkit.WebChromeClient; import android.webkit.WebView; . You can hover over the code underlined in red and you'll get some options to fix it. The first one will be the imports.

Android: can't get javascript to work on WebView even with setJavaScri...

javascript android webview
Rectangle 27 16

JavaBridge.log()
import netscape.javascript.JSObject;

public class JavaBridge
{
    public void log(String text)
    {
        System.out.println(text);
    }
}

[...]

webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) ->
{
    JSObject window = (JSObject) webEngine.executeScript("window");
    JavaBridge bridge = new JavaBridge();
    window.setMember("java", bridge);
    webEngine.executeScript("console.log = function(message)\n" +
        "{\n" +
        "    java.log(message);\n" +
        "};");
});

@Florian can you please help here? I'm getting a null pointer exception if i declare window as JSObject

console.log()

webview - JavaFX 8 WebEngine: How to get console.log() from javascript...

webview javafx-8 javafx-webengine
Rectangle 27 16

JavaBridge.log()
import netscape.javascript.JSObject;

public class JavaBridge
{
    public void log(String text)
    {
        System.out.println(text);
    }
}

[...]

webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) ->
{
    JSObject window = (JSObject) webEngine.executeScript("window");
    JavaBridge bridge = new JavaBridge();
    window.setMember("java", bridge);
    webEngine.executeScript("console.log = function(message)\n" +
        "{\n" +
        "    java.log(message);\n" +
        "};");
});

@Florian can you please help here? I'm getting a null pointer exception if i declare window as JSObject

console.log()

webview - JavaFX 8 WebEngine: How to get console.log() from javascript...

webview javafx-8 javafx-webengine
Rectangle 27 16

JavaBridge.log()
import netscape.javascript.JSObject;

public class JavaBridge
{
    public void log(String text)
    {
        System.out.println(text);
    }
}

[...]

webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) ->
{
    JSObject window = (JSObject) webEngine.executeScript("window");
    JavaBridge bridge = new JavaBridge();
    window.setMember("java", bridge);
    webEngine.executeScript("console.log = function(message)\n" +
        "{\n" +
        "    java.log(message);\n" +
        "};");
});

@Florian can you please help here? I'm getting a null pointer exception if i declare window as JSObject

console.log()

webview - JavaFX 8 WebEngine: How to get console.log() from javascript...

webview javafx-8 javafx-webengine
Rectangle 27 16

JavaBridge.log()
import netscape.javascript.JSObject;

public class JavaBridge
{
    public void log(String text)
    {
        System.out.println(text);
    }
}

[...]

webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) ->
{
    JSObject window = (JSObject) webEngine.executeScript("window");
    JavaBridge bridge = new JavaBridge();
    window.setMember("java", bridge);
    webEngine.executeScript("console.log = function(message)\n" +
        "{\n" +
        "    java.log(message);\n" +
        "};");
});

@Florian can you please help here? I'm getting a null pointer exception if i declare window as JSObject

console.log()

webview - JavaFX 8 WebEngine: How to get console.log() from javascript...

webview javafx-8 javafx-webengine