capture-website
Capture screenshots of websites
README
capture-website
Capture screenshots of websites
It uses Puppeteer (Chrome) under the hood.
See capture-website-cli for the command-line tool.
Install
- ```sh
- npm install capture-website
- ```
Note to Linux users: If you get a sandbox-related error, you need to enable system sandboxing.
Usage
- ```js
- import captureWebsite from 'capture-website';
- await captureWebsite.file('https://sindresorhus.com', 'screenshot.png');
- ```
API
captureWebsite.file(input, outputFilePath, options?)
Capture a screenshot of the given input and save it to the given outputFilePath.
Intermediate directories are created for you if they do not exist.
captureWebsite.buffer(input, options?)
Capture a screenshot of the given input.
captureWebsite.base64(input, options?)
Capture a screenshot of the given input.
input
Type: string
The URL, file URL, data URL, local file path to the website, or HTML.
- ```js
- import captureWebsite from 'capture-website';
- await captureWebsite.file('index.html', 'local-file.png');
- ```
options
Type: object
inputType
Type: string\
Default: 'url'\
Values: 'url' 'html'
Set it to html to treat input as HTML content.
- ```js
- import captureWebsite from 'capture-website';
- await captureWebsite.file('<h1>Awesome!</h1>', 'screenshot.png', {
- inputType: 'html'
- });
- ```
width
Type: number\
Default: 1280
Page width.
height
Type: number\
Default: 800
Page height.
type
Type: string\
Values: 'png' 'jpeg' 'webp'\
Default: 'png'
Image type.
quality
Type: number\
Values: 0...1\
Default: 1
Image quality. Only for {type: 'jpeg'} and {type: 'webp'}.
scaleFactor
Type: number\
Default: 2
Scale the webpage n times.
The default is what you would get if you captured a normal screenshot on a computer with a retina (High DPI) screen.
emulateDevice
Type: string\
Values: Devices(Use the name property)
Make it look like the screenshot was taken on the specified device.
This overrides the width, height, scaleFactor, and userAgent options.
- ```js
- import captureWebsite from 'capture-website';
- await captureWebsite.file('https://sindresorhus.com', 'screenshot.png', {
- emulateDevice: 'iPhone X'
- });
- ```
fullPage
Type: boolean\
Default: false
Capture the full scrollable page, not just the viewport.
defaultBackground
Type: boolean\
Default: true
Include the default white background.
Disabling this lets you capture screenshots with transparency.
timeout
Type: number (seconds)\
Default: 60
The number of seconds before giving up trying to load the page.
Specify 0 to disable the timeout.
delay
Type: number (seconds)\
Default: 0
The number of seconds to wait after the page finished loading before capturing the screenshot.
This can be useful if you know the page has animations that you like it to finish before capturing the screenshot.
waitForElement
Type: string
Wait for a DOM element matching the given CSS selector to appear in the page and to be visible before capturing the screenshot. It times out afteroptions.timeout seconds.
element
Type: string
Capture the DOM element matching the given CSS selector. It will wait for the element to appear in the page and to be visible. It times out afteroptions.timeout seconds. Any actions performed as part of options.beforeScreenshot occur before this.
hideElements
Type: string[]
Hide DOM elements matching the given CSS selectors.
Can be useful for cleaning up the page.
This sets [visibility: hidden](https://stackoverflow.com/a/133064/64949) on the matched elements.
- ```js
- import captureWebsite from 'capture-website';
- await captureWebsite.file('https://sindresorhus.com', 'screenshot.png', {
- hideElements: [
- '#sidebar',
- 'img.ad'
- ]
- });
- ```
removeElements
Type: string[]
Remove DOM elements matching the given CSS selectors.
This sets [display: none](https://stackoverflow.com/a/133064/64949) on the matched elements, so it could potentially break the website layout.
clickElement
Type: string
Click the DOM element matching the given CSS selector.
scrollToElement
Type: string | object
Scroll to the DOM element matching the given CSS selector.
element
Type: string
A CSS selector.
offsetFrom
Type: string\
Values: 'top' | 'right' | 'bottom' | 'left'
Offset origin.
offset
Type: number
Offset in pixels.
disableAnimations
Type: boolean\
Default: false
Disable CSS animations and transitions.
blockAds
Type: boolean\
Default: true
isJavaScriptEnabled
Type: boolean\
Default: true
Whether JavaScript on the website should be executed.
This does not affect the scripts and modules options.
modules
Type: string[]
Inject JavaScript modules into the page.
Accepts an array of inline code, absolute URLs, and local file paths (must have a .js extension).
- ```js
- import captureWebsite from 'capture-website';
- await captureWebsite.file('https://sindresorhus.com', 'screenshot.png', {
- modules: [
- 'https://sindresorhus.com/remote-file.js',
- 'local-file.js',
- `
- document.body.style.backgroundColor = 'red';
- `
- ]
- });
- ```
scripts
Type: string[]