WebSnap
WebSnap is a command-line tool for capturing screenshots of websites. It provides two implementations: a Rust-based version that uses Google Chrome in headless mode to capture screenshots, and a WebAssembly (WASM) version that serves as a placeholder for browser-based screenshot functionality. The tool allows customization of screenshot dimensions, user-agent strings, output formats, and more.
Features
- Capture website screenshots with customizable width, height, and user-agent.
- Support for output formats: WebP, PNG, JPG.
- Debug mode for detailed logging.
- Command-line interface with flexible arguments.
- WASM integration for potential browser-based usage (placeholder implementation).
- Sleep time configuration to handle dynamic content loading.
Prerequisites
- Rust Implementation:
- Rust toolchain (
cargo,rustc). - Google Chrome (stable version) installed.
- Dependencies:
clap,chrono,serde,md5.
- Rust toolchain (
- WASM Implementation:
wasm-packfor building WebAssembly.- Node.js and npm for JavaScript integration.
- A modern browser for testing WASM.
Installation
Rust Version
- Clone the repository:
- Install dependencies and build:
- The binary will be available at
target/release/websnap.
WASM Version
- Install
wasm-pack: - Build the WASM module:
- This generates a
pkgdirectory with the WASM module and JavaScript glue code.
Usage
Rust Version
Run the tool with the following command:
Options
-o, --output <FILE>: Output file or path for the screenshot.-d, --debug: Enable debug output.-h, --height <PIXELS>: Screenshot height (default: 1080).-w, --width <PIXELS>: Screenshot width (default: 1920).-s, --sleep <SECONDS>: Sleep time before capturing (default: 0).-u, --ua <STRING>: Custom User-Agent string.-t, --target <URL>: Target URL to capture (e.g.,https://example.com).-f, --format <FORMAT>: Output format (png,webp,jpg).--help: Print help information.
Example
Capture a screenshot of https://example.com with a custom size and output format:
WASM Version
The WASM version is a placeholder and logs parameters to the console. To integrate it into a web project:
- Create an HTML file (e.g.,
index.html):WebSnap WASM - Copy the
pkgdirectory to your web project. - Serve the project using a local server (e.g.,
npx serve). - Open the browser console to see the logged output.
Note: The WASM version requires a JavaScript-based screenshot implementation (e.g., using Puppeteer or canvas) to be fully functional.
Compilation and Packaging
Rust Compilation
- Build the Rust binary:
- Run tests (if any):
- Package the binary for distribution:
WASM Compilation
- Build the WASM module:
- Test the WASM module in a browser:
- Package for npm:
npm Integration
To publish the WASM module to npm:
- Update the
pkg/package.jsonwith your project details (e.g., name, version). - Publish to npm:
- Install in a JavaScript project:
- Import and use in your JavaScript code as shown in the WASM usage example.
Project Structure
websnap/
├── src/
│ ├── main.rs # Rust CLI implementation
│ └── lib.rs # WASM implementation
├── Cargo.toml # Rust dependencies and metadata
├── README.md # This file
└── pkg/ # WASM output (after wasm-pack build)
Limitations
- The Rust version requires Google Chrome to be installed and only works on systems supporting Chrome's headless mode.
- The WASM version is a placeholder and does not capture screenshots; it requires a JavaScript-based implementation for full functionality.
- No support for capturing dynamic content requiring user interaction.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
MIT License. See LICENSE for details.