# ruiner28/leethub architecture

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

- Interactive diagram: https://gitdiagram.com/ruiner28/leethub
- Repository: https://github.com/ruiner28/leethub

## Overview

##### Project Type and Purpose
- This is a browser extension built for Google Chrome (and Firefox, as indicated by the README) that acts as a bridge between LeetCode and GitHub. The main purpose is to detect when a user has solved a problem on LeetCode and automatically push the relevant code to a GitHub repository.
- The extension handles authentication with GitHub (via OAuth2) and interacts with the DOM of LeetCode pages to extract code, which it then commits using GitHub APIs.

##### File Structure
- Key Configuration/Manifest Files:
- manifest.json: This file defines the Chrome extension’s settings, permissions, and background scripts.
- package.json and package-lock.json: Manage npm dependencies and scripts.
- .eslintrc, .prettierrc, and .gitignore: Enforce code style and ignore unnecessary files.
- Assets and UI:
- welcome.html & css/welcome.css: Another UI component for onboarding or instructions.
- Scripts:
- Several scripts in the "scripts" directory, including authorize.js (for GitHub OAuth), background.js (to handle background events), and domain-specific scripts (leetcode.js, gfg.js, oauth2.js, and welcome.js). These scripts encapsulate the core business logic for authentication, data interception, and automated syncing.

##### Architectural Components and Relationships
a. Main Components:
- Browser Extension UI:
- Popup Interface (popup.html and popup.js): This is the user-facing interface that allows them to start the syncing process, view status, or trigger manual actions.
- Welcome/Onboarding Page (welcome.html): Used for first-time instructions.
- Background & Service Scripts:
- Background Script (scripts/background.js): Manages long-running tasks/events, such as listening for events from the browser or handling push notifications.
- Authorization & OAuth2 Modules (scripts/authorize.js, scripts/oauth2.js): Implement the GitHub authentication workflow.
- Code Extraction and Integration:
- Domain-specific Scripts (scripts/leetcode.js and scripts/gfg.js): These likely interact with the LeetCode (or GeeksForGeeks) websites, extract code when a problem is solved and trigger the sync.
- GitHub Integration:
- API interaction logic integrated into the scripts: responsible for pushing code to GitHub repositories using GitHub REST/GraphQL APIs.
- Extension Asset Configuration:
- manifest.json: Configures necessary permissions (like activeTab, storage, etc.) and registers scripts.

b. Relationships and Interactions:
- The popup (UI) triggers user-initiated actions (e.g., “authorize with GitHub”).
- On authorization, the OAuth2 scripts communicate with GitHub by redirecting the user to the GitHub login, then returning an authorization token.
- The background script continually monitors the browser’s activity (e.g., detecting that a LeetCode problem was solved).
- Once the code is extracted via the LeetCode integration script, the extension uses the GitHub API (via the authenticated session) to push the changes.
- From the User → UI (Popup, Welcome) → Authorization Request → GitHub OAuth Flow → API Access Token.
- LeetCode site → Code Extraction Script → Background Script → GitHub API for commit.

##### Architectural Patterns, Technologies, and Design Principles
- Client-Side Scripting for Browser Extensions: Use of standard JavaScript with modular scripts organized into specific responsibilities.
- Event-Driven Architecture: Leveraging background scripts and event listeners to detect changes on web pages.
- Separation of Concerns: Distinct scripts handle UI, OAuth, background processing, and code extraction.
- Integration with External Services (GitHub OAuth/API, LeetCode's site).

##### Instructing the Principal Software Engineer
- UI Components: Popup, Welcome Page.
- Background Services: Background.js, OAuth, and Code Extraction Modules.
- External Services: GitHub API endpoints.
- Configuration: Manifest and npm build tools/config scripts.
- Data flow from the browser extension UI to the background script.
- Event triggers from code detection on LeetCode to the sync logic.
- Authorization flows from the extension to GitHub and back.
- For internal scripts and modules (e.g., rectangles).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global User
    U["User"]

    %% Configuration Files
    subgraph "Configuration"
        M["Manifest (manifest.json)"] 
        N["NPM Config (package.json)"]
        L["Lock Config (package-lock.json)"]
        E["ESLint (.eslintrc)"]
        P["Prettier (.prettierrc)"]
        G["Git Ignore (.gitignore)"]
    end

    %% Browser Extension UI
    subgraph "Browser Extension UI"
        PU["Popup HTML (popup.html)"]
        PJ["Popup JS (popup.js)"]
        PC["Popup CSS (css/popup.css)"]
        W["Welcome Page (welcome.html)"]
        WC["Welcome CSS (css/welcome.css)"]
    end

    %% Background Services
    subgraph "Background Services"
        BS["Background Script (background.js)"]
    end

    %% GitHub Authorization & OAuth2
    subgraph "GitHub Authorization & OAuth2"
        A["Authorize Script (authorize.js)"]
        O["OAuth2 Script (oauth2.js)"]
    end

    %% Code Extraction Modules
    subgraph "Code Extraction"
        LC["LeetCode Extraction (leetcode.js)"]
        GF["GFG Extraction (gfg.js)"]
    end

    %% Extension Assets
    subgraph "Extension Assets"
        EA["Extension Asset (top_trending_leethub.mhtml)"]
    end

    %% External Services
    subgraph "External Services"
        GH["GitHub API"]
    end

    %% Relationships
    U -->|"trigger"| PU
    U -->|"onboarding"| W

    %% UI to Auth Flow
    PU -->|"initiate_auth"| A
    A -->|"OAuth_Flow"| O
    O -->|"auth_response"| BS

    %% UI Manual Sync
    PU -->|"manual_sync"| BS

    %% Code Detection Flow
    LC -->|"code_detected"| BS
    GF -->|"code_detected"| BS

    %% Code Pushing Flow
    BS -->|"push_code"| GH

    %% Configuration Influence
    M ---|"config_ui"| PU
    M ---|"config_background"| BS

    %% Assets Usage
    EA ---|"UI_assets"| PU

    %% Styles
    classDef config fill:#ffcc00,stroke:#000,stroke-width:1.5px;
    classDef ui fill:#66ccff,stroke:#000,stroke-width:1.5px;
    classDef background fill:#99ff99,stroke:#000,stroke-width:1.5px;
    classDef auth fill:#ff9999,stroke:#000,stroke-width:1.5px;
    classDef extraction fill:#ffccff,stroke:#000,stroke-width:1.5px;
    classDef asset fill:#ffcc99,stroke:#000,stroke-width:1.5px;
    classDef external fill:#ccccff,stroke:#000,stroke-width:1.5px;

    %% Assign classes
    class M,N,L,E,P,G config;
    class PU,PJ,PC,W,WC ui;
    class BS background;
    class A,O auth;
    class LC,GF extraction;
    class EA asset;
    class GH external;

    %% Click Events for Configuration Files
    click M "https://github.com/ruiner28/leethub/blob/main/manifest.json"
    click N "https://github.com/ruiner28/leethub/blob/main/package.json"
    click L "https://github.com/ruiner28/leethub/blob/main/package-lock.json"
    click E "https://github.com/ruiner28/leethub/blob/main/.eslintrc"
    click P "https://github.com/ruiner28/leethub/blob/main/.prettierrc"
    click G "https://github.com/ruiner28/leethub/blob/main/.gitignore"

    %% Click Events for Browser Extension UI
    click PU "https://github.com/ruiner28/leethub/blob/main/popup.html"
    click PJ "https://github.com/ruiner28/leethub/blob/main/popup.js"
    click PC "https://github.com/ruiner28/leethub/blob/main/css/popup.css"
    click W "https://github.com/ruiner28/leethub/blob/main/welcome.html"
    click WC "https://github.com/ruiner28/leethub/blob/main/css/welcome.css"

    %% Click Event for Background Service
    click BS "https://github.com/ruiner28/leethub/blob/main/scripts/background.js"

    %% Click Events for GitHub Authorization & OAuth2
    click A "https://github.com/ruiner28/leethub/blob/main/scripts/authorize.js"
    click O "https://github.com/ruiner28/leethub/blob/main/scripts/oauth2.js"

    %% Click Events for Code Extraction Modules
    click LC "https://github.com/ruiner28/leethub/blob/main/scripts/leetcode.js"
    click GF "https://github.com/ruiner28/leethub/blob/main/scripts/gfg.js"

    %% Click Event for Extension Assets
    click EA "https://github.com/ruiner28/leethub/blob/main/assets/extension/top_trending_leethub.mhtml"
```
