Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async init(headless) {
- const browser = await puppeteer.launch({
- headless
- });
- const page = await browser.newPage();
- // do more page stuff before loading, ie: user agent and so on
- return {
- page,
- browser
- };
- }
- // will take care of our login using headful
- async getLoginCookies() {
- const {
- page,
- browser
- } = await this.init(false)
- // asume we load page and login here using some method
- // and the website sets some cookie
- await page.goto('http://httpbin.org/cookies/set/authenticated/true')
- // store the cookie somewhere
- this.cookies = await page.cookies() // the cookies are collected as array
- // close the page and browser, we are done with this
- await page.close();
- await browser.close();
- return true;
- }
- // continue with our normal headless stuff
- async useHeadless() {
- const {
- page,
- browser
- } = await this.init(true)
- // we set all cookies we got previously
- await page.setCookie(...this.cookies) // three dots represents spread syntax. The cookies are contained in a array.
- // verify the cookies are working properly
- await page.goto('http://httpbin.org/cookies');
- const content = await page.$eval('body', e => e.innerText)
- console.log(content)
- // do other stuff
- // close the page and browser, we are done with this
- // deduplicate this however you like
- await page.close();
- await browser.close();
- return true;
- }
- // let's use this
- (async () => {
- const loginTester = new myAwesomePuppeteer()
- await loginTester.getLoginCookies()
- await loginTester.useHeadless()
- })()
- const puppeteer = require('puppeteer');
- class myAwesomePuppeteer {
- constructor() {
- // keeps the cookies on the class scope
- this.cookies;
- }
- // creates a browser instance and applies all kind of setup
- async init(headless) {
- const browser = await puppeteer.launch({
- headless
- });
- const page = await browser.newPage();
- // do more page stuff before loading, ie: user agent and so on
- return {
- page,
- browser
- };
- }
- // will take care of our login using headful
- async getLoginCookies() {
- const {
- page,
- browser
- } = await this.init(false)
- // asume we load page and login here using some method
- // and the website sets some cookie
- await page.goto('http://httpbin.org/cookies/set/authenticated/true')
- // store the cookie somewhere
- this.cookies = await page.cookies()
- // close the page and browser, we are done with this
- await page.close();
- await browser.close();
- return true;
- }
- // continue with our normal headless stuff
- async useHeadless() {
- const {
- page,
- browser
- } = await this.init(true)
- // we set all cookies we got previously
- await page.setCookie(...this.cookies)
- // verify the cookies are working properly
- await page.goto('http://httpbin.org/cookies');
- const content = await page.$eval('body', e => e.innerText)
- console.log(content)
- // do other stuff
- // close the page and browser, we are done with this
- // deduplicate this however you like
- await page.close();
- await browser.close();
- return true;
- }
- }
- // let's use this
- (async () => {
- const loginTester = new myAwesomePuppeteer()
- await loginTester.getLoginCookies()
- await loginTester.useHeadless()
- })()
- ➜ node app.js
- {
- "cookies": {
- "authenticated": "true"
- }
- }
Add Comment
Please, Sign In to add comment