8

Using WebView, certain websites take very long time to complete page load (as in WebViewClient.onPageFinished()) and, when that happens, this is characterize by Web Console errors of the following type:

E/Web Console(1916): Unsafe JavaScript attempt to access frame with URL
  http://mobile.example.com from frame with URL 
  http://ad.doubleclick.net/adi/interactive.example.com/front_sub;sz=320x50;ord=7340930261983.
  Domains, protocols and ports must match.
  05-26 10:44:15.274: E/Web Console(1916):  at null:1

I would like to be able to catch those errors and handle them in some way. e.g. issue a message or anything relevant to my app, actual handling is irrelevant at this point to the core question:

Is there a way to catch those errors? i.e. in a way that my app can be notified?

Note: This is not a Javascript question. I am not programming a website. I am accessing an existing website whose implementation is beyond my control. This is a WebView question (currently in the Android environment, but could be in other environments which are capable of hosting WebView as well).

scatmoi
  • 1,958
  • 4
  • 18
  • 32
  • **Note:** This is not a WebView question. This is a "How do I read logs?" question. – ozbek May 26 '13 at 15:06
  • A quick search yeilds [this](http://stackoverflow.com/questions/11461650/read-logs-permission-on-jelly-bean-api-16). I am afraid, you are out of luck with this. – ozbek May 26 '13 at 15:08
  • 3
    I don't think OP wants to read the logs, I think he wants to catch the error that generated the log message, and handle it himself. – Edward Falk May 26 '13 at 15:10
  • 1
    @shoerat Reading logs of other apps (or even system logs) is a horse of different color: (1) This type of log **is** received in the context of my app. (2) I hope that I don't need to intercept log messages in order to know about this event. I am hoping for some type of exception thrown or onReceivedError() notification, similar to the one that occurs on SSL errors. Is that possible? – scatmoi May 26 '13 at 15:12
  • AFAIK there is no notification for that. The best you can do is to monitor LogCat using AsyncTask following the example code in [this SO thread](http://stackoverflow.com/q/12692103) or even better [this open-source app](http://code.google.com/p/android-logger/). Good luck. – uTubeFan May 27 '13 at 00:47

3 Answers3

6

Do you have an example of a website?

You should be able to override WebChromeClient.onConsoleMessage(ConsoleMessage consoleMessage).

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Kristian
  • 354
  • 1
  • 4
0

I suggest this example provided by google for Debugging Web Apps

Notice that only "part1" is shown when you provide a comma delimited list of paramters within the console.log function, here is a piece of javascript code to test that:

console.log("part1","part2");

The result will be:

part1 -- From line 1 of http://example.js10/test.js      
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
Sofiane
  • 3
  • 1
0

As @Kristian posted here we can do like below,

 final WebView webView = (WebView) findViewById(R.id.webview);

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); // make sure JS enabled as true

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
        Log.d(getClass().toString(), consoleMessage.message() + " -- From line "
                + consoleMessage.lineNumber() + " of "
                + consoleMessage.sourceId());
        return super.onConsoleMessage(consoleMessage);
    }
});

webView.loadUrl(getString(R.string.url_google));
Omkar
  • 3,040
  • 1
  • 22
  • 42