Detecting when there are no Chrome Windows Open - Manifest V3

56 Views Asked by At

I have a manifest v3 chrome extension and I want to use the background.js to detect if there are no chrome windows open at all. I have the following code. It sends a notification when there are tabs and even relays the number of them. When I close all tabs however I never get the other notification confirming that they are all closed. I am using this method to determine when I should send the user a chrome notification and when I should not. I don't want to send a notification if the user has a active tab open because they will be able to see the notification on the tab. I want to only send one when they wont be able to see the notification in a tab.

function sendNotificationIfNoWindows() {
  chrome.windows.getAll({ populate: false }, (windows) => {
    if (windows.length === 0) {
      chrome.notifications.create({
        type: 'basic',
        iconUrl: chrome.runtime.getURL('icons/logo.png'),
        title: 'No Open Windows',
        message: 'There are no open Chrome windows.',
      });
    } else {
      chrome.notifications.create({
        type: 'basic',
        iconUrl: chrome.runtime.getURL('icons/logo.png'),
        title: 'No Open Windows',
        message: 'There are open Chrome windows.',
      });
    }
  });
}
setInterval(sendNotificationIfNoWindows, 5000); // Check every 5 seconds.
0

There are 0 best solutions below