<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Chuyi]]></title><description><![CDATA[Chuyi]]></description><link>https://chuyi.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 01:51:26 GMT</lastBuildDate><atom:link href="https://chuyi.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Building a chat translator extension for WhatsApp Web: lessons from fighting a moving DOM]]></title><description><![CDATA[Web chat apps re-render constantly, class names change without notice, and a quoted message looks a lot like a new one. Here's what I learned building…]]></description><link>https://chuyi.hashnode.dev/building-a-chat-translator-extension-for-whatsapp-web-lessons-from-fighting-a-moving-dom</link><guid isPermaLink="true">https://chuyi.hashnode.dev/building-a-chat-translator-extension-for-whatsapp-web-lessons-from-fighting-a-moving-dom</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[chrome-extensions]]></category><dc:creator><![CDATA[Chuyi]]></dc:creator><pubDate>Thu, 12 Nov 2026 16:10:13 GMT</pubDate><content:encoded><![CDATA[<p>Web chat apps re-render constantly, class names change without notice, and a quoted message looks a lot like a new one. Here's what I learned building <strong>Chuyi</strong>, a small extension that puts translations under chat messages in WhatsApp Web, Telegram Web and Messenger — and lets you review your translated reply before you press Enter yourself.</p>
<p>(Chuyi is independent and not affiliated with WhatsApp, Meta or Telegram.)</p>
<hr />
<h2>The copy-paste problem</h2>
<p>I kept watching people who sell online do the same dance: copy a buyer message, switch to a translator tab, paste, read, switch back, type a reply, translate that too, paste back, hit Enter. Do that across a busy afternoon and the rhythm of the conversation is gone.</p>
<p>Chrome's built-in "Translate this page" almost works — until it also translates the compose box, the sidebar, and every button label. A chat translator needs to touch <em>message text</em>, leave the UI alone, and stay out of the send path.</p>
<p>That last point mattered. If an extension fires Enter for you, one bad translation can go out as a wrong price or date. Chuyi's rule from day one: <strong>translate the draft, show a preview, and stop. You press Enter.</strong></p>
<hr />
<h2>MutationObserver + per-site adapters</h2>
<p>WhatsApp Web, Telegram Web, and Messenger are single-page apps. Messages appear, vanish on scroll, get replaced when a quote or reaction updates, and remount when you switch chats. A one-shot <code>querySelectorAll</code> at load time misses almost everything that matters.</p>
<p>The core loop:</p>
<ol>
<li>Attach a <code>MutationObserver</code> to the chat pane (or a stable parent that survives re-renders).</li>
<li>On each batch of mutations, walk added nodes and find message containers.</li>
<li>Extract the plain text of the message body — not the whole bubble.</li>
<li>Skip anything already marked as translated.</li>
<li>Ask the translation engine, then inject a small block under the original.</li>
</ol>
<p>The hard part is not the observer — it is that each site structures messages differently. WhatsApp nests quotes, forward labels, and link previews in the same bubble; Telegram nests differently; Messenger changes often enough that brittle CSS selectors die quietly.</p>
<p>So Chuyi uses <strong>per-site adapters</strong>: a thin layer per host that answers questions the shared pipeline should not care about.</p>
<ul>
<li>Where is the scrollable chat list?</li>
<li>What node is a message bubble?</li>
<li>Which child is the primary body text?</li>
<li>Which children are quotes, forwards, link previews, or system notices?</li>
<li>Where is the compose box, and how do we read/write its value without breaking the page's own handlers?</li>
</ul>
<p>Everything above the adapter — observer scheduling, dedupe, engine selection, rendering under the bubble — stays shared. WhatsApp renames a class? Fix one adapter. Telegram moves the compose box? Fix another.</p>
<p>A few practical lessons:</p>
<ul>
<li>Prefer attributes and roles that survive redesigns over long class chains.</li>
<li>Debounce mutation handling. Chat UIs fire a lot of noise (presence, typing indicators, image placeholders).</li>
<li>Mark translated nodes so a soft re-render of the bubble does not re-request the same sentence forever.</li>
<li>Treat "chat switched" as a first-class event: reattach to the new pane, or you will translate ghost nodes.</li>
</ul>
<hr />
<h2>Not re-translating quotes, forwards, and link previews</h2>
<p>The first version translated every text node it could find. That produced three failure modes users notice immediately:</p>
<ol>
<li><strong>Quoted replies</strong> got translated again inside the new message, so a short reply looked like a wall of duplicated text.</li>
<li><strong>Forwards</strong> often carry a header plus the original body; translating the header looks broken, and translating the body twice wastes engine calls.</li>
<li><strong>Link previews</strong> pull title/description from a URL. Those strings are not part of what the person typed, and translating them can invent meaning that was never in the chat.</li>
</ol>
<p>The fix is adapter-level classification before any engine call. For each candidate message, the adapter returns the body text <em>and</em> regions to ignore. Quotes, forward labels, and preview cards are skipped. Only the primary body goes to the translator.</p>
<p>That means reading the DOM the way a human reads the bubble: indented block is context; larger block is what they just said. Getting that wrong is worse than translating nothing — the UI looks busy and untrustworthy.</p>
<p>A related trap: scrolling up remounts older messages that look "new" to the observer. Deduping by a stable message id (when available) or a hash of chat context plus body text avoids hammering the engine on every scroll.</p>
<hr />
<h2>Free engines: Google Translate vs Chrome's Translator API (138+)</h2>
<p>Chuyi's free tier talks to translation engines <strong>from the browser</strong>. Two paths:</p>
<h3>Google Translate (network)</h3>
<p>Send the text from the extension to Google Translate and render the result. Pros: broad language coverage, familiar chat quality, works when on-device APIs are missing. Cons: text leaves the machine; network latency; partial failures must fail soft (show nothing rather than a half-wrong line under a price quote).</p>
<h3>Chrome's built-in Translator API (Chrome 138+)</h3>
<p>Newer Chromium builds expose an on-device translator. When available, text can stay on the device. Pros: stronger privacy, offline once language packs are present, no round trip for short messages. Cons: depends on browser version and packs; pairs differ from the cloud service; you still need a fallback when the API or pair is missing.</p>
<p>Chuyi treats engine choice as a setting with automatic fallback: prefer on-device when the user wants it and the pair is supported; otherwise Google Translate from the browser. Neither path routes chat text through Chuyi's servers.</p>
<p>I deliberately do <strong>not</strong> add a silent "AI rewrite" hop on the free tier. Pro offers tone controls (casual / business / short) on its separate AI translation path, with the same rule: the user reviews the draft and presses Enter.</p>
<hr />
<h2>Privacy choices: no server, no analytics</h2>
<p>A chat translator sits next to some of the most sensitive text a person types all day. If that text goes through my backend "for convenience," I inherit a data problem I do not want — so I refused that design.</p>
<p>So the free tier is shaped around constraints:</p>
<ul>
<li><strong>No Chuyi server in the translation path.</strong> The browser talks to Google Translate, or to Chrome's on-device translator.</li>
<li><strong>No contact list, phone number, or chat-title scraping</strong> for translation. The extension needs the message body and enough DOM context to place the translation — not an address book.</li>
<li><strong>No analytics SDK</strong> watching which chats you open. If I cannot measure something without reading private text, I do not measure it that way.</li>
</ul>
<p>This is stricter than many assistant-style extensions, and it shows up as missing dashboards on my side. Trust is the product for a tool that lives inside WhatsApp Web.</p>
<hr />
<h2>Why it never sends messages</h2>
<p>Automation is tempting: translate, fill the box, dispatch <code>Enter</code>, ship a demo GIF. I refused that path: <strong>sending is irreversible in a way that translating is not.</strong></p>
<p>Buyers remember a wrong price; landlords remember a wrong date. A mistranslated "cancel" is a conversation event, not a UI bug. Outgoing flow:</p>
<ol>
<li>You type in your language (or theirs).</li>
<li>You hit <strong>Alt+T</strong> (or the preview control) to translate the draft in place.</li>
<li>You read the preview. Undo if needed.</li>
<li><strong>You</strong> press Enter.</li>
</ol>
<p>The extension never owns the send key. Responsibility stays with the user, store review is happier, and money-sensitive chats stay "look once, then send."</p>
<p>It also simplifies the adapter: no need to synthesize trusted input events across each site's framework. Reading and writing the compose value, plus a preview surface, is enough.</p>
<hr />
<h2>What's next + feedback ask</h2>
<p>Chuyi is a free Chrome extension, now on the <a href="https://chromewebstore.google.com/detail/ddgapahiegjpoblofkeapnojkjcefphj">Chrome Web Store</a>. Homepage: <a href="https://translate.chuyi.ai">translate.chuyi.ai</a>. Most of my testing is on WhatsApp Web; Telegram Web and Messenger are adapted — bug reports from daily users of those clients are welcome.</p>
<p>Pro offers AI rewriting (casual / business / short), still with you pressing Enter. Chrome is the only browser store it's on today.</p>
<p>If you build content-script tools against moving chat DOMs, tell me how you handle quote detection and compose-box writes. If you sell across languages and try Chuyi, tell me where under-bubble placement feels wrong — that is taste as much as engineering.</p>
<p>Thanks for reading. I'm the developer of Chuyi, so take my enthusiasm with a grain of salt, and tell me what's broken.</p>
<p><strong>Chuyi is independent and not affiliated with WhatsApp, Meta or Telegram.</strong></p>
]]></content:encoded></item></channel></rss>