How sangtacviet/stv-manga-translator works
Project Type and Purpose
• This is a front-end–only Tampermonkey userscript named “stv-manga-translator.”
• Purpose: when you browse supported manga/manhua/manhwa websites, it automatically detects text elements on the page and replaces them with translated text.
File Structure Analysis
• Top level:
• package.json & build.js → Node-based build pipeline (bundles/minifies)
• README.md → high-level description
• src/
• base_userscript.js → userscript metadata block (@name, @match, @require, etc.) and core injection logic
• header.js → common constants (metadata, site‐matching patterns)
• class.js → core classes/interfaces (Plugin interface, translation engine wrapper, DOM utilities)
• plugins/ → one folder per supported site (e.g., comic_naver_com.js implements site‐specific selectors, parsing rules, translation injection)
README Insights
• Confirms it’s a browser userscript for automated manga translation.
Core Components
a. Runtime Components
1. Tampermonkey Container (Browser Extension)
2. Injected Userscript (final bundled script)
3. Core Orchestrator (code from base_userscript.js + class.js)
4. Plugin Modules (each file under src/plugins)
5. Browser DOM & CSS Selectors (target pages)
6. External Translation Service API (e.g., Google Translate)
b. Build‐Time Components
1. Node.js Build Script (build.js)
2. package.json & devDependencies (e.g., bundler, minifier)
Relationships and Data Flow
• Build Phase:
• build.js reads src/*.js → applies bundler/minifier → outputs final userscript
• package.json drives npm scripts
• Runtime Phase:
1. Tampermonkey injects the bundled script when URL matches header.js patterns.
2. Userscript’s Core Orchestrator initializes.
3. Core loads the matching Plugin Module based on current host.
4. Plugin scans page DOM for text elements (panels, speech bubbles).
5. Extracted text is sent via Fetch/XHR to the external translation API.
6. API response (translated text) is returned.
7. Plugin replaces original text nodes in the DOM with translations.
Architectural Patterns and Principles
• Plugin Architecture: each site is decoupled as a separate module implementing a shared interface.
• Single Responsibility:
• Header handles metadata/matching.
• Core classes handle orchestration and API calls.
• Plugins handle site‐specific selectors and DOM mutation.
• Build Pipeline Separation: clear split between development (Node.js) and runtime (browser).
Technology & Libraries
• JavaScript (ES6 modules)
• Node.js (build tooling)
• Tampermonkey (userscript loader)
• Fetch API or XMLHttpRequest (for translation service)
Detailing Level
• Header.js feeding the Core with site-matching rules.
• For each plugin (e.g., comic_naver_com), note its domain pattern and DOM selectors.
• HTTP request/response cycle with external API.