# lautilosio/totalyoutubetimeextension architecture

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

- Interactive diagram: https://gitdiagram.com/lautilosio/totalyoutubetimeextension
- Repository: https://github.com/lautilosio/totalyoutubetimeextension

## Overview

##### Project Type and Purpose
- This is a Chrome extension that detects open YouTube tabs, calculates cumulative watch time, and displays the result to the user.
- It interacts with the YouTube Data API (as per the README instructions) and utilizes the extension’s UI (popup) to display information.
- The core functionality revolves around scanning tabs, calculating durations (including estimated finish time), and grouping video tabs.

##### File Structure
- Key files include:
- manifest.json – Defines the extension’s metadata, permissions, and other configs.
- popup.html – The UI where the extension displays information.
- popup.css – Styling for the popup interface.
- popup.js – Handles the logic for detecting YouTube tabs, calculating durations, and managing interactions (e.g., receiving the saved API Key and triggering YouTube API interactions).
- getYoutubeAPI.md – Provides instructions for obtaining a YouTube Data API Key.
- The minimal file structure indicates that the extension logic is mostly contained within the popup.js and its associated HTML/CSS. There might be integration with Chrome’s tabs API to scan open YouTube pages.

##### Main Components
a. Browser / Chrome Extension Container:
- manifest.json (defines app’s metadata and permissions).
- The extension runtime context where the logic executes.
b. Popup UI Component:
- Contains popup.html (structure) and popup.css (presentation).
- Represents the user interface that the user interacts with in the browser.
c. Extension Logic / Background Process:
- Core functions implemented in popup.js. It handles:
- Accessing the list of open tabs via the Chrome API.
- Filtering for YouTube video tabs.
- Calculating the total watch time.
- Grouping tabs and computing the estimated finish time.
d. External YouTube Data API:
- Shown as an external system that the extension connects to once the user inputs their API Key.
- This component will feed video metadata (such as duration if further queries beyond tab detection are needed) to the extension.

##### Relationships and Interactions
- From the Browser/Extension Container to the Popup UI to illustrate that when a user accesses the extension, it displays the popup.
- From Popup UI to Extension Logic (popup.js) to denote that user actions (e.g., entering the API Key) and automatic events (like scanning open tabs) trigger logic processing.
- From Extension Logic to the Chrome Tabs API to indicate that it scans open tabs.
- From Extension Logic to the External YouTube Data API – when a valid API Key is provided, the system makes outbound API calls for additional video information.
- To also denote the feedback loops: calculated data being returned to the popup UI for display.

##### Architectural Patterns and Design Principles
- A modular design: the interface layer is clearly separated from the core logic.
- This structure lends itself to an MVC-like pattern where:
- The View – represented by popup.html and popup.css
- The Controller/Logic – represented by popup.js handling data transformation, API calls, and communication with Chrome’s APIs.

- That each component (e.g., “Popup UI”, “Extension Logic”, “Chrome Tabs API”, “External YouTube Data API”) is clearly labeled.
- Data flow from the Popup UI to the logic layer.
- Storage or retrieval of API keys.
- Communication between the extension logic and external APIs.

## Diagram (Mermaid)

```mermaid
flowchart TD

    subgraph "Browser/Extension Container"
        M["manifest.json"]:::internal
        R["Extension Runtime"]:::internal
    end

    subgraph "UI Layer"
        H["popup.html"]:::ui
        C["popup.css"]:::ui
    end

    subgraph "Extension Logic"
        P["popup.js"]:::logic
    end

    subgraph "External Services"
        Y["YouTube Data API"]:::external
        T["Chrome Tabs API"]:::external
    end

    M -->|"defines"| R
    R -->|"loads_UI"| H
    H -->|"styled_by"| C
    H -->|"user_input/APIKey"| P
    P -->|"scans_tabs"| T
    P -->|"fetches_video_details"| Y
    P -->|"returns_calculated_time"| H

    click M "https://github.com/lautilosio/totalyoutubetimeextension/blob/main/manifest.json"
    click H "https://github.com/lautilosio/totalyoutubetimeextension/blob/main/popup.html"
    click C "https://github.com/lautilosio/totalyoutubetimeextension/blob/main/popup.css"
    click P "https://github.com/lautilosio/totalyoutubetimeextension/blob/main/popup.js"
    click Y "https://github.com/lautilosio/totalyoutubetimeextension/blob/main/getYoutubeAPI.md"

    classDef internal fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef ui fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef logic fill:#FFFF99,stroke:#000,stroke-width:2px;
    classDef external fill:#FFB6C1,stroke:#000,stroke-width:2px;
```
