How filipeps/traduzir-paginas-web works
1. This is a browser extension for real-time page translation, not a standalone app or server. The architecture is split into browser-run pieces: background logic, content scripts injected into tabs, and extension UI pages for popup/options.
2. The central orchestration lives in src/background/background.js, which coordinates translation requests, engine selection, and messaging between UI and page scripts. It is backed by translationService.js for provider-specific translation calls and translationCache.js to avoid repeated remote requests. textToSpeech.js is a separate background capability for audio playback.
3. The page-level work happens in src/contentScript/. pageTranslator.js is the main DOM translator that modifies page text in place. Supporting scripts handle specific states and modes: showTranslated.js, showOriginal.js, and translateSelected.js. checkScriptIsInjected.js prevents duplicate injection, which matters because the extension modifies live pages repeatedly.
4. Content scripts are the boundary between the extension and arbitrary websites. They can read and rewrite page content, but only inside the permissions the browser grants. This is why the repo also includes CSS assets for translated/original states and a small mobile popup UI under contentScript/html and popupMobile.js.
5. The user-facing controls are in src/popup/ and src/options/. The popup subsystem contains separate entry points for translating a document, translating selected text, and an “improve translation” flow. The options page stores configuration such as engine choice, language preferences, dark mode, and behavioral settings.
6. The translation engines are abstracted as provider integrations rather than being hardwired into the UI. The repo and build artifacts show support for multiple providers and platform-specific packaging; the readme explicitly calls out Google and Yandex, while the code/build tree also includes provider-related assets for Bing, DeepL, and Libre. The background layer is the provider-agnostic boundary, while provider-specific code stays behind it.
7. Language and locale support is a first-class subsystem. All UI strings live under src/_locales/*/messages.json, and src/lib/i18n.js is the shared runtime access layer. Auxiliary scripts in extra/ validate locales and generate language metadata, which suggests localization is part of the release pipeline rather than manual maintenance.
8. Shared runtime helpers live in src/lib/. These include platform detection, config handling, browser API error normalization, language metadata, and a generic utilities module. This library layer keeps the background, popup, options, and content scripts from duplicating browser-specific glue.
9. The build and distribution structure is browser-extension oriented. src/manifest.json and src/chrome_manifest.json define extension packaging, while dist/firefox and dist/chromium hold release-specific artifacts like update manifests and registry files. gulpfile.js, polyfill.js, and the build instructions indicate a bundled build process that prepares browser-targeted outputs from the source tree.
10. There is no backend service in this repository. External communication is limited to the translation providers and any related service endpoints they require. The privacy model in the readme reflects that: the extension does not collect user data itself, but page content must be sent to the chosen translation engine to perform the translation.
11. Important architectural boundaries for a diagram are: browser UI surfaces, background coordination, page injection/DOM rewriting, shared libraries, localization data, and external translation APIs. The extension is essentially a client-side orchestrator that transforms arbitrary web pages in place while preserving the original content for toggling and recovery.
Main entry points: background.js Background · pageTranslator.js Page Translator · stuff.js Shared Lib · gulpfile.js Build Pipeline
Components 20
Browser UI
Extension pages the user interacts with directly.
- Popup UI · User-facing popup entry point for translation actions.
src/popup/popup.js - Document Flow · Popup flow for translating a whole document.
src/popup/popup-translate-document.js - Text Flow · Popup flow for translating selected text.
src/popup/popup-translate-text.js - Improve Flow · Popup flow for improving an existing translation.
src/popup/improve-translation.js - Options Page · Configuration UI for engine choice, languages, theme, and behavior.
src/options/options.js
Runtime
Injected and background code that runs inside the browser.
- Background · Central message router for translation requests, engine selection, caching, and page/UI coordination.
src/background/background.js - Translation API · Provider-agnostic layer that calls remote translation engines.
src/background/translationService.js - Cache · Avoids repeated remote translation calls for the same content.
src/background/translationCache.js - Text to Speech · Background capability for speech playback.
src/background/textToSpeech.js - Page Translator · Main content-script translator that rewrites page text in place.
src/contentScript/pageTranslator.js - Inject Guard · Prevents duplicate script injection into live pages.
src/contentScript/checkScriptIsInjected.js - Page Modes · Show translated, show original, and translate-selected flows.
src/contentScript/showTranslated.js - Mobile Popup · Small content-script popup surface for mobile interactions.
src/contentScript/popupMobile.js
Shared Core
Common helpers and data used across extension surfaces.
- i18n Runtime · Shared runtime access layer for localized strings.
src/lib/i18n.js - Shared Lib · Common browser, config, language, and utility helpers used across extension surfaces.
src/lib/stuff.js - Locales · All UI string catalogs and language metadata used by the extension.
src/_locales/en/messages.json
Build & Release
Packaging, localization tooling, and distribution artifacts.
- Manifests · Browser-extension packaging definitions for Chromium and Firefox.
src/manifest.json - Build Pipeline · Bundling and release preparation via build scripts and polyfills.
gulpfile.js - Release Artifacts · Browser-specific release outputs and update metadata.
dist/chromium/updates.xml
External Services
Third-party translation providers used through the background layer.
- Translation Providers · External engines such as Google, Yandex, Bing, DeepL, and Libre used for remote translation.
Connections 22
- Popup UI → Background: send actions · Popup initiates translation and configuration requests through background messaging.
- Document Flow → Background: request translate · Document translation flow delegates orchestration to background.
- Text Flow → Background: request translate · Selected-text flow delegates orchestration to background.
- Improve Flow → Background: refine text · Improve-translation flow routes through background coordination.
- Options Page → Shared Lib: read config · Options UI uses shared helpers for settings and platform behavior.
- Popup UI → i18n Runtime: labels · Popup UI pulls localized strings at runtime.
- Options Page → i18n Runtime: labels · Options UI uses shared localization access.
- Background → Translation API: delegate · Background hands off provider-specific translation work.
- Background → Cache: cache lookups · Background checks and stores repeated translations.
- Background → Text to Speech: audio · Background exposes speech playback capability.
- Translation API → Translation Providers: remote calls · Translation service calls out to external provider endpoints.
- Page Translator → Translation API: translate text · Content script requests translated text through the background boundary.
- Page Translator → Inject Guard: guard injection · Prevents duplicate page-script installation.
- Page Translator → Page Modes: mode changes · Translator cooperates with show-original and show-translated behavior.
- Page Modes → Mobile Popup: controls · Page mode actions can be surfaced in the mobile in-page popup.
- Background → Page Translator: messaging · Background coordinates with injected page translators in tabs.
- Background → Popup UI: status updates · Background notifies extension UI about translation state.
- Shared Lib → i18n Runtime: uses · Shared helpers provide the runtime localization layer.
- Shared Lib → Locales: loads strings · Shared runtime resolves localized message catalogs.
- Manifests → Build Pipeline: packaging · Extension manifests are assembled into browser-specific builds.
- Build Pipeline → Release Artifacts: outputs · Build process produces release bundles and update metadata.
- Build Pipeline → Manifests: consumes · Bundling is driven by extension manifest definitions.