# nytimes/svg-crowbar architecture

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

- Interactive diagram: https://gitdiagram.com/nytimes/svg-crowbar
- Repository: https://github.com/nytimes/svg-crowbar

## Overview

##### Project type and purpose
- From the README and file tree, this is a client-side, open-source “tool” – a Chrome-specific bookmarklet that you drag into your bookmarks bar. When invoked on any webpage, it extracts SVG nodes plus their computed styles and packages them into a standalone SVG file for download (e.g., to edit in Illustrator).
- There is no backend or server-side component: it’s purely JavaScript/CSS/HTML running in the browser.

##### File structure
- Top-level files:
- index.html – demo page / user instructions / bookmarklet installer link
- svg-crowbar.js & svg-crowbar-2.js – the two flavors of the bookmarklet code
- LICENSE & README.md – docs and license
- assets/ directory: imported.css & linked.css – CSS snippets that the script may inline or reference when reconstructing styles in the extracted SVG.
- No src/ or server directories—confirms this is purely front-end.

##### README for additional insights
- Purpose: extract any SVG from arbitrary pages, preserving styling
- Built “with d3.js in mind,” but no direct d3 dependency in the repo—indicates the code uses standard DOM/SVG/CSS APIs to query nodes and styles.

a. Main components:
1. User & Browser UI
2. Bookmarklet Launcher (code loaded from svg-crowbar.js)
3. DOM/SVG Extraction Engine
4. Style Collector (CSSOM interpreter + assets/imported.css + assets/linked.css)
5. SVG Serializer & File Generator (Blob / data URI / download link)
6. Output (Downloaded .svg file)

b. Relationships and interactions:
- User clicks the bookmarklet → the Launcher script injects itself into the current page context
- Launcher invokes the Extraction Engine → it walks the DOM to collect <svg> elements.
- Style Collector queries computed styles for each SVG node and merges any referenced CSS from assets.
- SVG Serializer builds a standalone SVG string, wraps it in a Blob or data URI, then triggers a download in the browser.
- Downloaded file handed back to the user’s file system.

c. Architectural patterns / design principles:
- Client-only micro-utility / bookmarklet pattern (IIFE that lives in the browser context)
- Modularity: two script versions (svg-crowbar.js is minified combined code; svg-crowbar-2.js may be more modular for debugging)
- Progressive enhancement: works on any SVG, regardless of framework (d3.js, plain JS, etc.)

d. Key technologies / libraries:
- JavaScript DOM API, CSSOM APIs, Blob API, download anchor (HTML5)
- CSS assets for style inlining (imported.css / linked.css)
- Optional alignment with d3.js data-binding and SVG creation patterns

##### Detailed coverage
- Interactions (e.g., “queryComputedStyle( ) on each node”)
- Conditional paths (e.g., if external CSS rules are found, inline them)
- Both versions of the script, showing that svg-crowbar-2.js is a variant entry point.

## Diagram (Mermaid)

```mermaid
flowchart TB
    User["User & Browser UI"]:::runtime
    Launcher1["Bookmarklet Launcher\n(svg-crowbar.js)"]:::code
    Launcher2["Bookmarklet Launcher\n(svg-crowbar-2.js)"]:::code
    Extraction["DOM/SVG Extraction Engine"]:::transform
    StyleCollector["Style Collector Module"]:::assets
    ImportCss["Imported CSS"]:::assets
    LinkedCss["Linked CSS"]:::assets
    Serializer["SVG Serializer & File Generator"]:::transform
    Output["Downloaded SVG File"]:::output
    Docs["Documentation & Usage\n(README.md)"]:::docs
    License["License Information\n(LICENSE)"]:::docs

    User --> Launcher1
    User --> Launcher2
    Launcher1 --> Extraction
    Launcher2 --> Extraction
    Extraction --> StyleCollector
    StyleCollector --> Serializer
    Serializer --> Output
    StyleCollector --> ImportCss
    StyleCollector --> LinkedCss

    click User "https://github.com/nytimes/svg-crowbar/blob/gh-pages/index.html"
    click Launcher1 "https://github.com/nytimes/svg-crowbar/blob/gh-pages/svg-crowbar.js"
    click Launcher2 "https://github.com/nytimes/svg-crowbar/blob/gh-pages/svg-crowbar-2.js"
    click Extraction "https://github.com/nytimes/svg-crowbar/blob/gh-pages/svg-crowbar.js"
    click StyleCollector "https://github.com/nytimes/svg-crowbar/blob/gh-pages/assets/imported.css"
    click StyleCollector "https://github.com/nytimes/svg-crowbar/blob/gh-pages/assets/linked.css"
    click Serializer "https://github.com/nytimes/svg-crowbar/blob/gh-pages/svg-crowbar.js"
    click Docs "https://github.com/nytimes/svg-crowbar/blob/gh-pages/README.md"
    click License "https://github.com/nytimes/svg-crowbar/tree/gh-pages/LICENSE"

    classDef code fill:#e6ffe6,stroke:#2d7a2d,color:#034003
    classDef transform fill:#fff7cc,stroke:#a67c00,color:#5c3d00
    classDef assets fill:#e6f0ff,stroke:#224b7a,color:#001f3f
    classDef output fill:#d3d3d3,stroke:#666666,color:#333333
    classDef docs fill:#f0f0f0,stroke:#999999,color:#333333
    class User runtime
    class Docs,License docs
```
