GoogleChrome / web-vitals-extension

A Chrome extension to measure essential metrics for a healthy site
https://chrome.google.com/webstore/detail/web-vitals/ahfhijdlegdabablpippeagghigmibma?hl=en
Apache License 2.0
2.35k stars 105 forks source link

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

Closed tunetheweb closed 1 year ago

tunetheweb commented 1 year ago

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.

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:

https://github.com/GoogleChrome/web-vitals-extension/blob/dd624b99fc87e5df649634620dd6f93e53ff9650/service_worker.js#L53-L67

But not in the other:

https://github.com/GoogleChrome/web-vitals-extension/blob/dd624b99fc87e5df649634620dd6f93e53ff9650/service_worker.js#L72-L74

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: