Custom Translation Styling (CSS)
Relevant source files
The following files were used as context for generating this wiki page:
- src/entrypoints/options/pages/translation/custom-translation-style/index.tsx
- src/entrypoints/options/pages/translation/custom-translation-style/css-editor.tsx
- src/assets/styles/custom-translation-node.css
- src/entrypoints/options/pages/translation/custom-translation-style/style-preview.tsx
- src/utils/host/translate/decorate-translation.ts
- src/locales/en.yml
- src/utils/config/tests/example/v030.ts
The Custom Translation Styling feature allows users to visually distinguish translated text from the original content on a webpage. It provides a flexible system that supports both a selection of predefined styles and the application of fully custom CSS, enabling a personalized reading experience.
This system is managed through the "Translation" section of the extension's options page. Users can select a preset or enable a CSS editor to input their own styles, which are then dynamically applied to translated content on web pages.
Sources: src/locales/en.yml:208-210, src/entrypoints/options/pages/translation/custom-translation-style/index.tsx
Configuration
The styling behavior is controlled by the translationNodeStyle object within the main application configuration.
Sources: src/utils/config/tests/example/v030.ts:114-118
| Field | Type | Description |
|---|---|---|
preset | string | The name of the selected preset style. Examples include default, blur, blockquote, weakened, dashedLine, border, textColor, background. |
isCustom | boolean | A flag that indicates whether custom CSS is enabled. If true, the customCSS field is used. |
customCSS | string | null | A string containing the user-defined CSS rules to be applied. It is null if custom CSS is not used. |
Sources: src/utils/config/tests/example/v030.ts:114-118, src/locales/en.yml:211-219
Architecture and Flow
The styling system's flow begins with user configuration in the options page and ends with the dynamic application of styles to translated elements in a content script.
The diagram below illustrates the high-level data flow for applying translation styles.
Style Application
The core logic for applying styles resides in the decorateTranslationNode function. This function is called for each translated HTMLElement and is responsible for adding the necessary attributes or injecting CSS to render the desired style.
Sources: src/utils/host/translate/decorate-translation.ts
decorateTranslationNode Logic
The function follows a clear decision path based on the user's configuration.
This diagram shows the logic within the decorateTranslationNode function.
Sources: src/utils/host/translate/decorate-translation.ts:13-52
A key part of this system is the data-read-frog-custom-translation-style attribute, which is added to the translated element. Its value is either the name of a preset or 'custom', allowing CSS rules to target the translated nodes.
Sources: src/utils/host/translate/decorate-translation.ts:8, 18, 51
Preset Styles
When a preset style is selected, decorateTranslationNode sets the data-read-frog-custom-translation-style attribute to the name of the preset (e.g., blur). The corresponding CSS rules are defined in custom-translation-node.css and are loaded by the content script.
Example of preset styles from custom-translation-node.css:
/* src/assets/styles/custom-translation-node.css:2-10 */
[data-read-frog-custom-translation-style='blur'] {
filter: blur(3px);
transition: filter 0.2s ease-in-out;
}
[data-read-frog-custom-translation-style='blur']:hover {
filter: blur(0);
}
Sources: src/assets/styles/custom-translation-node.css:2-10, src/utils/host/translate/decorate-translation.ts:51
Custom CSS Injection
If isCustom is true and customCSS is provided, the system injects the user's CSS into the active page. This is done using a performance-oriented approach:
- Constructable Stylesheets: The primary method uses the
CSSStyleSheetAPI. A single stylesheet is created and reused. Its content is updated viareplace(), which is more efficient than manipulating DOM elements. The CSS text is cached to prevent redundant updates if the style hasn't changed. - Fallback
<style>Tag: For older browsers that do not support Constructable Stylesheets, a fallback mechanism injects a<style>element with the IDread-frog-custom-css-markerinto the document's<head>.
Sources: src/utils/host/translate/decorate-translation.ts:16-46
Options Page UI
The user interface for this feature is composed of several React components that work together to provide a seamless configuration experience.
Sources: src/entrypoints/options/pages/translation/custom-translation-style/index.tsx
CustomTranslationStyle Component
This is the main component that orchestrates the UI. It contains:
- A
RadioGroupto select from the available preset styles. - A
Switchto toggle between preset styles and custom CSS. - It conditionally renders the
CSSEditorandStylePreviewcomponents based on the user's selection.
Sources: src/entrypoints/options/pages/translation/custom-translation-style/index.tsx
CSSEditor Component
This component provides the editor for user-defined styles.
- It uses
CSSCodeEditorfor the input field. - Live Validation: It validates the CSS syntax in real-time using a debounced input value (
useDebouncedValuewith a 500ms delay). ThelintCSSutility function performs the syntax check. - Length Check: It ensures the CSS does not exceed
MAX_CUSTOM_CSS_LENGTH. - Save State: The "Save" button is dynamically enabled or disabled based on whether the CSS is valid and has unsaved changes.
- It displays contextual validation messages, such as "Validating...", "CSS has syntax errors", "All changes saved", etc.
Sources: src/entrypoints/options/pages/translation/custom-translation-style/css-editor.tsx
StylePreview Component
This component gives immediate visual feedback.
- It renders a sample text block.
- A
useEffecthook callsdecorateTranslationNodeto apply the currently configured style (either preset or custom) to the preview element whenever the style configuration changes. - When custom CSS is active, it offers additional inputs to change the preview text, language, and text direction (
ltr/rtl) to help users test their styles in different scenarios.
Sources: src/entrypoints/options/pages/translation/custom-translation-style/style-preview.tsx