How to detect that the Chrome tab is broken

I am writing an extension to register the use of Facebook. I found that even if the facebook tab crashed, the timer still counts, trying to fix it. According to the document , such an event does not exist.

Is there an API for detecting tab failure or crash event?

+8
google-chrome google-chrome-extension
source share
2 answers

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

This API is available only to users on the developer's channel , so if you want to make the extension widely available to everyone, then you need to use an alternative method. You can create a script content that creates a message port via chrome.runtime.connect , and in onDisconnect use chrome.tabs.sendMessage or chrome.tabs.executeScript to check if the tab is alive: If the tab no longer exists, then chrome.runtime.lastError will be installed and an error message will be displayed.

+4
source share

Here's how I managed to detect a page drop in my Chrome extension:

  • the background script should send a β€œcheck” message to the content script at intervals, and the content script should return a response.
  • If the page crashes, the response to the background script will be vague, then do what you need. 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 
0
source share

All Articles