Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #325 from addnab/master
Browse files Browse the repository at this point in the history
Allow setting of `headers`
  • Loading branch information
schickling authored Nov 3, 2017
2 parents 6fca427 + 94b7a22 commit 219e34d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const chromeless = new Chromeless({
- [`scrollTo(x: number, y: number)`](docs/api.md#api-scrollto)
- [`scrollToElement(selector: string)`](docs/api.md#api-scrolltoelement)
- [`setHtml(html: string)`](docs/api.md#api-sethtml)
- [`setExtraHTTPHeaders(headers: Headers)`](docs/api.md#api-setextrahttpheaders)
- [`setViewport(options: DeviceMetrics)`](docs/api.md#api-setviewport)
- [`evaluate<U extends any>(fn: (...args: any[]) => void, ...args: any[])`](docs/api.md#api-evaluate)
- [`inputValue(selector: string)`](docs/api.md#api-inputvalue)
Expand Down
20 changes: 20 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,26 @@ __Example__
await chromeless.setHtml('<h1>Hello world!</h1>')
```

---------------------------------------

<a name="api-setextrahttpheaders" />

### setExtraHTTPHeaders(headers: Headers): Chromeless<T>

Sets extra HTTP headers.

__Arguments__
- `headers` - headers as keys / values of JSON object

__Example__

```js
await chromeless.setExtraHTTPHeaders({
'accept-language': 'en-US,en;q=0.8'
})
```


---------------------------------------

<a name="api-setviewport" />
Expand Down
7 changes: 7 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ChromeRemote from './chrome/remote'
import Queue from './queue'
import {
ChromelessOptions,
Headers,
Cookie,
CookieQuery,
PdfOptions,
Expand Down Expand Up @@ -187,6 +188,12 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

setExtraHTTPHeaders(headers: Headers): Chromeless<T> {
this.queue.enqueue({ type: 'setExtraHTTPHeaders', headers })

return this
}

evaluate<U extends any>(
fn: (...args: any[]) => void,
...args: any[]
Expand Down
8 changes: 8 additions & 0 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Client,
Command,
ChromelessOptions,
Headers,
Cookie,
CookieQuery,
PdfOptions,
Expand All @@ -21,6 +22,7 @@ import {
scrollTo,
scrollToElement,
setHtml,
setExtraHTTPHeaders,
press,
setViewport,
clearCookies,
Expand Down Expand Up @@ -96,6 +98,8 @@ export default class LocalRuntime {
return this.clearCookies()
case 'setHtml':
return this.setHtml(command.html)
case 'setExtraHTTPHeaders':
return this.setExtraHTTPHeaders(command.headers)
case 'cookies':
return this.cookies(command.nameOrQuery)
case 'allCookies':
Expand Down Expand Up @@ -293,6 +297,10 @@ export default class LocalRuntime {
return await getAllCookies(this.client)
}

async setExtraHTTPHeaders(headers: Headers): Promise<void> {
return await setExtraHTTPHeaders(this.client, headers)
}

async setCookies(
nameOrCookies: string | Cookie | Cookie[],
value?: string,
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export type Command =
type: 'setHtml'
html: string
}
| {
type: 'setExtraHTTPHeaders'
headers: Headers
}
| {
type: 'press'
keyCode: number
Expand Down Expand Up @@ -179,6 +183,8 @@ export type Command =
files: string[]
}

export type Headers = Record<string, string>

export interface Cookie {
url?: string
domain?: string
Expand Down
10 changes: 9 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as cuid from 'cuid'
import { Client, Cookie, DeviceMetrics, PdfOptions, BoxModel, Viewport } from './types'
import { Client, Cookie, DeviceMetrics, PdfOptions, BoxModel, Viewport, Headers } from './types'
import * as CDP from 'chrome-remote-interface'
import * as AWS from 'aws-sdk'

Expand Down Expand Up @@ -362,6 +362,14 @@ export async function setCookies(
}
}

export async function setExtraHTTPHeaders(
client: Client,
headers: Headers,
): Promise<void> {
const { Network } = client
await Network.setExtraHTTPHeaders({ headers })
}

export async function mousedown(
client: Client,
selector: string,
Expand Down

0 comments on commit 219e34d

Please sign in to comment.