Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding setCookies option to a page instance #49

Merged
merged 5 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,37 @@ only really makes sense if you're calling Grover directly (and not via middlewar
Grover.new('<some URI with basic authentication', username: 'the username', password: 'super secret').to_pdf
```

#### Adding cookies
To set request cookies when requesting a URL, pass an array of hashes as such
_N.B._ Only the `name` and `value` properties are required.
See [page.setCookies](https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagesetcookiecookies) documentation for more details.

```ruby
myCookies = [
{ name: 'sign_username', value: 'any@any.com', domain: 'mydomain' },
{ name: '_session_id', value: '9c014df0b699d8dc08d1c472f8cc594c', domain: 'mydomain' }
]
Grover.new('<some URI with cookies', cookies: myCookies).to_pdf
```

If you need to forward the cookies from the original request, you could extract them as such:

```ruby
def header_cookies
request.headers['Cookie'].split('; ').map do |cookie|
key, value = cookie.split '='
{ name: key, value: value, domain: request.headers['Host'] }
end
end
```

And give that array to Grover:

```ruby
Grover.new('<some URI with cookies', cookies: header_cookies).to_pdf
```


#### Page URL for middleware requests (or passing through raw HTML)
If you want to have the header or footer display the page URL, Grover requires that this is passed through via the
`display_url` option. This is because the page URL is not available in the raw HTML!
Expand Down
6 changes: 6 additions & 0 deletions lib/grover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def self.convert_function(convert_action)
await page.authenticate({ username, password });
}

// Setting cookies
const cookies = options.cookies; delete options.cookies
if (Array.isArray(cookies)) {
await page.setCookie(...cookies);
}

// Set caching flag (if provided)
const cache = options.cache; delete options.cache;
if (cache != undefined) {
Expand Down