<?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: Mazhar Mahmood </title>
    <description>The latest articles on DEV Community by Mazhar Mahmood  (@tokenwiseme).</description>
    <link>https://dev.to/tokenwiseme</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%2F4256908%2F7c6148a8-7a52-4906-902c-9a4a47d4dd17.png</url>
      <title>DEV Community: Mazhar Mahmood </title>
      <link>https://dev.to/tokenwiseme</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tokenwiseme"/>
    <language>en</language>
    <item>
      <title>Writing a Chromium content script that survives the site changing under you</title>
      <dc:creator>Mazhar Mahmood </dc:creator>
      <pubDate>Mon, 23 Nov 2026 08:06:07 +0000</pubDate>
      <link>https://dev.to/tokenwiseme/writing-a-chromium-content-script-that-survives-the-site-changing-under-you-4hba</link>
      <guid>https://dev.to/tokenwiseme/writing-a-chromium-content-script-that-survives-the-site-changing-under-you-4hba</guid>
      <description>&lt;p&gt;Most browser extensions do not fail because the logic is wrong. They fail because the page changed. A class name got hashed differently after a deploy, a container moved one level up, and the button that worked on Friday does nothing on Monday.&lt;/p&gt;

&lt;p&gt;Token Wise puts a live cost estimate next to the prompt box on the AI sites people already use. That means running code inside pages I do not control, on a release schedule I do not control. Here is what that taught me about content scripts that survive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop building on class names
&lt;/h2&gt;

&lt;p&gt;The obvious way to find a prompt box is a selector. That selector is usually built from class names, and class names in a modern frontend are compiled, scoped and meaningless. They change when someone refactors a component, and nothing about the change is announced.&lt;/p&gt;

&lt;p&gt;What lasts longer is intent the page has to expose for its own reasons: &lt;code&gt;aria-label&lt;/code&gt;, &lt;code&gt;role&lt;/code&gt;, &lt;code&gt;data-testid&lt;/code&gt;, a &lt;code&gt;placeholder&lt;/code&gt;, the &lt;code&gt;contenteditable&lt;/code&gt; attribute, and position relative to elements that cannot move, like the send button. An accessibility label survives a redesign because removing it breaks the site for screen readers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write a resolver, not a selector
&lt;/h2&gt;

&lt;p&gt;Instead of one selector that either matches or does not, run several strategies and score the candidates they return. Each strategy is cheap, and the score picks the element that looks most like a composer.&lt;/p&gt;

&lt;p&gt;The important part is what happens when nothing scores well enough: attach nothing. A badge stuck to the wrong element is worse than no badge, because the user sees a wrong number and no reason to doubt it. Missing UI is a bug report. Confidently wrong UI is a trust problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Re-run cheaply, on the events that matter
&lt;/h2&gt;

&lt;p&gt;A MutationObserver on a scoped container catches most of it, with a debounce so a streaming response does not trigger a re-scan on every token. But mutation is not the only signal. A composer can exist for a while before it is usable, and it can be swapped for a new node with identical markup.&lt;/p&gt;

&lt;p&gt;Listening for &lt;code&gt;focusin&lt;/code&gt; turned out to be more reliable than watching the tree. The moment someone clicks or tabs into the composer, the node in front of them is by definition the real one, so attach then. One listener covers navigation, remounts and lazy rendering at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Single-page navigation has no load event
&lt;/h2&gt;

&lt;p&gt;Every AI chat site is an SPA. Changing conversation does not reload the page, so there is no second chance to initialise. The content script compares &lt;code&gt;location.href&lt;/code&gt; against the last value it saw and resets when it differs.&lt;/p&gt;

&lt;p&gt;Doing this inside the content script rather than through browser events kept the extension off the &lt;code&gt;webNavigation&lt;/code&gt; permission entirely, which is one fewer alarming line in the install prompt and one fewer thing to justify in a store listing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assume the page is stricter than your test page
&lt;/h2&gt;

&lt;p&gt;The best reminder of this was Trusted Types. Assigning a string to &lt;code&gt;innerHTML&lt;/code&gt; does not degrade on a page that enforces them, it throws, and the failure shows up only on that one site. Building every node with &lt;code&gt;createElement&lt;/code&gt; and &lt;code&gt;textContent&lt;/code&gt; fixed it and removed a whole class of silent breakage.&lt;/p&gt;

&lt;p&gt;The same idea applies in reverse for your own UI: keep it in a shadow root with reset styles, so a site's global CSS cannot restyle the badge or the modal. A blunt &lt;code&gt;all: initial&lt;/code&gt; at the shadow boundary handles the rest of the inherited properties.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep the site list out of the bundle
&lt;/h2&gt;

&lt;p&gt;A store review takes days. A frontend deploy happens without warning. Shipping a fixed list of supported sites means every new platform waits for a release, and every redesign ships as a broken version until the next one.&lt;/p&gt;

&lt;p&gt;The extension fetches a small registry of platforms and their detection hints, then merges it over the bundled list. The critical detail is validation: the remote payload is checked for shape before merging, and anything malformed is discarded in favour of the bundled copy. Remote configuration trusted blindly is just a remote crash button.&lt;/p&gt;

&lt;h2&gt;
  
  
  Every network call gets a deadline
&lt;/h2&gt;

&lt;p&gt;Service workers are killed aggressively. A fetch without a timeout can leave a spinner up forever with no error to log. Every request carries an explicit abort deadline and a structured failure record, so a failure is visible in diagnostics instead of looking like a slow network. The same restraint applies to logging: settings and prompt length, never prompt text.&lt;/p&gt;

&lt;h2&gt;
  
  
  The honest limitation
&lt;/h2&gt;

&lt;p&gt;None of this makes the extension robust against being wrong about what it can see. The estimate is built from what is on screen. If a site renders only part of a long thread, virtualises a list, or replaces a conversation with a summary, the count rests on a partial view and can sit well below what the request actually carries to the model. The badge is a reading of the page, not of the wire.&lt;/p&gt;

&lt;p&gt;That is the cost of never asking for an API key, and I would take it again. If you are building inside someone else's DOM, the resolver, the focus listener and the shadow root will save you more time than any amount of selector tuning.&lt;/p&gt;

&lt;p&gt;Token Wise is free to try: &lt;a href="https://chromewebstore.google.com/detail/token-wise/akmhikhlfebjbipfbpadfaibemlkpclc" rel="noopener noreferrer"&gt;https://chromewebstore.google.com/detail/token-wise/akmhikhlfebjbipfbpadfaibemlkpclc&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Estimating AI prompt cost locally in a Chrome extension, with no API key</title>
      <dc:creator>Mazhar Mahmood </dc:creator>
      <pubDate>Sat, 21 Nov 2026 07:48:22 +0000</pubDate>
      <link>https://dev.to/tokenwiseme/estimating-ai-prompt-cost-locally-in-a-chrome-extension-with-no-api-key-92m</link>
      <guid>https://dev.to/tokenwiseme/estimating-ai-prompt-cost-locally-in-a-chrome-extension-with-no-api-key-92m</guid>
      <description>&lt;p&gt;Most people only find out what a prompt cost after the bill arrives. By then the prompt is long gone and there is no way to tell whether it was the 40 word version or the 400 word one.&lt;/p&gt;

&lt;p&gt;Token Wise is a Chromium extension that puts the estimate back at the moment of typing. This post is about the parts that were interesting to build, not a launch pitch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why local estimation
&lt;/h2&gt;

&lt;p&gt;The first decision was whether to call a tokenizer endpoint. That would have been easy and wrong. It means an API key, a server, a privacy policy covering prompt text, and a round trip on every keystroke pause. Instead the extension approximates token counts in the browser and applies published per model rates, so the number appears while typing with nothing leaving the tab.&lt;/p&gt;

&lt;p&gt;The approximation is not exact, and it does not need to be. If a prompt is heading for 900 tokens against 120, the user needs to know it is expensive, not that it is 913 rather than 906.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the count is shown
&lt;/h2&gt;

&lt;p&gt;A small badge sits above the existing prompt box on the AI chat sites people already use. It is deliberately not a separate app: no new interface to learn, no prompts to copy across. Colour coding is coarse on purpose, low, medium or high, because a precise figure invites arguments about the last two percent and hides the point.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rewrite
&lt;/h2&gt;

&lt;p&gt;Showing a cost is half the job. The other half is making the cheaper version take one click. Optimize sends the prompt over HTTPS to a rewriting service and returns a shorter, clearer version, then shows the original and the rewrite side by side with the token difference before anything is sent. Four presets cover Balanced, Shorter, Clearer and More specific, and a keyboard shortcut does the same without leaving the keyboard.&lt;/p&gt;

&lt;p&gt;Two details matter for trust:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The sent prompt is not stored or logged.&lt;/li&gt;
&lt;li&gt;There is a local only mode that keeps everything on the device and never makes the call at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What the extension does not do
&lt;/h2&gt;

&lt;p&gt;It cannot see your actual provider bill. It shows an estimate from published rates, not the amount you will be charged, because there is no billing access and no OAuth handshake with the model vendors. That is a real limitation and worth stating plainly rather than burying in a tooltip.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would do differently
&lt;/h2&gt;

&lt;p&gt;Build the history panel earlier. Having the last hundred prompts with CSV export turned out to be more useful than expected, for spotting the handful of habitual prompts that eat most of the budget, and it is the kind of thing that is easy to defer and hard to retrofit.&lt;/p&gt;

&lt;p&gt;If you are working on anything similar, the local estimate plus published rates combination is worth trying before reaching for an API. It is cheaper, it is faster, and it removes the entire class of conversations about where prompt text is stored.&lt;/p&gt;

&lt;p&gt;Token Wise is free to try: &lt;a href="https://chromewebstore.google.com/detail/token-wise/akmhikhlfebjbipfbpadfaibemlkpclc" rel="noopener noreferrer"&gt;chromewebstore.google.com/detail/token-wise/akmhikhlfebjbipfbpadfaibemlkpclc&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>javascript</category>
      <category>chrome</category>
    </item>
  </channel>
</rss>
