# 0xsdeo/hook_js architecture

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

- Interactive diagram: https://gitdiagram.com/0xsdeo/hook_js
- Repository: https://github.com/0xsdeo/hook_js

## Overview

##### Project Type and Purpose
- This is an open-source collection of JavaScript “hook” userscripts (for Tampermonkey/Greasemonkey) that override or wrap built-in Web APIs (console, fetch, XHR, DOM properties, storage, debugger functions, CryptoJS, cookie, etc.).
- Its goal is to help reverse-engineers and front-end security analysts intercept calls, log parameters and stacks, or bypass anti-debugging tricks by injecting these scripts at document-start.

##### File Structure Analysis
- Top-level script files (Hook_fetch.js, Hook_xhr.js, hook_JSON.js) each target a single API.
- Folders group related scripts:
- hook_console/ (hook_log, hook_clear, hook_table, Forbidden_log)
- hook_window/ (hook_localStorage, hook_sessionStorage, hook_history, hook_close)
- Hook_xss/ (Hook_innerHTML, Hook_outerHTML)
- Hook_CryptoJS/ (unfinished CryptoJS hooks)
- Hook_cookie/ (v0.1, v0.2 cookie hooks)
- hook_debugger/ (Bypass_Debugger, Hook_Function, Hook_eval, plus tutorial-series scripts)
- “实用小脚本” (Fixed_window_size, 格式化检测 bypass)
- README describes each script’s purpose, usage notes, and sample outputs (in /image/README).

##### Key Architectural Components
a. Browser environment
b. Userscript engine (Tampermonkey/Greasemonkey)
c. HookJS script loader/manifest (each script’s @include, @run-at document-start)
d. Hook modules:
- Console hooks
- Window/storage hooks
- Network hooks (fetch, XHR)
- DOM hooks (innerHTML, outerHTML)
- CryptoJS hook
- Cookie hook
- Debugger bypass hooks
- Utility scripts
e. Underlying Web APIs / JS built-ins being overridden
f. Logging/output channel (console.log, stack traces, screenshots)
g. Consumer (developer observing console, using stack info)

##### Relationships and Data Flows
- At document-start, the userscript engine injects each HookJS module into the page context.
- Modules override or wrap target methods/properties on global objects (window, Document.prototype, XMLHttpRequest.prototype, etc.).
- When the page’s own code invokes those APIs, control flows into the HookJS wrapper, which:
- Captures parameters and stack traces
- Optionally bypasses anti-debugger traps (skips debugger statements)
- Logs details back via console or other means
- Passes control through to the original implementation (except in some blocking hooks)
- The developer views the output in the browser console to analyze behavior.

##### Architectural Patterns and Design Principles
- Modular grouping by concern (single-responsibility per hook script)
- Method overriding and proxy patterns (Function.prototype, Proxy usage in some bypass scripts)
- Central injection point (userscript manifest) with consistent run-at=document-start
- Non-invasive fall-through: after logging, original behavior continues unless explicitly blocked

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Browser Environment"
        TM["Tampermonkey Engine"]:::engine
        HCL["HookJS Core Loader<br/>(@run-at document-start)"]:::engine
    end

    TM --> HCL

    subgraph "Hook Modules"
        subgraph "Console Hooks"
            CH_FOLDER["hook_console/"]:::console
        end
        subgraph "Network Hooks"
            FH["Hook_fetch.js"]:::network
            XH["Hook_xhr.js"]:::network
        end
        subgraph "DOM Hooks"
            XSS_FOLDER["Hook_xss/"]:::dom
            JSON_HOOK["hook_JSON.js"]:::dom
        end
        subgraph "Storage Hooks"
            WS_FOLDER["hook_window/"]:::storage
        end
        subgraph "CryptoJS Hook"
            CRYPTO["Hook_CryptoJS/Hook_CryptoJS(未完成作).js"]:::crypto
        end
        subgraph "Cookie Hooks"
            CK_FOLDER["Hook_cookie/"]:::cookie
        end
        subgraph "Debugger Bypass Hooks"
            DBG_FOLDER["hook_debugger/"]:::debug
        end
        subgraph "Utility Scripts"
            UTIL_FOLDER["实用小脚本/"]:::util
        end
    end

    HCL --> CH_FOLDER
    HCL --> FH
    HCL --> XH
    HCL --> XSS_FOLDER
    HCL --> JSON_HOOK
    HCL --> WS_FOLDER
    HCL --> CRYPTO
    HCL --> CK_FOLDER
    HCL --> DBG_FOLDER
    HCL --> UTIL_FOLDER

    subgraph "Web APIs / Page Code"
        API["Original Web APIs<br/>(console, fetch, XHR,<br/>DOM, storage, debugger, cookie, CryptoJS)"]:::api
        PAGE["Page Code"]:::api
    end

    CH_FOLDER -->|"override console methods"| API
    FH -->|"wrap fetch"| API
    XH -->|"wrap XHR"| API
    XSS_FOLDER -->|"override DOM properties"| API
    JSON_HOOK -->|"wrap JSON methods"| API
    WS_FOLDER -->|"override storage/window methods"| API
    CRYPTO -->|"override CryptoJS methods"| API
    CK_FOLDER -->|"override document.cookie"| API
    DBG_FOLDER -->|"bypass debugger"| API
    UTIL_FOLDER -->|"utility injections"| API

    API -->|"normal execution"| PAGE

    subgraph "Output Channel"
        CONS["Developer Console<br/>(logs parameters + stack)"]:::output
    end

    CH_FOLDER --> CONS
    FH --> CONS
    XH --> CONS
    XSS_FOLDER --> CONS
    JSON_HOOK --> CONS
    WS_FOLDER --> CONS
    CRYPTO --> CONS
    CK_FOLDER --> CONS
    DBG_FOLDER --> CONS
    UTIL_FOLDER --> CONS

    %% Click Events
    click CH_FOLDER "https://github.com/0xsdeo/hook_js/tree/main/hook_console/"
    click WS_FOLDER "https://github.com/0xsdeo/hook_js/tree/main/hook_window/"
    click FH "https://github.com/0xsdeo/hook_js/blob/main/Hook_fetch.js"
    click XH "https://github.com/0xsdeo/hook_js/blob/main/Hook_xhr.js"
    click XSS_FOLDER "https://github.com/0xsdeo/hook_js/tree/main/Hook_xss/"
    click JSON_HOOK "https://github.com/0xsdeo/hook_js/blob/main/hook_JSON.js"
    click CRYPTO "https://github.com/0xsdeo/hook_js/blob/main/Hook_CryptoJS/Hook_CryptoJS(未完成作).js"
    click CK_FOLDER "https://github.com/0xsdeo/hook_js/tree/main/Hook_cookie/"
    click DBG_FOLDER "https://github.com/0xsdeo/hook_js/tree/main/hook_debugger/"
    click UTIL_FOLDER "https://github.com/0xsdeo/hook_js/tree/main/实用小脚本/"

    classDef console fill:#bbdefb,stroke:#0d47a1
    classDef network fill:#c8e6c9,stroke:#1b5e20
    classDef dom fill:#ffe0b2,stroke:#e65100
    classDef storage fill:#e1bee7,stroke:#4a148c
    classDef crypto fill:#f8bbd0,stroke:#880e4f
    classDef cookie fill:#d7ccc8,stroke:#3e2723
    classDef debug fill:#ffcdd2,stroke:#b71c1c
    classDef util fill:#f0f4c3,stroke:#827717
    classDef engine fill:#cfd8dc,stroke:#455a64
    classDef api fill:#ffffff,stroke:#000000
    classDef output fill:#fff9c4,stroke:#f57f17
```
