This repository has been archived by the owner on Nov 24, 2018. It is now read-only.
/
local.ts
85 lines (67 loc) 路 2.29 KB
/
local.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Chrome, Command, ChromelessOptions, Client } from '../types'
import * as CDP from 'chrome-remote-interface'
import LocalRuntime from './local-runtime'
import { evaluate } from '../util'
interface RuntimeClient {
client: Client
runtime: LocalRuntime
}
export default class LocalChrome implements Chrome {
private options: ChromelessOptions
private runtimeClientPromise: Promise<RuntimeClient>
constructor(options: ChromelessOptions = {}) {
this.options = options
this.runtimeClientPromise = this.initRuntimeClient()
}
private async initRuntimeClient(): Promise<RuntimeClient> {
const target = await CDP.New()
const client = await CDP({ target })
await this.setViewport(client)
const runtime = new LocalRuntime(client, this.options)
return { client, runtime }
}
private async setViewport(client: Client) {
const { viewport = {} } = this.options
const config: any = {
deviceScaleFactor: 1,
mobile: false,
scale: viewport.scale || 1,
fitWindow: false, // as we cannot resize the window, `fitWindow: false` is needed in order for the viewport to be resizable
}
const versionResult = await CDP.Version()
const isHeadless = versionResult['User-Agent'].includes('Headless')
if (viewport.height && viewport.width) {
config.height = viewport.height
config.width = viewport.width
} else if (isHeadless) {
// just apply default value in headless mode to maintain original browser viewport
config.height = 900
config.width = 1440
} else {
config.height = await evaluate(
client,
(() => window.innerHeight).toString()
)
config.width = await evaluate(
client,
(() => window.innerWidth).toString()
)
}
await client.Emulation.setDeviceMetricsOverride(config)
await client.Emulation.setVisibleSize({
width: config.width,
height: config.height,
})
}
async process<T extends any>(command: Command): Promise<T> {
const { runtime } = await this.runtimeClientPromise
return (await runtime.run(command)) as T
}
async close(): Promise<void> {
const { client } = await this.runtimeClientPromise
if (this.options.cdp.closeTab) {
CDP.Close({ id: client.target.id })
}
await client.close()
}
}