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 4 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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@ 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
If you need to set cookies to your new page instance, just pass them along like this (please note that the only required properties are `name` and `value`. Check [.setCookies](https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagesetcookiecookies) docs):

```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, let's say, you wanted to just forward the cookies that you got on your original request, you can do something like:

```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
myCookiesFromRequest = header_cookies(request)
Grover.new('<some URI with cookies', cookies: myCookiesFromRequest).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 (cookies != undefined && Array.isArray(cookies)) {
await page.setCookie(...cookies);
}

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