From 94b7a2268c50e9bf82eccd5f13c6da126e71c78a Mon Sep 17 00:00:00 2001 From: Abdud Dayan Adeeb Date: Sun, 29 Oct 2017 01:45:30 -0400 Subject: [PATCH] Add setExtraHTTPHeaders functionality --- README.md | 1 + docs/api.md | 20 ++++++++++++++++++++ src/api.ts | 7 +++++++ src/chrome/local-runtime.ts | 8 ++++++++ src/types.ts | 6 ++++++ src/util.ts | 10 +++++++++- 6 files changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d554596a..1809e464 100644 --- a/README.md +++ b/README.md @@ -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(fn: (...args: any[]) => void, ...args: any[])`](docs/api.md#api-evaluate) - [`inputValue(selector: string)`](docs/api.md#api-inputvalue) diff --git a/docs/api.md b/docs/api.md index 31c00ba0..fa4a2225 100644 --- a/docs/api.md +++ b/docs/api.md @@ -357,6 +357,26 @@ __Example__ await chromeless.setHtml('

Hello world!

') ``` + --------------------------------------- + + + +### setExtraHTTPHeaders(headers: Headers): Chromeless + +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' +}) +``` + + --------------------------------------- diff --git a/src/api.ts b/src/api.ts index 940d17df..6afda553 100644 --- a/src/api.ts +++ b/src/api.ts @@ -3,6 +3,7 @@ import ChromeRemote from './chrome/remote' import Queue from './queue' import { ChromelessOptions, + Headers, Cookie, CookieQuery, PdfOptions, @@ -187,6 +188,12 @@ export default class Chromeless implements Promise { return this } + setExtraHTTPHeaders(headers: Headers): Chromeless { + this.queue.enqueue({ type: 'setExtraHTTPHeaders', headers }) + + return this + } + evaluate( fn: (...args: any[]) => void, ...args: any[] diff --git a/src/chrome/local-runtime.ts b/src/chrome/local-runtime.ts index 1f56b3aa..455efb95 100644 --- a/src/chrome/local-runtime.ts +++ b/src/chrome/local-runtime.ts @@ -2,6 +2,7 @@ import { Client, Command, ChromelessOptions, + Headers, Cookie, CookieQuery, PdfOptions, @@ -21,6 +22,7 @@ import { scrollTo, scrollToElement, setHtml, + setExtraHTTPHeaders, press, setViewport, clearCookies, @@ -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': @@ -293,6 +297,10 @@ export default class LocalRuntime { return await getAllCookies(this.client) } + async setExtraHTTPHeaders(headers: Headers): Promise { + return await setExtraHTTPHeaders(this.client, headers) + } + async setCookies( nameOrCookies: string | Cookie | Cookie[], value?: string, diff --git a/src/types.ts b/src/types.ts index 394fc35c..8338d9d1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -126,6 +126,10 @@ export type Command = type: 'setHtml' html: string } + | { + type: 'setExtraHTTPHeaders' + headers: Headers + } | { type: 'press' keyCode: number @@ -179,6 +183,8 @@ export type Command = files: string[] } +export type Headers = Record + export interface Cookie { url?: string domain?: string diff --git a/src/util.ts b/src/util.ts index d80501b1..898f80dc 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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' @@ -362,6 +362,14 @@ export async function setCookies( } } +export async function setExtraHTTPHeaders( + client: Client, + headers: Headers, +): Promise { + const { Network } = client + await Network.setExtraHTTPHeaders({ headers }) +} + export async function mousedown( client: Client, selector: string,