24 days ago

How can a message be sent to a non-injected tab via a chrome-extension while disregarding the "caught (in promise) Error" message?

The given scenario involves an options page where the user can add or delete URLs to block. When the user clicks the save button, the updated list of URLs is saved in storage. On the popup page, when the user clicks the start button, all the URLs in storage are blocked by overriding innerHtml. On stop, all the injected tabs receive a stop message, restoring the original HTML. Also, when the user clicks on delete a URL and saves it on the options page, a stop message is sent to all the tabs, irrespective of whether they are injected or not. This is done as it is not known which URL existed before and which have been removed. While sending a message to all the tabs, an error occurs for the tabs that are not injected. The task is to catch (ignore) the error to prevent it from being shown as an error on the extensions page. If not possible, the tabs that have been injected need to be figured out in background.js, and the message should be sent to those.
24 days ago
Verified Answer
Yes, there is a way to catch and ignore this error. You can wrap the line that sends the message inside a try...catch block. Here's an example:
tabs.forEach((tab) => {
  try {
    const resp = chrome.tabs.sendMessage(, {type : 'StopTimer'});
  } catch (err) {
    console.log('Error sending message to tab',, err);
By wrapping the line that sends the message inside a try...catch block, any errors that occur while sending the message will be caught and logged to the console, but the code will not throw an unhandled exception.