3

I am using node.js and i have access to my signed cookies obviously.

They are in this form:

{ 'connect.sid': 's:qX4ZrttrjydtrjkgsdghsdghrewynZj4Ew2OUh.tTSILkcvgsegsegsegsr99gmW5
0XLcJefM' }

Puppeteer supports cookies and has this function in order to pass the parameters of the cookie:

page.setCookie(...cookies)
...cookies <...Object>
name <string> required
value <string> required
url <string>
domain <string>
path <string>
expires <number> Unix time in seconds.
httpOnly <boolean>
secure <boolean>
sameSite <string> "Strict" or "Lax".
returns: <Promise>

As you can see, you provide the parameters of each field directly. Is there a way to pass my signed cookie directly to puppeteer?

user1584421
  • 3,499
  • 11
  • 46
  • 86

1 Answers1

7

You have to extract and use the proper cookie format.

  1. First install this extension called EditThisCookie.
  2. Then export your cookie. You will get an array of cookies. enter image description here

  3. Use ... spread to pass all cookies as argument to setCookie.

    await page.setCookie(...cookies)
    

Done!

If you want to write the cookies, then here is a format for that.

const cookies = [
 {
    "domain": "localhost", // google.com, yahoo.com etc. Without the host
    "hostOnly": true,
    "httpOnly": true,
    "name": "connect.sid", // here is the actual cookie name
    "path": "/",
    "sameSite": "no_restriction",
    "secure": false,
    "session": true,
    "storeId": "0",
    "value": "s%3AliYZ-M8urEQLfgn2_kSG_FIPwVTr5VQs.5rrJW7hzuXebekzTRgPYFTYri5nljhGCp8Dz%2FgLoSN4", // and the value
    "id": 1
 }
]

Working example

const cookie = {
    name: 'login_email',
    value: 'set_by_cookie@domain.com',
    domain: '.paypal.com',
    url: 'https://www.paypal.com/',
    path: '/',
    httpOnly: true,
    secure: true
}

const puppeteer = require('puppeteer');


const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setCookie(cookie)
await page.goto('https://www.paypal.com/signin')
await page.screenshot({
    path: 'paypal_login.png'
})
await browser.close()

If you notice original docs, it shows page.setCookie(...cookies) cookies with three dots. Which basically means you can pass an array of objects as arguments that way.

How you put the data into cookie variable is upto you, you can hard code it, you can use some database etc. That's not related to how you pass the cookie.

If it's only one cookie like in paypal example, you pass them with page.setCookie(cookie), but if it's multiple cookie like you got from exporting using EditThisCookie or the localhost example above, then you have to use the spread operator with three dots like I've explained above.

You can read more about spread and rest on this question.

Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73
  • 2
    Thanks a lot! You showed me it's possible. However, i intend to do this process from inside node. So i cannot use an extension like this. Do you know how this is feasible from inside node.js? If i knew what this extension was doing exactly, maybe i could replicate it. – user1584421 May 18 '18 at 23:25
  • Well `page.cookies` perhaps? :D – Md. Abu Taher May 18 '18 at 23:50
  • I searched but i found nothing.. Can you provide some more info on what i do with that please? – user1584421 May 19 '18 at 05:45
  • I downloaded this addon and run some tests... So i guess "connect.sid" is the "name" field of my cookie. Then all the random characters after 's:' is the "value" of my cookie? So how do i figure out the rest of the fields from my cookie? – user1584421 May 24 '18 at 00:22
  • Sorry, but how exactly do i pass all the cookies as a parameter? – user1584421 May 28 '18 at 23:06
  • if you explort, then you would get an array. You can pass all of them just like I explained in the answer above. please read my answer above. – Md. Abu Taher May 29 '18 at 07:04
  • Thanks but i read the answer and i didnt understand it. I get an array but in the clipboard. Do i have to hardcode it then to my program? In the puppeteer docs they say to pass an object not an array. You said ti use three dots ... to pass all the cookies. How does that work. The cookies are in the clipboard. How do i pass them to the program? – user1584421 May 29 '18 at 11:42
  • I took the array of cookies and pasted it into the puppeteer code, but i got this error: C:\pupet\tryme.js:18 "domain": ".google.com", ^^^^^^^^^^^^^^^ SyntaxError: Unexpected string at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) – user1584421 May 29 '18 at 14:23
  • Added some example. – Md. Abu Taher May 29 '18 at 18:02
  • Thanks! I didnt even know about the spread and rest operator, that's why i couldn't figure it out. Although i wanted a working example with an array, and not a single cookie, it's alright i will research more. – user1584421 May 30 '18 at 09:21
  • You're a legend. – Reinier68 Oct 19 '20 at 18:12