Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Four things I learned building a double-click translator extension in Manifest V3

We make LingvoBerry, a reading app for language learners. Our readers asked for the same word card they use in our books on any website, so we built a Chrome extension: double-click a word, see its meaning in that senten

We make LingvoBerry, a reading app for language learners. Our readers asked for the same word card they use in our books on any website, so we built a Chrome extension: double-click a word, see its meaning in that sentence, its CEFR level and its pronunciation, and save it to flashcards. Here are four decisions that were less obvious than they looked.

1. Draw the card in a Shadow DOM, or every site will restyle it

A content script that injects a div inherits whatever the page's CSS says about div, button and font-size. The fix is a host element with an attached shadow root: the page's selectors stop at the boundary, and our stylesheet stays ours. One more benefit: the same card.js draws the card on our own landing page, so the demo and the real thing never drift apart.

2. The page's lang attribute lies; the sentence doesn't

<html lang="en"> is a template default on half the web. Spanish articles on English-language templates are common, and a multilingual page can switch language paragraph by paragraph. We run chrome.i18n.detectLanguage on the sentence around the word and trust it when it is reliable; the lang attribute is only the fallback. A word in another alphabet than the detected language is checked on its own or left alone. It also lets the extension stay silent on pages in the reader's own language, which matters more than any feature: an extension that pops up where you don't need it gets uninstalled.

3. Settle the permissions while the user count is small

Adding a permission that shows a warning in an update disables the extension for every existing user until they accept it. So we settled the list while we had few users: activeTab, contextMenus, scripting, storage, alarms, sidePanel, plus access to sites, asked at install, because a translator that works on one tab at a time is not what people install it for. After that the list is frozen, and a test fails the build if anyone adds to it.

4. Translate the sentence in the reader's browser

The word's meaning depends on the sentence, so the sentence needs translating too. Rather than send the sentence to our server and translate it there, the extension asks Google Translate for it from the reader's own browser and passes that translation to our server as context for the word. That translation is never cached on our side, and the extension sends the sentence to Google only when the reader looks a word up.

The result

A small card in 14 languages, with words saved together with their sentences and reviewed with spaced repetition in a side panel. The extension is LingvoBerry for Chrome, if you want to see these choices in practice. If you build extensions: how do you handle language detection on pages that mix languages?

This article was drafted with AI assistance and checked against our code by the team.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.