Merge pull request #4098 from Half-Shot/hs/log-line-fix

Add a log function for handling complex values to the WebView client.
This commit is contained in:
Benoit Marty 2024-12-30 11:40:30 +01:00 committed by GitHub
commit 9adebb7cfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -41,6 +41,27 @@ class WebViewWidgetMessageInterceptor(
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
// Due to https://github.com/element-hq/element-x-android/issues/4097
// we need to supply a logging implementation that correctly includes
// objects in log lines.
view?.evaluateJavascript(
"""
function logFn(consoleLogFn, ...args) {
consoleLogFn(
args.map(
a => typeof a === "string" ? a : JSON.stringify(a)
).join(' ')
);
};
globalThis.console.debug = logFn.bind(null, console.debug);
globalThis.console.log = logFn.bind(null, console.log);
globalThis.console.info = logFn.bind(null, console.info);
globalThis.console.warn = logFn.bind(null, console.warn);
globalThis.console.error = logFn.bind(null, console.error);
""".trimIndent(),
null
)
// We inject this JS code when the page starts loading to attach a message listener to the window.
// This listener will receive both messages:
// - EC widget API -> Element X (message.data.api == "fromWidget")