GoogleChrome/web-vitals-extension

Spurious errors are reported when navigating to chrome:// URLs

tunetheweb opened this issue · 0 comments

Describe the bug
Navigating to chrome:// or chrome-extension:// or chrome-search:// URLs (e.g. the extensions's own options page!) leads to errors, which are particularly confusing for the developers of the extension who don't understand them

To Reproduce
Steps to reproduce the behavior.

  • Turn on the extension
  • Navigate to the options page
  • Check errors in either the Errors screen (if testing an unreleased dev install) or the Service Worker "Inspect Views"

Expected behavior

We should only load the extension on real web pages (i.e. those starting with http like https:// or http://).

We do have a check for this in one of the two event listeners:

// User has navigated to a new URL in a tab
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
const tabIdKey = tabId.toString();
if (tab.active) {
chrome.storage.local.set({[tabIdKey]: false});
} else {
chrome.storage.local.set({[tabIdKey]: true}); // tab was loaded in background
}
if (
changeInfo.status == 'complete' &&
tab.url.startsWith('http') &&
tab.active
) {

But not in the other:

// User has made a new or existing tab visible
chrome.tabs.onActivated.addListener(({tabId, windowId}) => {
getWebVitals(tabId);

This is because this event doesn't have access to the URL.

This works to catch the error:

function getWebVitals(tabId) {
  chrome.scripting.executeScript({
    target: { tabId: tabId },
    files: ['src/browser_action/vitals.js'],
  }, (result) => {
    // Catch errors such as "This page cannot be scripted due
    // to an ExtensionsSettings policy."
    let error = chrome.runtime.lastError;
    if (error && error.message &&
      !error.message.startsWith("Cannot access contents of url \"chrome") &&
      !error.message.startsWith("Cannot access a chrome:// URL")
    ) {
      console.error(error.message);
    }
  });
}

But seems a bit brittle depending on the exact wording, not to mention I'd prefer to prevent the code from executing rather than catch the exception.

You also change allow these URLs in the manifest (for pretty good reason to be honest!).

Have asked our resident extensions Dev Rel (Oliver) for advice.

Version:

  • OS w/ version: MacOS Ventura 13.3.1
  • Browser w/ version Chrome 112