Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/utyfua/puppeteer-tough-cookie-store
Puppeteer store for cookie management library tough cookie.
https://github.com/utyfua/puppeteer-tough-cookie-store
cookie cookiejar cookies jar puppeteer tough-cookie
Last synced: about 1 month ago
JSON representation
Puppeteer store for cookie management library tough cookie.
- Host: GitHub
- URL: https://github.com/utyfua/puppeteer-tough-cookie-store
- Owner: utyfua
- License: mit
- Created: 2022-03-23T20:52:38.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-09-06T08:56:45.000Z (2 months ago)
- Last Synced: 2024-10-01T15:03:58.061Z (about 1 month ago)
- Topics: cookie, cookiejar, cookies, jar, puppeteer, tough-cookie
- Language: TypeScript
- Homepage: https://utyfua.github.io/puppeteer-tough-cookie-store/
- Size: 86.9 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# puppeteer-tough-cookie-store
Puppeteer store for cookie management library [tough cookie](https://github.com/goinstant/tough-cookie "tough cookie").
[[library types]](https://utyfua.github.io/puppeteer-tough-cookie-store/)
Tested in `puppeteer/chromium` only!
## Installation
```
npm install puppeteer-tough-cookie-store
```or
```
yarn add puppeteer-tough-cookie-store
```## Usage example
```js
const puppeteer = require('puppeteer');
// const { getStoreByPage } = require('puppeteer-tough-cookie-store')
const { getStoreByPage } = require('../')
const { CookieJar, MemoryCookieStore } = require('tough-cookie');// optional libraries, you need only what you are really using
const got = require('got'); // make sure its got@11, cuz got@12 forcing to using imports instead requiresasync function getPage() {
// launch some browser
let browser = await puppeteer.launch({
headless: false,
args: [
'--incognito',
'--start-in-incognito',
],
});const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();return [browser, page];
}async function runtime() {
// test url
const url = 'https://www.npmjs.com/package/puppeteer-tough-cookie-store';// launch browser
let [browser, page] = await getPage();// init cookieJar
let cookieJarBrowser = new CookieJar(await getStoreByPage(page));// do base puppeteer request
await page.goto(url)
console.log('browser 1: cookieJarBrowser.getCookies', await cookieJarBrowser.getCookies(url));// do requests with cookies by another libs
await testLibraries(url, cookieJarBrowser);// detach cookies to memory storage
const cookieJarMemory = await cookieJarBrowser.clone(new MemoryCookieStore);
// now we can close the browser but we will have cookies to work
await browser.close();// do requests with cookies by another libs but without browser
await testLibraries(url, cookieJarMemory);// launch another browser
[browser, page] = await getPage();
// init cookieJar
cookieJarBrowser = new CookieJar(await getStoreByPage(page));// check for no cookies cuz we are in incognito
console.log('browser 2: cookieJarBrowser.getCookies empty', await cookieJarBrowser.getCookies(url));
// restore cookies
cookieJarBrowser = await cookieJarMemory.clone(await getStoreByPage(page));
console.log('browser 2: cookieJarBrowser.getCookies restored', await cookieJarBrowser.getCookies(url));// cookies were restored, so we can go requests again!
// do base puppeteer request
await page.goto(url)
console.log('browser 1: cookieJarBrowser.getCookies', await cookieJarBrowser.getCookies(url));// do requests with cookies by another libs
await testLibraries(url, cookieJarBrowser);// close browser at the end of our work
await browser.close();
}async function testLibraries(url, cookieJar) {
// got request library
await doGotRequest(url, cookieJar);
}// got request library
async function doGotRequest(url, cookieJar) {
// do request with got
const gotResponse = await got({
cookieJar,
url
});
// check cookies we have sent
console.log('got request header', gotResponse.request.options.headers.cookie);
// check cookies we have got
console.log('got response set-cookie header', gotResponse.headers['set-cookie']);
// already sync with jar object
console.log('cookieJar.getCookies after got request', await cookieJar.getCookies(url));
}runtime();
```## TODO:
- add tests
- documentation## License
Copyright © 2022, Utyfua. Released under the MIT License.