# killergerbah/asbplayer architecture

> Architecture diagram of the GitHub repository killergerbah/asbplayer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/killergerbah/asbplayer
- Repository: https://github.com/killergerbah/asbplayer

## Overview

##### Project Type and Purpose
- asbplayer is primarily a browser-based PWA plus a browser extension.
- It lets language-learners load and control subtitles on streaming/local video, extract subtitles and media, and create Anki flashcards via AnkiConnect or an optional WebSocket proxy.

##### Main Components
A. Shared Library (common/)
- Contains hooks, services, UI components, subtitle readers/encoders, utilities.
- Published as an npm workspace, consumed by both client and extension.
B. Client Web App (client/)
- A React + Vite PWA served at https://killergerbah.github.io/asbplayer.
- Offers offline support (service worker), video + subtitle rendering, flashcard UI, settings UI.
C. Browser Extension (extension/)
- Manifest, content scripts (page.ts), background scripts (background.ts), controllers, handlers, services, popup/side-panel UI.
- Injects overlay UIs onto streaming sites, captures media/screenshot/audio, stores settings in chrome.storage, communicates with content scripts via chrome.runtime messaging.
D. Local WebSocket/HTTP Proxy (scripts/web-socket-server/)
- A small Go server that speaks the asbplayer WebSocket protocol and proxies commands to AnkiConnect (HTTP).
- Also exposes CLI scripts for load-subtitles and seek.
E. Localization Pipeline (loc/ + scripts/loc/)
- Crowdin integration, CSV/XLIFF conversion scripts to manage translations.
F. External Services
- AnkiConnect (HTTP API) for card creation/updating.
- Streaming video sites (Netflix, YouTube, etc.) — content scripts detect video elements and subtitle tracks.
- Optional dictionary extensions (Yomitan, VocabSieve) for in-app lookups.

##### Component Relationships & Data Flows
1. User → Browser Web App
- Drag-and-drop media/subtitles → client renders video, overlays UI.
- WebSocket (or HTTP POST) ↔ Proxy Server → Proxy → AnkiConnect.
2. User → Browser Extension
- Content Script injected into target video page ↔ Background Script (chrome.runtime messaging).
- Content Script ↔ Shared common/services for subtitle parsing, audio capture.
- Background Script → AnkiConnect (HTTP) or WebSocket proxy.
3. Shared Code
- common/ modules imported by both client and extension, ensuring consistency in subtitle parsing, UI components, i18n, etc.
4. Localization
- scripts/loc pipelines pull/push translations to Crowdin, generate JSON under loc/external and loc/untranslated.
5. Media & Subtitles
- Subtitle files are Base64-encoded (in client or extension) before loading.
- PGS parser worker for certain subtitle formats.
- UI components read subtitle collections and control video playback (auto-pause, condensed/fast-forward).

##### Architectural Patterns & Principles
- Monorepo with Yarn Workspaces for shared code.
- “Controller + Handler” pattern in extension: controllers hook into DOM/events, handlers process messages/commands.
- Pub/Sub via BroadcastChannel and frame-bridge for cross-frame communication.
- Modular, reusable React components for both extension UI and website.
- Extension leverages Chrome extension API (storage, tabs, messaging) as the “backend” within the browser.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Interaction
    User(("User")):::external
    User -->|"Drag-and-drop"| Client
    User -->|"Click extension icon"| PopupUI

    %% Browser Environment
    subgraph "Browser Environment"
        direction TB
        subgraph "Client PWA"
            direction TB
            Client["Client Web App (React+Vite PWA)"]:::client
            IndexedDB[(IndexedDB)]:::storage
            LocalStorage[(localStorage)]:::storage
            Client --> IndexedDB
            Client --> LocalStorage
        end

        subgraph "Browser Extension"
            direction TB
            ContentScript["Content Script"]:::extension
            BackgroundScript["Background Script"]:::extension
            Controllers["Controllers"]:::extension
            Handlers["Handlers"]:::extension
            ServicesExt["Services"]:::extension
            PopupUI["Popup & Side-Panel UI"]:::extension
            OffscreenRecorder["Offscreen Recorder"]:::extension
            ContentScript -->|"chrome.runtime.sendMessage"| BackgroundScript
            BackgroundScript -->|"chrome.runtime.sendMessage"| ContentScript
            Controllers --> Handlers
            Handlers --> ServicesExt
            ServicesExt --> BackgroundScript
            PopupUI --> BackgroundScript
            ContentScript --> ServicesExt
            ServicesExt --> OffscreenRecorder
        end
    end

    %% Shared Library
    subgraph "Shared Library (common/)" 
        direction TB
        AnkiModule["Anki Module"]:::common
        SubtitleReader["Subtitle Reader + PGS Parser"]:::common
        SubtitleCollection["Subtitle Collection"]:::common
        AudioClip["Audio Clip & MP3 Encoder"]:::common
        ComponentsLib["UI Components Library"]:::common
        WebSocketClient["WebSocket Client"]:::common
        HooksUtils["Hooks & Utilities"]:::common
        SettingsStorage["App Settings Storage"]:::common
    end

    %% Proxy Server
    Proxy["WebSocket/HTTP Proxy Server"]:::proxy

    %% External Services
    StreamingSites(("Streaming Video Sites")):::external
    AnkiConnect(("AnkiConnect (HTTP API)")):::external
    Crowdin(("Crowdin")):::external

    %% Localization Pipeline
    subgraph "Localization Pipeline"
        direction TB
        ConversionScripts["Conversion Scripts"]:::external
        LocFiles["Generated JSON Files"]:::external
    end

    %% Connections
    Client -->|"imports common modules"| AnkiModule
    Client --> SubtitleReader
    Client --> WebSocketClient
    Client --> ComponentsLib
    Client --> HooksUtils
    Client --> SettingsStorage

    ContentScript --> SubtitleReader
    ContentScript --> AudioClip
    ContentScript --> SettingsStorage
    ContentScript --> AnkiModule

    BackgroundScript --> AnkiModule

    Client -->|"WebSocket"| Proxy
    BackgroundScript -->|"WebSocket or HTTP POST"| Proxy
    Proxy -->|"HTTP POST"| AnkiConnect

    StreamingSites -->|"Content Script injection"| ContentScript

    ConversionScripts --> Crowdin
    ConversionScripts --> LocFiles

    %% Click Events
    click Client "https://github.com/killergerbah/asbplayer/tree/main/client/"
    click ContentScript "https://github.com/killergerbah/asbplayer/tree/main/extension/src/"
    click BackgroundScript "https://github.com/killergerbah/asbplayer/tree/main/extension/src/"
    click Controllers "https://github.com/killergerbah/asbplayer/tree/main/extension/src/controllers/"
    click Handlers "https://github.com/killergerbah/asbplayer/tree/main/extension/src/handlers/"
    click ServicesExt "https://github.com/killergerbah/asbplayer/tree/main/extension/src/services/"
    click PopupUI "https://github.com/killergerbah/asbplayer/tree/main/extension/src/ui/"
    click Proxy "https://github.com/killergerbah/asbplayer/tree/main/scripts/web-socket-server/"
    click AnkiModule "https://github.com/killergerbah/asbplayer/tree/main/common/anki/"
    click SubtitleReader "https://github.com/killergerbah/asbplayer/tree/main/common/subtitle-reader/"
    click SubtitleCollection "https://github.com/killergerbah/asbplayer/tree/main/common/subtitle-collection/"
    click AudioClip "https://github.com/killergerbah/asbplayer/tree/main/common/audio-clip/"
    click ComponentsLib "https://github.com/killergerbah/asbplayer/tree/main/common/app/components/"
    click WebSocketClient "https://github.com/killergerbah/asbplayer/tree/main/common/web-socket-client/"
    click HooksUtils "https://github.com/killergerbah/asbplayer/tree/main/common/app/hooks/"
    click SettingsStorage "https://github.com/killergerbah/asbplayer/blob/main/common/app/services/app-settings-storage.ts"
    click OffscreenRecorder "https://github.com/killergerbah/asbplayer/blob/main/extension/src/offscreen-audio-recorder.ts"
    click ConversionScripts "https://github.com/killergerbah/asbplayer/tree/main/scripts/loc/"
    click LocFiles "https://github.com/killergerbah/asbplayer/tree/main/loc/external/"

    %% Styles
    classDef client fill:#D0E8FF,stroke:#0366d6,color:#0366d6
    classDef extension fill:#E0F8E0,stroke:#27a844,color:#27a844
    classDef common fill:#FFF4D0,stroke:#d69f00,color:#d69f00
    classDef proxy fill:#FFE8D0,stroke:#d15704,color:#d15704
    classDef external fill:#EEEEEE,stroke:#666666,color:#666666
    classDef storage fill:#FFFFFF,stroke:#999999,color:#444444,stroke-dasharray: 2 2
```
