Guest User

Untitled

a guest
Jan 21st, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. async init(headless) {
  2. const browser = await puppeteer.launch({
  3. headless
  4. });
  5. const page = await browser.newPage();
  6. // do more page stuff before loading, ie: user agent and so on
  7. return {
  8. page,
  9. browser
  10. };
  11. }
  12.  
  13. // will take care of our login using headful
  14. async getLoginCookies() {
  15. const {
  16. page,
  17. browser
  18. } = await this.init(false)
  19.  
  20. // asume we load page and login here using some method
  21. // and the website sets some cookie
  22. await page.goto('http://httpbin.org/cookies/set/authenticated/true')
  23.  
  24. // store the cookie somewhere
  25. this.cookies = await page.cookies() // the cookies are collected as array
  26.  
  27. // close the page and browser, we are done with this
  28. await page.close();
  29. await browser.close();
  30. return true;
  31. }
  32.  
  33. // continue with our normal headless stuff
  34. async useHeadless() {
  35. const {
  36. page,
  37. browser
  38. } = await this.init(true)
  39.  
  40. // we set all cookies we got previously
  41. await page.setCookie(...this.cookies) // three dots represents spread syntax. The cookies are contained in a array.
  42.  
  43. // verify the cookies are working properly
  44. await page.goto('http://httpbin.org/cookies');
  45. const content = await page.$eval('body', e => e.innerText)
  46. console.log(content)
  47.  
  48. // do other stuff
  49. // close the page and browser, we are done with this
  50. // deduplicate this however you like
  51. await page.close();
  52. await browser.close();
  53. return true;
  54. }
  55.  
  56. // let's use this
  57.  
  58. (async () => {
  59. const loginTester = new myAwesomePuppeteer()
  60. await loginTester.getLoginCookies()
  61. await loginTester.useHeadless()
  62. })()
  63.  
  64. const puppeteer = require('puppeteer');
  65.  
  66. class myAwesomePuppeteer {
  67. constructor() {
  68. // keeps the cookies on the class scope
  69. this.cookies;
  70. }
  71.  
  72. // creates a browser instance and applies all kind of setup
  73. async init(headless) {
  74. const browser = await puppeteer.launch({
  75. headless
  76. });
  77. const page = await browser.newPage();
  78. // do more page stuff before loading, ie: user agent and so on
  79. return {
  80. page,
  81. browser
  82. };
  83. }
  84.  
  85. // will take care of our login using headful
  86. async getLoginCookies() {
  87. const {
  88. page,
  89. browser
  90. } = await this.init(false)
  91.  
  92. // asume we load page and login here using some method
  93. // and the website sets some cookie
  94. await page.goto('http://httpbin.org/cookies/set/authenticated/true')
  95.  
  96. // store the cookie somewhere
  97. this.cookies = await page.cookies()
  98.  
  99. // close the page and browser, we are done with this
  100. await page.close();
  101. await browser.close();
  102. return true;
  103. }
  104.  
  105. // continue with our normal headless stuff
  106. async useHeadless() {
  107. const {
  108. page,
  109. browser
  110. } = await this.init(true)
  111.  
  112. // we set all cookies we got previously
  113. await page.setCookie(...this.cookies)
  114.  
  115. // verify the cookies are working properly
  116. await page.goto('http://httpbin.org/cookies');
  117. const content = await page.$eval('body', e => e.innerText)
  118. console.log(content)
  119.  
  120. // do other stuff
  121. // close the page and browser, we are done with this
  122. // deduplicate this however you like
  123. await page.close();
  124. await browser.close();
  125. return true;
  126. }
  127. }
  128.  
  129. // let's use this
  130. (async () => {
  131. const loginTester = new myAwesomePuppeteer()
  132. await loginTester.getLoginCookies()
  133. await loginTester.useHeadless()
  134. })()
  135.  
  136. ➜ node app.js
  137. {
  138. "cookies": {
  139. "authenticated": "true"
  140. }
  141. }
Add Comment
Please, Sign In to add comment