This is how I managed to detect a page crash in my chrome extension:
- have the background script send "check" message to the content script at an interval, and the content script must return an answer.
- if the page crashed, the response back to background script will be undefined, then do what you need to do. In my case, refresh the page.
content_script:
...
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.action == 'page_check') {
sendResponse('OK');
}
});
background.js:
...
setInterval(() => {
chrome.tabs.query({ url: `http://<your_page_defined_in_manifest>` }, function (tabs) {
if (tabs.length > 0) {
chrome.tabs.sendMessage(tabs[0].id, { action: "page_check" }, function (response) {
if (!response) {
chrome.tabs.reload(tabs[0].id);
}
});
}
});
}, 60000); //every minute