# rob--w/crxviewer architecture

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

- Interactive diagram: https://gitdiagram.com/rob--w/crxviewer
- Repository: https://github.com/rob--w/crxviewer

## Overview

This project is a Chrome (and multi‑browser) extension called “CRX Viewer” that lets users view the source code of any Chrome extension without installing it. The extension achieves this by downloading a CRX (or similar extension package) from various extension stores, converting that package (using helper libraries to extract or “transpile” it into a zip file format), and then rendering the source files in a browser tab with features such as syntax highlighting, beautification, file search, and even hash calculation.

##### Main Components
- Extension UI Components:
- Viewer UI: Represented by files such as crxviewer.html, crxviewer.js, and associated LESS/CSS (e.g., crxviewer.less).
- Popup UI: popup.html and popup.js that provide a popup interface.
- Options page: options.html and options.js where users can configure behaviors.
- Background/Service Components:
- Background scripts like background.js, service_worker.js, and bg-contextmenu.js handle event listening, context menu creation, and coordinate tasks on actions.
- Core Logic and Helper Libraries:
- CRX-to-ZIP conversion: See src/lib/crx-to-zip.js.
- File processing utilities: Libraries for beautification (src/lib/beautify), prettification/syntax highlighting (src/lib/prettify), and ZIP handling (src/lib/zip.js and its supporting files).
- Other utility scripts such as domain-fronter.js (possibly handling cross-domain issues) and modern-crypto.js for cryptographic functions.
- External / Build Scripts:
- Files under “external”, “chrome-web-store”, “firefox-stuff”, and “opera-addon-gallery” indicate support scripts to build or package the extension for different browsers.
- The make.js and package.json file are used for building and managing dependencies.

##### Relationships and Interactions
- User Interaction Flow:
- The extension installs and injects a browser button on CRX detection (or through context menu via bg-contextmenu.js).
- When the user clicks the button, the background scripts handle the event.
- Action handlers then either download the extension (CRX file) from an external source (e.g., the Chrome Web Store) or process local files provided by the user.
- Processing Flow:
- The CRX file is fetched and then processed by helper libraries (notably the “crx-to-zip” converter) to extract the embedded files.
- Once extracted, the file names and contents are passed to the viewer module.
- Additional libraries are used to beautify code, apply syntax highlighting, search through files, display file hashes, and optionally handle embedded zip files.
- Data Flow:
- Then, flow to the UI layer where the resulting source files are displayed and interacted with (filtering, searching, etc.).

##### Architectural Patterns and Design Principles
- Separation of Concerns:
- UI and view layers are clearly separated (HTML pages, CSS/LESS, JS handling user actions) from the background processing logic.
- Modularity:
- Core functionality (conversion, beautification, and file manipulation) is abstracted in library modules under src/lib.
- Cross-browser Compatibility:
- Presence of separate manifest files (manifest.json, manifest_firefox.json, manifest_opera.json) and specific directories for browser-specific resources indicate the design’s attention to supporting multiple browsers.
- Event-Driven Architecture:
- Using background scripts and service workers to respond to user events (context menu, toolbar clicks) and asynchronous file processing benefits from event-driven design.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Top Layer: User
    U["User Action"]:::user

    %% External Integration Layer
    subgraph "External Integration"
        CS["Chrome Web Store Modules"]:::external
        FM["Firefox Manifest"]:::external
        OM["Opera Manifest"]:::external
    end

    %% Background/Service Layer
    subgraph "Background/Service Layer"
        BS["Background Script"]:::background
        CM["Context Menu & Event Handling"]:::background
        SW["Service Worker"]:::background
    end

    %% Core Logic and Helper Libraries
    subgraph "Core Logic"
        CTZ["CRX-to-ZIP Conversion"]:::core
        BT["Beautification Utils"]:::core
        PT["Syntax Highlighting/Prettification"]:::core
        ZH["ZIP Handling"]:::core
        DF["Domain Fronter"]:::core
        MC["Modern Crypto"]:::core
        S2["SHA256"]:::core
    end

    %% UI Layer
    subgraph "UI Components"
        VH["Viewer UI (HTML)"]:::frontend
        VJ["Viewer UI (JS)"]:::frontend
        VL["Viewer UI (LESS)"]:::frontend
        PH["Popup UI (HTML)"]:::frontend
        PJ["Popup UI (JS)"]:::frontend
        OH["Options Page (HTML)"]:::frontend
        OJ["Options Page (JS)"]:::frontend
    end

    %% Build & Dependency
    subgraph "Build & Dependency"
        MJS["Build Script (make.js)"]:::build
        PKG["Package Management (package.json)"]:::build
        EB["External Builder"]:::build
    end

    %% Connections for runtime flow
    U -->|"triggers"| CM
    U -->|"configures"| OH

    CS -->|"providesCRX"| BS
    FM -->|"providesCRX"| BS
    OM -->|"providesCRX"| BS

    BS -->|"initiates conversion"| CTZ
    CM --> CTZ
    SW --> CTZ

    %% Core logic details
    CTZ -->|"extracts"| BT
    CTZ -->|"extracts"| PT
    CTZ -->|"extracts"| ZH
    CTZ -->|"preprocesses"| DF
    CTZ -->|"verifies"| MC
    CTZ -->|"hashes"| S2

    %% Output to UI
    CTZ -->|"renders"| VH
    BT --> VH
    PT --> VJ
    ZH --> VH
    DF --> VH
    MC --> VH
    S2 --> VH

    %% Build dependencies (dashed lines)
    MJS ---|"builds"| BS
    PKG ---|"builds"| CTZ
    EB ---|"builds"| VH

    %% Styling classes
    classDef user fill:#FFCC00,stroke:#333,stroke-width:2px;
    classDef external fill:#FFAAAA,stroke:#900,stroke-width:2px;
    classDef background fill:#Aaffaa,stroke:#393,stroke-width:2px;
    classDef core fill:#FFFFAA,stroke:#999900,stroke-width:2px;
    classDef frontend fill:#AACCEE,stroke:#3366CC,stroke-width:2px;
    classDef build fill:#DDDDDD,stroke:#777,stroke-width:2px;

    %% Click events for UI Components
    click VH "https://github.com/rob--w/crxviewer/blob/master/src/crxviewer.html"
    click VJ "https://github.com/rob--w/crxviewer/blob/master/src/crxviewer.js"
    click VL "https://github.com/rob--w/crxviewer/blob/master/src/crxviewer.less"
    click PH "https://github.com/rob--w/crxviewer/blob/master/src/popup.html"
    click PJ "https://github.com/rob--w/crxviewer/blob/master/src/popup.js"
    click OH "https://github.com/rob--w/crxviewer/blob/master/src/options.html"
    click OJ "https://github.com/rob--w/crxviewer/blob/master/src/options.js"

    %% Click events for Background/Service Components
    click BS "https://github.com/rob--w/crxviewer/blob/master/src/background.js"
    click CM "https://github.com/rob--w/crxviewer/blob/master/src/bg-contextmenu.js"
    click SW "https://github.com/rob--w/crxviewer/blob/master/src/service_worker.js"

    %% Click events for Core Logic
    click CTZ "https://github.com/rob--w/crxviewer/blob/master/src/lib/crx-to-zip.js"
    click BT "https://github.com/rob--w/crxviewer/tree/master/src/lib/beautify"
    click PT "https://github.com/rob--w/crxviewer/tree/master/src/lib/prettify"
    click ZH "https://github.com/rob--w/crxviewer/blob/master/src/lib/zip.js"
    click DF "https://github.com/rob--w/crxviewer/blob/master/src/domain-fronter.js"
    click MC "https://github.com/rob--w/crxviewer/blob/master/src/lib/modern-crypto.js"
    click S2 "https://github.com/rob--w/crxviewer/blob/master/src/lib/sha256.js"

    %% Click events for External Integration
    click CS "https://github.com/rob--w/crxviewer/tree/master/chrome-web-store"
    click FM "https://github.com/rob--w/crxviewer/blob/master/src/manifest_firefox.json"
    click OM "https://github.com/rob--w/crxviewer/blob/master/src/manifest_opera.json"

    %% Click events for Build & Dependency
    click MJS "https://github.com/rob--w/crxviewer/blob/master/make.js"
    click PKG "https://github.com/rob--w/crxviewer/blob/master/package.json"
    click EB "https://github.com/rob--w/crxviewer/tree/master/external"
```
