javaandroidwebviewwebchromeclient

console message captured by onConsoleMessage is incomplete


I have WebView into which I'm loading a webpage, I have set the custom WebChromeClient to capture console logs but I've found it is incomplete comparing to the console in browser. The message returned by cm.message() doesnt contain all values passed to the console methods.

console.log("Test");//Test

will work but following will only display first param

console.log("test", 1);//test - expected test 1

is there any way to workaround it?


Solution

  • An interesting bug!

    You can wrap console.log in your own function that concatenates all the arguments into a single string. The JavaScript code for doing this is below:

    (function() { 
        var oldLog = console.log;
        console.log = function() { 
            oldLog.call(console, Array.prototype.slice.call(arguments).join(" "));
        }
    })();
    

    You can inject it into your page with WebView.loadUrl('javascript:...'), or WebView.evaluateJavascript.