Add a log function for handling complex values to the WebView client.

This commit is contained in:
Half-Shot
2024-12-27 14:57:32 +00:00
parent a369218aeb
commit 37ebf5a2d5

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(logFn, ...args) {
logFn(
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")