我开发了一个应用程序来分析网络流量,同时播放youtube视频。它使用chrome.webRequest,我使用onHeadersReceived事件计算流量。
我希望使用服务工作者进行同样的操作,以便使应用程序独立于浏览器。我获取服务工作者的事件,但它不起作用。
有什么建议我可以继续吗?
发布于 2016-01-08 17:04:30
好的,广义的想法是侦听获取事件,提取您需要的信息,并允许请求到达网络。在Service:https://serviceworke.rs/api-analytics.html中有一个https://serviceworke.rs/api-analytics.html,但是相关的代码在这里(在烹饪手册中还有注释源 ):
self.onfetch = function(event) {
event.respondWith(
// Log the request…
log(event.request)
// …and then actually perform it.
.then(fetch)
);
};
// Post basic information of the request to a backend for historical purposes.
function log(request) {
var returnRequest = function() {
return request;
};
var data = {
method: request.method,
url: request.url
};
return fetch(LOG_ENDPOINT, {
method: 'POST',
body: JSON.stringify(data),
headers: { 'content-type': 'application/json' }
})
.then(returnRequest, returnRequest);
}https://stackoverflow.com/questions/34675237
复制相似问题