Search code examples
javascriptgoogle-chromegoogle-chrome-extension

Is there a way that I can make chrome extension that reloads the page if chrome says that "The site cannot be reached"?


I am trying to make a chrome extension that reloads the page if the site cannot be reached. I know the site reload command, but I am not able to figure out the code for it. Can you please help me out here?


Solution

  • Use webNavigation API in the background script.

    background.js:

    chrome.webNavigation.onErrorOccurred.addListener(info => {
      if (!info.frameId && info.error === 'net::ERR_NAME_NOT_RESOLVED') {
        chrome.tabs.reload(info.tabId);
      }
    });