# dawn-lc/iwaradownloadtool architecture

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

- Interactive diagram: https://gitdiagram.com/dawn-lc/iwaradownloadtool
- Repository: https://github.com/dawn-lc/iwaradownloadtool

## Overview

##### Project Type and Purpose
- This is a browser userscript (Tampermonkey/GreasyFork) named “Iwara视频下载工具.” It runs entirely client-side in a Chromium-based or Firefox browser. Its purpose is to scrape Iwara video pages, extract video URLs (and third-party cloud drive links), offer bulk download via Aria2 (or native downloads), and manage user preferences (auto-follow, auto-like, private or hidden video access).

##### File Structure Analysis
- **Top-level**
- `.github/` – CI workflows, issue templates, multi-language README stubs.
- `build/` – custom TypeScript/JavaScript scripts to compile, inline CSS, remove comments, and produce the final `.user.js` for release.
- `src/` – all TypeScript modules for runtime logic. Notable sub-folders:
– `css/` – main.css injected into the page
– `i18n/` – locale JSON files (en, zh_cn)
– `mata/` – userscript metadata template
– `types/` – custom `.d.ts` definitions
- `test/` – unit tests for helper functions
- `tsconfig.json`, `package.json` – build & dependency configuration

##### README Insights
- Features list (bulk download, Aria2 integration, auto-actions, private/hidden video support)
- Browser and script manager compatibility
- Dependencies: moment.js, Dexie.js (IndexedDB wrapper), emoji-regex, aria2rpc

##### Diagramming: Main Components & Their Interactions
a. **Build Pipeline**
- Input: `src/` (TS modules), `css/`, `mata/`
- Steps:
1. Transpile TS → JS (`build/build.ts`)
2. Inline CSS into JS (`build/inlineCSS.ts`)
3. Strip comments (`build/removeComments.ts`)
4. Package metadata & produce final `.user.js` (`build/release.js`)
- Output: distributable UserScript

b. **Runtime Environment**
- **Browser Host**
- Tampermonkey/GreasyFork loads the `.user.js`
- Script runs in the context of Iwara video pages
- **Core Modules (src/)**
- `main.ts` – entry point: reads `config`, initializes DB & i18n, injects CSS & UI
- `db.ts` – Dexie-based IndexedDB for storing settings, followed authors, download history
- `hijack.ts` – monkey-patches/fetch intercepts to grab dynamically loaded comments or pagination
- `function.ts` / `import.ts` – utilities for parsing DOM, extracting links, and building download tasks
- `aria2rpc` integration – sends RPC commands to an external Aria2 server for downloads
- `toastify.ts` – handles in-page notifications
- `i18n.ts` – locale switching

c. **External Services**
- **Iwara Web Server** – supplies HTML pages, comments JSON, video file URLs
- **Aria2 RPC Server** (optional) – receives HTTP RPC calls from the script to schedule downloads

d. **Data Flows & Interactions**
1. **Page Load** → Tampermonkey injects script
2. Script reads `config` (from `config.ts` or UI‐persisted DB settings)
3. Initialize Dexie DB → fetch local state (e.g., which authors are auto-followed)
4. Inject CSS + UI controls (download buttons, settings panel)
5. When a user clicks “Download” (or auto-download is enabled):
- The script collects video URLs and optional cloud drive links
- Depending on mode:
– **Aria2 Mode**: call `aria2rpc.addUri(...)`
– **Native Mode**: dynamically create `` and trigger click
- Record task in Dexie DB → show status via toastify
6. For auto-like/follow: trigger additional fetch requests or DOM events to simulate these actions

##### Architectural Patterns & Design Principles
- **Modular TS code**: each feature in its own module
- **Event-driven**: UI events (clicks) trigger download pipelines
- **Client-only**: no backend server except optional Aria2 process
- **Separation of concerns**: build pipeline vs runtime logic; CSS vs JS; i18n vs business logic

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% Build Time
  subgraph "Build Time"
    direction TB
    BB["build/build.ts\n(Transpile TS→JS)"]:::build
    IC["build/inlineCSS.ts\n(Inline CSS)"]:::build
    RC["build/removeComments.ts\n(Strip Comments)"]:::build
    RL["build/release.js\n(Package & Release)"]:::build
    BB --> IC --> RC --> RL
    RL --> OUT[".user.js\n(distributable)"]:::build
  end

  %% Runtime
  subgraph "Runtime (Browser)"
    direction TB
    TM["Tampermonkey/GreasyFork"]:::external
    USR[".user.js"]:::build
    TM --> USR

    subgraph "Script Modules"
      direction TB
      subgraph "Initialization"
        direction TB
        Main["main.ts"]:::internal
        Config["config.ts"]:::internal
        Env["env.ts"]:::internal
        Enum["enum.ts"]:::internal
        Date["date.ts"]:::internal
        Class["class.ts"]:::internal
        Import["import.ts"]:::internal
      end
      subgraph "UI & i18n"
        direction TB
        CSSmod["css/main.css"]:::internal
        I18nMod["i18n.ts"]:::internal
        ENjson["en.json"]:::internal
        ZHjson["zh_cn.json"]:::internal
        Toast["toastify.ts"]:::internal
        Ext["userjs.mata"]:::internal
      end
      subgraph "Data & Storage"
        direction TB
        DB["db.ts\n(Dexie IndexedDB)"]:::storage
        A2Types["types/aria2.d.ts"]:::internal
        CSSDefs["types/css.d.ts"]:::internal
        IwaraTypes["types/iwara.d.ts"]:::internal
        MainTypes["types/main.d.ts"]:::internal
      end
      subgraph "Core Functionality"
        direction TB
        Hijack["hijack.ts\n(HTTP/FETCH Hijacking)"]:::internal
        Func["function.ts\n(DOM Parsing & Task Builder)"]:::internal
        AR["aria2rpc integration"]:::internal
      end
    end

    USR --> Main
    Main --> Config
    Main --> DB
    Main --> I18nMod
    Main --> CSSmod
    Main --> Hijack
    Main --> Func
    Func --> AR

    %% External services
    Iwara["Iwara Web Server"]:::external
    A2RPC["Aria2 RPC Server"]:::external

    Hijack --> Iwara
    Func --> Iwara
    AR --> A2RPC

    %% User actions
    ClickDL["User Click 'Download'"]:::external
    ClickDL --> Func
    ClickDL --> AR
    ClickDL -->|"Native Mode"| USR
  end

  %% Legend
  subgraph Legend
    direction TB
    Build["Build Tools"]:::build
    Internal["Internal Modules"]:::internal
    Storage["IndexedDB Storage"]:::storage
    External["External Services"]:::external
  end

  classDef build fill:#bbbbbb,stroke:#888,stroke-width:1px;
  classDef internal fill:#bbeeff,stroke:#0277bd,stroke-width:1px;
  classDef storage fill:#b2ff59,stroke:#558b2f,stroke-width:1px;
  classDef external fill:#ffcc80,stroke:#ef6c00,stroke-width:1px;

  %% Click Events
  click BB "https://github.com/dawn-lc/iwaradownloadtool/blob/master/build/build.ts"
  click IC "https://github.com/dawn-lc/iwaradownloadtool/blob/master/build/inlineCSS.ts"
  click RC "https://github.com/dawn-lc/iwaradownloadtool/blob/master/build/removeComments.ts"
  click RL "https://github.com/dawn-lc/iwaradownloadtool/blob/master/build/release.js"
  click Main "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/main.ts"
  click Config "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/config.ts"
  click DB "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/db.ts"
  click Hijack "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/hijack.ts"
  click Func "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/function.ts"
  click Import "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/import.ts"
  click A2Types "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/types/aria2.d.ts"
  click Toast "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/toastify.ts"
  click I18nMod "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/i18n.ts"
  click ENjson "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/i18n/en.json"
  click ZHjson "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/i18n/zh_cn.json"
  click CSSmod "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/css/main.css"
  click Ext "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/mata/userjs.mata"
  click Env "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/env.ts"
  click Enum "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/enum.ts"
  click Date "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/date.ts"
  click Class "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/class.ts"
  click CSSDefs "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/types/css.d.ts"
  click IwaraTypes "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/types/iwara.d.ts"
  click MainTypes "https://github.com/dawn-lc/iwaradownloadtool/blob/master/src/types/main.d.ts"
  click test "https://github.com/dawn-lc/iwaradownloadtool/blob/master/test/replaceVariable.test.js"
  click tsconfig "https://github.com/dawn-lc/iwaradownloadtool/blob/master/tsconfig.json"
  click pkg "https://github.com/dawn-lc/iwaradownloadtool/blob/master/package.json"
  click CI "https://github.com/dawn-lc/iwaradownloadtool/blob/master/.github/workflows/Release.yml"
  click ISSUE "https://github.com/dawn-lc/iwaradownloadtool/blob/master/.github/ISSUE_TEMPLATE/bug_report.yaml"
```
