Capture the web in many ways using headless chrome
This program is an overlay of puppeteer which is designed to allow the easy extraction of single or multiple pages or sections, in multiple formats and in the fastest way possible.
If you want to use it inside your scripts, save and install it in your project dependencies.
npm install --save webpage-capture
Once it has done you can import webpage-capture in your scripts and start using it, refer to the usage section.
You can also use it from the command line using the cli module, installing it globally:
npm install -g webpage-capture-cli
Than you can start playing around with webcapture command and with the options using the built-in help typing: --help
First you have to import webpage-capture and initializire a new capturer with default or custom options.
import WebCapture from 'webpage-capture'
const capturer = new WebCapture()
(async () => {
// Single input
await capturer.capture('https://google.it')
// Multiple inputs
const res = await capturer.capture([
'/~https://github.com/b4dnewz',
'/~https://github.com/b4dnewz/webpage-capture'
])
console.log(res);
})().catch(console.log)
.then(capturer.close())
Don't forget to close the capturer once you have done, otherwise the headless browser instance will not disconnect correctly.
The constructor can also take options to set default values for all the subsequent captures:
const capturer = new WebCapture({
// default options
})
Type: String
Default value: process.cwd()
Specify a custom directory where place the captured files.
Type: Number
Default value: 30000
Specify the default page timeout to load a page (in ms).
Type: Object
Set the headers to be used during all the requests.
Type: String, Object
Set the default viewport that is used when not specified.
Type: Boolean
Default value: false
Run the script in headfull mode with a delay of 1 second so you can see what is doing.
Type: Array
Default value: []
Custom launch arguments to be passed to Chromium instance, if you are a linux user and you are experiencing some issues, you may want to disable sandbox or setup differently.
Capture a screnshot of the given input
and save it to the given outputFilePath
.
Returns a Promise<void>
that resolves when the screenshot is written.
Capture a screnshot of the given input
.
Returns a Promise<Buffer>
with the screenshot as binary.
Capture a screnshot of the given input
.
Returns a Promise<string>
with the screenshot as Base64.
Capture one or multiple input
using the given options.
For a complete list of options see below.
Returns a Promise<void>
that resolves when the screenshot is written.
Type: String
Default value: png
Possible values: png, jpeg, pdf, html, buffer, base64
Select the capture output type.
Type: Number
The timeout to use when capturing input.
Type: Object
An object of headers to use when capturing input.
await capturer.capture('/~https://github.com/b4dnewz/webpage-capture', {
headers: {
'x-powered-by': 'webpage-capture'
}
})
Type: Number
Wait for the specified time before capturing the page. (in milliseconds)
Type: String
Wait until the selector is visible into page.
Type: Boolean
Capture the full scrollable page, not just the visible viewport.
Type: String[]
Inject scripts into the page.
await capturer.capture('/~https://github.com/b4dnewz/webpage-capture', {
scripts: [
'http://example.com/some/remote/file.js',
'./local-file.js',
'document.body.style.backgroundColor = "hotpink";'
]
})
Type: String[]
Inject styles into the page.
await capturer.capture('/~https://github.com/b4dnewz/webpage-capture', {
styles: [
'http://example.com/some/remote/file.css',
'./local-file.css',
'body { background-color: "hotpink"; }'
]
})
Type: String, String[]
One or more viewport to capture.
// capture single viewport
await capturer.capture('/~https://github.com/b4dnewz/webpage-capture', {
viewport: 'nexus-5'
})
// capture multiple viewports
await capturer.capture('/~https://github.com/b4dnewz/webpage-capture', {
viewport: ['nexus-5', 'nexus-10']
})
Type: String
Default value: false
Capture all viewports that match the category name.
// capture all mobile viewports
await capturer.capture('/~https://github.com/b4dnewz/webpage-capture', {
viewportCategory: 'mobile'
})
MIT © Filippo Conti
- Create an issue and describe your idea
- Fork the project (/~https://github.com/b4dnewz/webpage-capture/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Write some tests and run it (
npm run test'
) - Publish the branch (
git push origin my-new-feature
) - Create a new Pull Request