Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if Chrome tab is crashed

I'm writing a extension for logging usage of Facebook. I found that even if the facebook tab crashed the timer still counting so trying to fix it. According to the doc, there seems no such event.

Is there API for detecting if the tab is crashed, or crash event?

like image 722
Robert Avatar asked Jul 04 '14 03:07

Robert


People also ask

Why does my Chrome tab say crashed?

Your computer may have run out of memory, and can't load the site while also running your apps, extensions, and programs. To free up memory: Close every tab except for the one that's showing the error message. Quit other apps or programs that are running.

Is it normal for Chrome to crash?

If your computer is low on RAM (which is often a problem due to Chrome's high memory usage), it may cause websites to crash. Try closing all tabs you're not using, pausing any Chrome downloads, and quitting any unnecessary programs running on your computer.


2 Answers

The chrome.processes.onExited event is triggered when a renderer crashes (which is a process that hosts one or more tabs).

This API is only available to users on the developer channel, so if you want to make the extension widely available for everyone, then you need to use an alternative method. You could create a content script that creates a message port via chrome.runtime.connect, and in the onDisconnect event use chrome.tabs.sendMessage or chrome.tabs.executeScript to check whether the tab is still alive: If the tab does not exist any more, then chrome.runtime.lastError will be set and indicate a communication error.

like image 104
Rob W Avatar answered Sep 29 '22 21:09

Rob W


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
like image 33
mstephano Avatar answered Sep 29 '22 20:09

mstephano