admin管理员组文章数量:1026989
I'm developing a small Chrome extension for personal use on a very specific case (website automation) but I've a problem. How can I catch a network error on a background script and call a certain function.
I've implemented this method:
chrome.webRequest.onErrorOccurred.addListener(
handleNetworkError,
{urls: ["http://*/*", "https://*/*"]
});
It catches some network errors, from what I see manly ::net
errors, DNS failing, networks changed etc.
However today I noticed that HTTP errors like:
GET 522 (Origin Connection Time-out)
didn't trigger the listener, how can I make it work on those too?
I'm developing a small Chrome extension for personal use on a very specific case (website automation) but I've a problem. How can I catch a network error on a background script and call a certain function.
I've implemented this method:
chrome.webRequest.onErrorOccurred.addListener(
handleNetworkError,
{urls: ["http://*/*", "https://*/*"]
});
It catches some network errors, from what I see manly ::net
errors, DNS failing, networks changed etc.
However today I noticed that HTTP errors like:
GET https://example. 522 (Origin Connection Time-out)
didn't trigger the listener, how can I make it work on those too?
Share Improve this question edited Jan 23, 2015 at 20:55 TCB13 asked Jan 23, 2015 at 8:50 TCB13TCB13 3,1553 gold badges46 silver badges71 bronze badges2 Answers
Reset to default 4With help of the the user @Xan, I managed to e up with this:
function extractStatus(line) {
var match = line.match(/[^ ]* (\d{3}) (.*)/);
if (match)
return {code: match[1], message: match[2]};
else
return undefined;
}
chrome.tabs.query({active: true, lastFocusedWindow: true }, function(tabsArray) {
tab = tabsArray[0];
scope = {urls: ["https://example./*"], tabId: tab.id};
console.log("Listening for errors...");
// ::net errors
chrome.webRequest.onErrorOccurred.addListener(handleNetworkError, scope);
// HTTP errors
chrome.webRequest.onHeadersReceived.addListener(function(details) {
var status = extractStatus(details.statusLine);
if (!status)
return;
if (status.code.charAt(0) == '5' || status.code.charAt(0) == '4')
handleNetworkError();
}, scope);
});
I assume that from the perspective of webRequest
/ Chrome's network stack, this request actually pleted. So you need to hook to some other event, e.g.
function extractStatus(line) {
var match = line.match(/[^ ]* (\d{3}) (.*)/);
if(match) {
return {code: match[1], message: match[2]};
} else {
return undefined;
}
}
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
var status = extractStatus(details.statusLine);
if(status) {
// Do something based on status.code
}
},
{urls: ["<all_urls>"]}
);
Note that this event is optionally blocking: you can redirect the request if needed.
Obviously, this will create a lot of work for your extension; in the above snippet, the listener is not blocking, but if you do make it blocking it will slow down your Chrome considerably - only use it when absolutely necessary and filter by URL when appropriate.
I'm developing a small Chrome extension for personal use on a very specific case (website automation) but I've a problem. How can I catch a network error on a background script and call a certain function.
I've implemented this method:
chrome.webRequest.onErrorOccurred.addListener(
handleNetworkError,
{urls: ["http://*/*", "https://*/*"]
});
It catches some network errors, from what I see manly ::net
errors, DNS failing, networks changed etc.
However today I noticed that HTTP errors like:
GET 522 (Origin Connection Time-out)
didn't trigger the listener, how can I make it work on those too?
I'm developing a small Chrome extension for personal use on a very specific case (website automation) but I've a problem. How can I catch a network error on a background script and call a certain function.
I've implemented this method:
chrome.webRequest.onErrorOccurred.addListener(
handleNetworkError,
{urls: ["http://*/*", "https://*/*"]
});
It catches some network errors, from what I see manly ::net
errors, DNS failing, networks changed etc.
However today I noticed that HTTP errors like:
GET https://example. 522 (Origin Connection Time-out)
didn't trigger the listener, how can I make it work on those too?
Share Improve this question edited Jan 23, 2015 at 20:55 TCB13 asked Jan 23, 2015 at 8:50 TCB13TCB13 3,1553 gold badges46 silver badges71 bronze badges2 Answers
Reset to default 4With help of the the user @Xan, I managed to e up with this:
function extractStatus(line) {
var match = line.match(/[^ ]* (\d{3}) (.*)/);
if (match)
return {code: match[1], message: match[2]};
else
return undefined;
}
chrome.tabs.query({active: true, lastFocusedWindow: true }, function(tabsArray) {
tab = tabsArray[0];
scope = {urls: ["https://example./*"], tabId: tab.id};
console.log("Listening for errors...");
// ::net errors
chrome.webRequest.onErrorOccurred.addListener(handleNetworkError, scope);
// HTTP errors
chrome.webRequest.onHeadersReceived.addListener(function(details) {
var status = extractStatus(details.statusLine);
if (!status)
return;
if (status.code.charAt(0) == '5' || status.code.charAt(0) == '4')
handleNetworkError();
}, scope);
});
I assume that from the perspective of webRequest
/ Chrome's network stack, this request actually pleted. So you need to hook to some other event, e.g.
function extractStatus(line) {
var match = line.match(/[^ ]* (\d{3}) (.*)/);
if(match) {
return {code: match[1], message: match[2]};
} else {
return undefined;
}
}
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
var status = extractStatus(details.statusLine);
if(status) {
// Do something based on status.code
}
},
{urls: ["<all_urls>"]}
);
Note that this event is optionally blocking: you can redirect the request if needed.
Obviously, this will create a lot of work for your extension; in the above snippet, the listener is not blocking, but if you do make it blocking it will slow down your Chrome considerably - only use it when absolutely necessary and filter by URL when appropriate.
本文标签: javascriptChrome Extensions Background Script Catch Network and HTTP ErrorsStack Overflow
版权声明:本文标题:javascript - Chrome Extensions: Background Script Catch Network and HTTP Errors - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745656063a2161603.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论