# mohit-s96/youtube_ocr_extension architecture

> Architecture diagram of the GitHub repository mohit-s96/youtube_ocr_extension, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mohit-s96/youtube_ocr_extension
- Repository: https://github.com/mohit-s96/youtube_ocr_extension

## Overview

1. **What this repo is**
   - A Chrome extension for OCR on YouTube videos.
   - It is fully client-side: no backend, no API server, no cloud OCR service.
   - The main purpose is to let a user select a region of a video frame and extract readable text from it.

2. **Extension surface**
   - The user interacts with a **popup UI** (`src/popup.html`, `src/popup.js`) and likely a keyboard shortcut.
   - The popup is the entry point for starting the crop/OCR flow, as described in the README.
   - `src/styles.css` and `src/img/` support the UI presentation and branding.

3. **Background orchestration**
   - `src/background.js` acts as the extension’s central coordinator.
   - In a Chrome extension architecture, this is typically where shortcut handling, message routing, and privileged extension actions are managed.
   - It likely bridges the popup/content script/offscreen components rather than doing OCR directly in the UI.

4. **Content script boundary**
   - `src/contentScript.js` is injected into YouTube pages.
   - Its role is to operate in the context of the web page: detect the video page, overlay/select the crop region, and send page-specific information back to the extension runtime.
   - This file forms the boundary between the extension and the YouTube DOM/video content.

5. **Offscreen processing layer**
   - `src/offscreen.html` and `src/offscreen.js` indicate a separate hidden processing context.
   - In Manifest V3, offscreen documents are commonly used for tasks that need DOM-like capabilities without a visible tab.
   - Here, that layer likely handles screenshot/canvas processing and OCR execution while staying invisible to the user.

6. **OCR engine**
   - The README explicitly says OCR is done with **TesseractJS**.
   - That means text recognition happens in-browser using WebAssembly/service-worker-style processing provided by the library.
   - This is the core compute-heavy subsystem and is isolated from the UI to keep the extension responsive.

7. **End-to-end data flow**
   - User starts the action from popup or shortcut.
   - Background script coordinates the request.
   - Content script works on the YouTube page to identify the selected video area.
   - Offscreen context receives image data/screenshot region, runs OCR via TesseractJS, and returns extracted text.
   - The result is then surfaced back to the user through the extension UI.

8. **Browser/runtime model**
   - This is a **Chrome extension** built around Manifest V3-style pieces.
   - The presence of an offscreen document suggests it relies on extension APIs rather than a traditional page-only implementation.
   - The README’s note about Manifest V2 on a separate branch confirms the current tree is the newer MV3 architecture.

9. **Build/tooling layer**
   - `webpack.common.js`, `webpack.dev.js`, and `webpack.prod.js` show that the extension is bundled with Webpack.
   - `package.json` defines the app’s dependencies and build scripts.
   - This bundling step is important because the extension is split across several runtime contexts that need to be packaged together.

10. **Static assets and packaging**
   - `assets/` and `src/img/` hold branding and UI assets such as the logo/favicon.
   - `src/manifest.json` is the extension contract that wires together popup, background, content script, offscreen page, permissions, and shortcuts.
   - The final build output is what gets loaded as an unpacked extension in Chrome.

11. **Architectural boundary summary**
   - The repo cleanly separates **UI**, **page injection**, **background coordination**, and **OCR processing**.
   - The most important architectural split is between the visible extension surfaces and the hidden processing context used to perform screenshot-based OCR without a backend.

Main entry points: [`src/background.js`](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/background.js) (Background), [`src/manifest.json`](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/manifest.json) (Manifest), [`src/contentScript.js`](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/contentScript.js) (Content script), [`src/popup.html`](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/popup.html) (Popup UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Extension runtime"]
  node_popup["Popup UI<br/>ui entry<br/>[popup.html]"]
  node_popup_js["Popup logic<br/>ui controller<br/>[popup.js]"]
  node_background["Background<br/>coordinator<br/>[background.js]"]
  node_content_script["Content script<br/>page bridge<br/>[contentScript.js]"]
  node_styles["Styles<br/>ui styling<br/>[styles.css]"]
end

subgraph group_processing["OCR processing"]
  node_offscreen_html["Offscreen page<br/>hidden runtime<br/>[offscreen.html]"]
  node_offscreen_js["Offscreen logic<br/>processing worker<br/>[offscreen.js]"]
  node_tesseract(("TesseractJS<br/>ocr engine"))
end

subgraph group_tooling["Build and packaging"]
  node_manifest["Manifest<br/>extension config<br/>[manifest.json]"]
  node_webpack_common["Webpack common<br/>bundler config<br/>[webpack.common.js]"]
  node_webpack_dev["Webpack dev<br/>bundler config<br/>[webpack.dev.js]"]
  node_webpack_prod["Webpack prod<br/>bundler config<br/>[webpack.prod.js]"]
  node_package["Package config<br/>deps and scripts<br/>[package.json]"]
  node_popup_assets[("UI assets<br/>branding assets")]
  node_assets[("Static assets<br/>branding assets")]
  node_readme["README<br/>docs<br/>[README.md]"]
end

node_popup -->|"uses"| node_popup_js
node_popup_js -->|"starts flow"| node_background
node_background -->|"injects/routs"| node_content_script
node_content_script -->|"messages"| node_background
node_content_script -->|"captures region"| node_offscreen_html
node_offscreen_html -->|"hosts"| node_offscreen_js
node_offscreen_js -->|"runs OCR"| node_tesseract
node_offscreen_js -->|"returns text"| node_background
node_manifest -->|"declares"| node_popup
node_manifest -->|"declares"| node_background
node_manifest -->|"injects"| node_content_script
node_manifest -->|"enables"| node_offscreen_html
node_manifest -->|"bundles"| node_styles
node_webpack_common -->|"shared by"| node_webpack_dev
node_webpack_common -->|"shared by"| node_webpack_prod
node_package -->|"scripts"| node_webpack_common
node_package -->|"scripts"| node_webpack_dev
node_package -->|"scripts"| node_webpack_prod
node_assets -->|"brands"| node_popup
node_popup_assets -->|"decorates"| node_popup
node_readme -->|"describes"| node_background
node_readme -->|"mentions"| node_tesseract

click node_popup "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/popup.html"
click node_popup_js "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/popup.js"
click node_background "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/background.js"
click node_content_script "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/contentScript.js"
click node_offscreen_html "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/offscreen.html"
click node_offscreen_js "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/offscreen.js"
click node_manifest "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/manifest.json"
click node_webpack_common "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/webpack.common.js"
click node_webpack_dev "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/webpack.dev.js"
click node_webpack_prod "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/webpack.prod.js"
click node_package "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/package.json"
click node_styles "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/styles.css"
click node_popup_assets "https://github.com/mohit-s96/youtube_ocr_extension/tree/release/src/img"
click node_assets "https://github.com/mohit-s96/youtube_ocr_extension/tree/release/assets"
click node_readme "https://github.com/mohit-s96/youtube_ocr_extension/blob/release/README.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_popup,node_popup_js,node_background,node_content_script,node_styles toneBlue
class node_offscreen_html,node_offscreen_js,node_tesseract toneAmber
class node_manifest,node_webpack_common,node_webpack_dev,node_webpack_prod,node_package,node_popup_assets,node_assets,node_readme toneMint
```

## Components

### Extension runtime

Visible Chrome extension surfaces and coordination logic.

- [Popup UI](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/popup.html): ui entry · User-facing popup that starts the OCR flow. · `src/popup.html`
- [Popup logic](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/popup.js): ui controller · Handles popup interactions and triggers extension actions. · `src/popup.js`
- [Background](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/background.js): coordinator · Central extension coordinator for messages, shortcuts, and orchestration. · `src/background.js`
- [Content script](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/contentScript.js): page bridge · Injected into YouTube pages to work with the video DOM and selection boundary. · `src/contentScript.js`
- [Styles](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/styles.css): ui styling · Shared styling for the popup and extension UI. · `src/styles.css`

### OCR processing

Hidden processing context and OCR engine.

- [Offscreen page](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/offscreen.html): hidden runtime · Invisible document used for background processing tasks. · `src/offscreen.html`
- [Offscreen logic](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/offscreen.js): processing worker · Runs screenshot/canvas handling and coordinates OCR execution. · `src/offscreen.js`
- **TesseractJS**: ocr engine · In-browser OCR engine used to extract text from image data.

### Build and packaging

Bundling, manifest, and static assets.

- [Manifest](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/src/manifest.json): extension config · Wires together permissions, entry points, and extension runtime pieces. · `src/manifest.json`
- [Webpack common](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/webpack.common.js): bundler config · Shared bundling configuration for all extension builds. · `webpack.common.js`
- [Webpack dev](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/webpack.dev.js): bundler config · Development build configuration. · `webpack.dev.js`
- [Webpack prod](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/webpack.prod.js): bundler config · Production build configuration. · `webpack.prod.js`
- [Package config](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/package.json): deps and scripts · Defines dependencies and build scripts for the extension. · `package.json`
- [UI assets](https://github.com/mohit-s96/youtube_ocr_extension/tree/release/src/img): branding assets · Icons and images used by the extension UI. · `src/img`
- [Static assets](https://github.com/mohit-s96/youtube_ocr_extension/tree/release/assets): branding assets · Packaged static assets such as logo and favicon resources. · `assets`
- [README](https://github.com/mohit-s96/youtube_ocr_extension/blob/release/README.md): docs · Describes the extension behavior, usage, and architecture. · `README.md`

## Connections

- Popup UI → Popup logic: uses · Popup UI is driven by its JavaScript controller.
- Popup logic → Background: starts flow · Popup actions request the OCR workflow through the background coordinator.
- Background → Content script: injects/routs · Background coordinates page-side work through the content script.
- Content script → Background: messages · Content script sends page and selection state back to the extension runtime.
- Content script → Offscreen page: captures region · Selected video-frame imagery is forwarded for hidden processing.
- Offscreen page → Offscreen logic: hosts · The offscreen document loads the processing logic.
- Offscreen logic → TesseractJS: runs OCR · Offscreen logic delegates text recognition to TesseractJS.
- Offscreen logic → Background: returns text · Extracted OCR text is sent back for orchestration and display.
- Manifest → Popup UI: declares · Manifest defines the popup entry point.
- Manifest → Background: declares · Manifest registers the background coordinator.
- Manifest → Content script: injects · Manifest configures content script injection on YouTube pages.
- Manifest → Offscreen page: enables · Manifest includes the offscreen document used for processing.
- Manifest → Styles: bundles · Manifest participates in packaging the extension UI assets.
- Webpack common → Webpack dev: shared by · Development build extends shared bundling rules.
- Webpack common → Webpack prod: shared by · Production build extends shared bundling rules.
- Package config → Webpack common: scripts · Package scripts drive the Webpack build pipeline.
- Package config → Webpack dev: scripts · Development script uses the dev build configuration.
- Package config → Webpack prod: scripts · Production script uses the production build configuration.
- Static assets → Popup UI: brands · Static assets support the visible extension surface.
- UI assets → Popup UI: decorates · Images and icons support the popup UI presentation.
- README → Background: describes · Documentation explains the extension architecture and flow.
- README → TesseractJS: mentions · README identifies TesseractJS as the OCR engine.
