Building a Layout-Preserving Bilingual Web Translator & Streaming Batch LLM Extension in MV3

2026-11-29来源 dev.toEnglish

Most browser translation extensions suffer from three recurring UX problems:

  1. Appending translated <font> or <span> tags inline breaks compact flex navbars and buttons.
  2. Translating an entire article sentence-by-sentence with an LLM is slow and wastes tokens on repeated system prompts.
  3. Many modern extensions require accounts, subscriptions, or inject ads.

I built Modern Translator (GPL-3.0, v1.1.1) — a completely free, ad-free, no-login open-source browser extension for Chrome, Edge, and Firefox (140+) — to solve these cleanly.

1. Spatial-Budget Bilingual Layout & Ruby Rail Solver

Instead of blindly appending text beside every DOM node, the layout engine estimates available inline/block spatial budgets:

  • Regular prose paragraphs render in clean bilingual blocks.
  • Compact UI elements (navbars, buttons, badges) automatically switch to Ruby dual-line stacking so translations never overflow or wrap menus awkwardly.
  • Segments whose translation is identical to the source text (code identifiers, proper nouns, matching abbreviations) are skipped automatically.

2. Super-Streaming LLM Batch Scheduler in MV3

Inside the Manifest V3 service worker (src/background/llmBatchStream.js), adjacent viewport segments are grouped into streaming batch calls:

  • Cuts round-trip latency and slashes token overhead by sharing context across paragraphs.
  • Pairs with a global translation cache (translationCache.js) so toggling Bilingual / Original / Translation is instant.
  • Supports Google Translate, Microsoft Translator, any custom OpenAI-compatible endpoint, plus an optional community-sponsored free LLM quota out of the box.

3. Shadow DOM Isolation & Cross-Iframe Selection

Popovers and floating controls mount inside isolated Shadow DOM trees (react-shadow) so host stylesheets (GitHub, Notion, docs sites) never leak in. Chrome MV3 clipboard writes and TTS audio playback run via an offscreen document, while the Firefox target script strips offscreen and emits a compatible background.scripts manifest automatically.

4. Bonus: YouTube & Bilibili Language Classroom + Anki Wordbook

In v1.1, Modern Translator also turns video players into a language study workspace:

  • Sentence-by-sentence subtitle jump and replay (< / > shortcuts) directly inside the player.
  • AI pre-study vocabulary flashcards extracted by chapter (IPA pronunciation, CEFR level, timestamp jump, self-test mode).
  • Local Wordbook with mastery tracking and one-click TSV export compatible with Anki and Excel.

Links & Privacy

All settings, custom API keys, and your wordbook stay strictly on your device. No login required.

Feedback, issues, and PRs are very welcome!

原文:https://www.a8q9.cn/en/article/building-a-layout-preserving-bilingual-web-translator-streaming-batch-llm-extens-4ac3ac.html

相关文章