<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Noah Rafkin</title>
    <description>The latest articles on DEV Community by Noah Rafkin (@beatingthespin).</description>
    <link>https://dev.to/beatingthespin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3786569%2F4f822574-731e-4e5b-9dc0-b495a511e4f7.png</url>
      <title>DEV Community: Noah Rafkin</title>
      <link>https://dev.to/beatingthespin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/beatingthespin"/>
    <language>en</language>
    <item>
      <title>Registering a Chrome extension’s content scripts one site at a time</title>
      <dc:creator>Noah Rafkin</dc:creator>
      <pubDate>Fri, 27 Nov 2026 17:12:45 +0000</pubDate>
      <link>https://dev.to/beatingthespin/registering-a-chrome-extensions-content-scripts-one-site-at-a-time-269j</link>
      <guid>https://dev.to/beatingthespin/registering-a-chrome-extensions-content-scripts-one-site-at-a-time-269j</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The release manifest declares the supported origins as optional and ships with an empty &lt;code&gt;content_scripts&lt;/code&gt; 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 &lt;code&gt;false&lt;/code&gt;, the dashboard stops. After a grant, it sends &lt;code&gt;REGISTER_SITE_TRACKING&lt;/code&gt; to the background handler with the site ID and domains. The handler checks the request before passing it to the registration module.&lt;/p&gt;

&lt;p&gt;Here is a reduced version of the registration. In the extension, &lt;code&gt;TRACKING_SCRIPTS&lt;/code&gt; contains the ordered bundle for that site.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scripting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;registerContentScripts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`dt-${siteId}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;siteOrigins&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;TRACKING_SCRIPTS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;runAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'document_idle'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;allFrames&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;persistAcrossSessions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ID matters when someone enables a site again. The single-site path uses &lt;code&gt;updateContentScripts&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The example comes from &lt;a href="https://www.beatthespin.com" rel="noopener noreferrer"&gt;Beat the Spin Tracker&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Drafted with AI from the project source and tests.&lt;/p&gt;

</description>
      <category>abotwrotethis</category>
      <category>javascriptlibraries</category>
    </item>
  </channel>
</rss>
