scatmoi
scatmoi

Reputation: 2018

Is there a way to catch Web console errors?

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).

Upvotes: 8

Views: 2177

Answers (3)

Omkar
Omkar

Reputation: 3100

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));

Upvotes: 0

Sofiane
Sofiane

Reputation: 3

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      

Upvotes: 0

Kristian
Kristian

Reputation: 354

Do you have an example of a website?

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

Upvotes: 6

Related Questions