Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript WebView:使service worker的console.log输出在logcat中可见_Javascript_Android_Webview - Fatal编程技术网

Javascript WebView:使service worker的console.log输出在logcat中可见

Javascript WebView:使service worker的console.log输出在logcat中可见,javascript,android,webview,Javascript,Android,Webview,调试运行在WebView中的javascript非常方便,通过将WebChromeClient添加到WebView(请参阅),javascript的任何console.log()输出都可以在logcat中看到 但是,当您的网页使用时,服务人员的任何console.log()输出似乎都不会进入logcat 有什么方法可以做到这一点吗?我不确定这是否是最好的方法,但它对我的目的起到了一定的作用。它涉及从服务工作者向其控制下的每个页面发送消息,服务工作者中具有以下功能: const sendMessa

调试运行在
WebView
中的javascript非常方便,通过将
WebChromeClient
添加到
WebView
(请参阅),javascript的任何
console.log()
输出都可以在logcat中看到

但是,当您的网页使用时,服务人员的任何
console.log()
输出似乎都不会进入logcat


有什么方法可以做到这一点吗?

我不确定这是否是最好的方法,但它对我的目的起到了一定的作用。它涉及从服务工作者向其控制下的每个页面发送消息,服务工作者中具有以下功能:

const sendMessageToPage = function (message) {
    return self.clients.matchAll().then(clients => {
        return Promise.all(clients.map(client => {
            return client.postMessage('(service worker message) ' + message);
        }));
    });
};
navigator.serviceWorker.onmessage = function (event) {
    console.log(event.data);
};
然后在受控页面(html)中,您只需
console.log
从服务人员处收到的消息内容:

const sendMessageToPage = function (message) {
    return self.clients.matchAll().then(clients => {
        return Promise.all(clients.map(client => {
            return client.postMessage('(service worker message) ' + message);
        }));
    });
};
navigator.serviceWorker.onmessage = function (event) {
    console.log(event.data);
};
然后,页面中的
console.log
消息在logcat中可见

我可以从中看到的主要缺点是,服务人员记录的消息不一定与直接记录在页面中的消息严格按时间顺序排列,但这对我来说并不是什么问题


我很想找到更好的方法…

你最后做了什么?你找到解决办法了吗?@ishaan我预感到一些有用的东西。。。但不确定这是否是“最佳实践”。。。在这里看到我的新答案:整洁的黑客!:)看来这可能是目前唯一的办法。