Reputation: 13099
It is really handy for debugging javascript running in a WebView
that, by adding a WebChromeClient
to your WebView
(see here), any console.log()
output from the javascript is visible in the logcat.
But it seems that when your web page makes use of a service worker, any console.log()
output from the service worker does not make its way into the logcat.
Is there any way of making this happen?
Upvotes: 1
Views: 1070
Reputation: 13099
I'm not sure if this is the best way to do it, but it kinda worked for my purposes. It involves sending a message from the service worker to each of the pages under its control, with the following function in the service worker:
const sendMessageToPage = function (message) {
return self.clients.matchAll().then(clients => {
return Promise.all(clients.map(client => {
return client.postMessage('(service worker message) ' + message);
}));
});
};
Then in the controlled page (html) you just console.log
the message content when received from the service worker:
navigator.serviceWorker.onmessage = function (event) {
console.log(event.data);
};
Then the console.log
message from the page is visible in the logcat.
The main downside I could see with this is that the messages logged from the service worker are not necessarily in strict time sequence with those logged directly in the page, but that wasn't really an issue for me.
Would love to find a better way...
Upvotes: 1