The Sites switch in my Chrome extension does two jobs. It asks Chrome for access to the selected site, then sets up the scripts that read the page. Those jobs have different failure modes. A user can deny permission, or Chrome can grant it and still reject the registration. The UI has to know which step actually succeeded.

The release manifest declares the supported origins as optional and ships with an empty content_scripts array. When a user turns on tracking, the dashboard resolves the site’s domains into Chrome match patterns and requests access inside the click flow. If Chrome returns false, the dashboard stops. After a grant, it sends REGISTER_SITE_TRACKING to the background handler with the site ID and domains. The handler checks the request before passing it to the registration module.

Here is a reduced version of the registration. In the extension, TRACKING_SCRIPTS contains the ordered bundle for that site.

await chrome.scripting.registerContentScripts([{ id: `dt-${siteId}`, matches: siteOrigins, js: TRACKING_SCRIPTS, runAt: 'document_idle', allFrames: false, persistAcrossSessions: true, }]) 

The ID matters when someone enables a site again. The single-site path uses updateContentScripts when Chrome reports that the ID is already registered. The bulk path checks registered IDs, removes collisions, and registers the new batch. Both paths keep the site ID stable, so a stale registration with that ID can be replaced.

Turning a site off has one extra wrinkle. Two entries can share a host pattern. The dashboard collects the origins still used by the remaining enabled sites before it asks Chrome to remove access. It then sends an unregister message for that site’s script ID. Unregistering affects future injections. Code already injected into a loaded document has a separate lifetime, so the next navigation is an important test when changing this flow.

For a regression test, I’d enable two entries that share an origin, turn one off, and verify three things independently. The first registration disappears, the second remains, and Chrome retains the shared grant. That one case catches a bug a single-site toggle test would never see.

The example comes from Beat the Spin Tracker.

Drafted with AI from the project source and tests.