capture-website
Advanced tools
Comparing version 0.1.0 to 0.1.1
26
index.js
@@ -5,8 +5,10 @@ 'use strict'; | ||
const fileUrl = require('file-url'); | ||
const isUrl = require('is-url-superb'); | ||
const puppeteer = require('puppeteer'); | ||
const devices = require('puppeteer/DeviceDescriptors'); | ||
const toughCookie = require('tough-cookie'); | ||
const writeFile = promisify(fs.writeFile); | ||
const isUrl = string => /^(https?|file):\/\/|^data:/.test(string); | ||
const hideElements = elements => { | ||
@@ -29,2 +31,21 @@ for (const element of elements) { | ||
const parseCookie = (url, cookie) => { | ||
if (typeof cookie === 'object') { | ||
return cookie; | ||
} | ||
const jar = new toughCookie.CookieJar(undefined, {rejectPublicSuffixes: false}); | ||
jar.setCookieSync(cookie, url); | ||
const ret = jar.serializeSync().cookies[0]; | ||
// Use this instead of the above when the following issue is fixed: | ||
// https://github.com/salesforce/tough-cookie/issues/149 | ||
// const ret = toughCookie.parse(cookie).serializeSync(); | ||
ret.name = ret.key; | ||
delete ret.key; | ||
return ret; | ||
}; | ||
const captureWebsite = async (url, options) => { | ||
@@ -84,3 +105,4 @@ const finalUrl = isUrl(url) ? url : fileUrl(url); | ||
if (options.cookies) { | ||
await page.setCookie(...options.cookies); | ||
const cookies = options.cookies.map(cookie => parseCookie(finalUrl, cookie)); | ||
await page.setCookie(...cookies); | ||
} | ||
@@ -87,0 +109,0 @@ |
{ | ||
"name": "capture-website", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Capture screenshots of websites", | ||
@@ -38,4 +38,4 @@ "license": "MIT", | ||
"file-url": "^2.0.2", | ||
"is-url-superb": "^2.0.0", | ||
"puppeteer": "^1.12.2" | ||
"puppeteer": "^1.12.2", | ||
"tough-cookie": "^3.0.1" | ||
}, | ||
@@ -52,5 +52,4 @@ "devDependencies": { | ||
"tempy": "^0.2.1", | ||
"tough-cookie": "^3.0.1", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -52,3 +52,3 @@ # capture-website [![Build Status](https://travis-ci.com/sindresorhus/capture-website.svg?branch=master)](https://travis-ci.com/sindresorhus/capture-website) | ||
The URL or local file path to the website. | ||
The URL, file URL, data URL, or local file path to the website. | ||
@@ -285,6 +285,28 @@ ```js | ||
Type: `Object[]` | ||
Type: `Array<string | Object>` | ||
Set cookies in the [following format](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies). | ||
Set cookies in [browser string format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) or [object format](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies). | ||
Tip: Go to the website you want a cookie for and [copy-paste it from DevTools](https://stackoverflow.com/a/24961735/64949). | ||
```js | ||
const captureWebsite = require('capture-website'); | ||
(async () => { | ||
await captureWebsite.file('https://sindresorhus.com', 'screenshot.png', { | ||
cookies: [ | ||
// This format is useful for when you copy it from the browser | ||
'id=unicorn; Expires=Wed, 21 Oct 2018 07:28:00 GMT;', | ||
// This format is useful for when you have to manually create a cookie | ||
{ | ||
name: 'id', | ||
value: 'unicorn', | ||
expires: Math.round(new Date('2018-10-21').getTime() / 1000) | ||
} | ||
] | ||
}); | ||
})(); | ||
``` | ||
##### authentication | ||
@@ -291,0 +313,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17495
10
165
405
+ Addedtough-cookie@^3.0.1
+ Addedip-regex@2.1.0(transitive)
+ Addedpsl@1.10.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedtough-cookie@3.0.1(transitive)
- Removedis-url-superb@^2.0.0
- Removedip-regex@1.0.3(transitive)
- Removedis-url-superb@2.0.0(transitive)
- Removedurl-regex@3.2.0(transitive)