0.0.19 • Published 4 months ago

screenshotkit v0.0.19

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

screenshotkit

A Node.js native module for taking WebKit screenshots. This package provides a high-performance way to capture screenshots of web content using WebKit on macOS.

Installation

npm install screenshotkit

Requirements

  • macOS (This is a macOS-only package as it uses native WebKit)
  • Node.js 18 or later

No Xcode or compiler toolchain is required for installation, as prebuilt binaries are provided for common macOS architectures and Node.js versions.

Prebuilt Binaries

This package includes prebuilt binaries for macOS arm64 architecture with Node.js versions 18, 20, and 22. The appropriate binary is automatically selected and used based on your Node.js version.

Benefits of bundled binaries:

  • No post-install scripts required
  • Works in environments where post-install scripts are disabled
  • Faster installation as no download or compilation is needed
  • Works offline

If you want to build from source instead, you can use:

npm install --build-from-source

This will require Xcode Command Line Tools to be installed.

For Maintainers

Building Binaries for Different Node.js Versions

To build binaries for different Node.js versions locally:

npm run build:binaries

This will build binaries for Node.js versions 18, 20, and 22 (requires NVM to be installed) and store them in the lib/binding directory with the following structure:

lib/binding/
  ├── binary-manifest.json
  ├── node-v108/
  │   └── screenshotkit.node  # Binary for Node.js 18
  ├── node-v115/
  │   └── screenshotkit.node  # Binary for Node.js 20
  └── node-v127/
      └── screenshotkit.node  # Binary for Node.js 22

Creating a New Release

This project includes a simple release script that handles version bumping and building binaries:

npm run release

The script will: 1. Prompt you for a new version number (or automatically bump the patch version) 2. Update the version in package.json 3. Build binaries for different Node.js versions 4. Commit the changes and create a git tag 5. Push the changes and tag to GitHub

After the release is created, you can publish to npm with:

npm publish

Usage

const WebKitInstance = require('screenshotkit');
const fs = require('fs');

async function example() {
  const webkit = new WebKitInstance();
  
  // Capture a screenshot of a URL
  const pngData = await webkit.captureURL('https://example.com');
  
  // Save the PNG data to a file
  fs.writeFileSync('screenshot.png', pngData);
  
  // Or load a URL first and then take a screenshot
  await webkit.loadURL('https://example.com');
  const pngData2 = await webkit.takeScreenshot();
  
  // Load HTML content directly
  await webkit.loadHTML('<html><body><h1>Hello World</h1></body></html>');
  
  // Evaluate JavaScript
  const result = await webkit.evaluateJavaScript('document.title');
  
  // Get HTML content
  const html = await webkit.getHTMLContent();
}

API

new WebKitInstance()

Creates a new WebKitInstance instance.

loadURL(url: string, timeout?: number): Promise<void>

Loads a URL in the WebView. Optional timeout in seconds (defaults to 30).

loadHTML(html: string, timeout?: number): Promise<void>

Loads HTML content directly into the WebView. Optional timeout in seconds (defaults to 30).

captureURL(url: string): Promise<Uint8Array>

Captures a screenshot of the given URL and returns a PNG image as a Uint8Array.

evaluateJavaScript(script: string): Promise<string | undefined>

Evaluates JavaScript code in the WebView and returns the result.

getHTMLContent(): Promise<string>

Gets the current HTML content of the WebView.

takeScreenshot(): Promise<Uint8Array>

Takes a screenshot of the current WebView content and returns a PNG image as a Uint8Array.

Development

Building from Source

To build the project from source:

# Install dependencies
npm install

# Build the TypeScript code
npm run build

The compiled JavaScript will be output to the dist directory.

License

ISC

0.0.19

4 months ago

0.0.18

4 months ago

0.0.17

4 months ago

0.0.15

4 months ago

0.0.14

4 months ago

0.0.11

4 months ago

0.0.10

4 months ago

0.0.9

4 months ago

0.0.8

5 months ago

0.0.7

5 months ago

0.0.1

5 months ago