-
Notifications
You must be signed in to change notification settings - Fork 660
/
index.js
114 lines (85 loc) · 4 KB
/
index.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import OS from 'os-family';
import { parse as parseUrl } from 'url';
import dedicatedProviderBase from '../base';
import ChromeRunTimeInfo from './runtime-info';
import getConfig from './config';
import { start as startLocalChrome, stop as stopLocalChrome } from './local-chrome';
import * as cdp from './cdp';
import { GET_WINDOW_DIMENSIONS_INFO_SCRIPT } from '../../../utils/client-functions';
const MIN_AVAILABLE_DIMENSION = 50;
export default {
...dedicatedProviderBase,
_getConfig (name) {
return getConfig(name);
},
_getBrowserProtocolClient () {
return cdp;
},
async _createRunTimeInfo (hostName, configString, allowMultipleWindows) {
return ChromeRunTimeInfo.create(hostName, configString, allowMultipleWindows);
},
async openBrowser (browserId, pageUrl, configString, allowMultipleWindows) {
const parsedPageUrl = parseUrl(pageUrl);
const runtimeInfo = await this._createRunTimeInfo(parsedPageUrl.hostname, configString, allowMultipleWindows);
runtimeInfo.browserName = this._getBrowserName();
runtimeInfo.browserId = browserId;
runtimeInfo.providerMethods = {
resizeLocalBrowserWindow: (...args) => this.resizeLocalBrowserWindow(...args)
};
await startLocalChrome(pageUrl, runtimeInfo);
await this.waitForConnectionReady(browserId);
if (allowMultipleWindows)
runtimeInfo.activeWindowId = this.calculateWindowId();
await cdp.createClient(runtimeInfo);
this.openedBrowsers[browserId] = runtimeInfo;
},
async resizeWindowAfterOpeningBrowser (browserId) {
const runtimeInfo = this.openedBrowsers[browserId];
runtimeInfo.viewportSize = await this.runInitScript(browserId, GET_WINDOW_DIMENSIONS_INFO_SCRIPT);
await this._ensureWindowIsExpanded(browserId, runtimeInfo.viewportSize);
},
async closeBrowser (browserId) {
const runtimeInfo = this.openedBrowsers[browserId];
if (cdp.isHeadlessTab(runtimeInfo))
await cdp.closeTab(runtimeInfo);
else
await this.closeLocalBrowser(browserId);
if (OS.mac || runtimeInfo.config.headless)
await stopLocalChrome(runtimeInfo);
if (runtimeInfo.tempProfileDir)
await runtimeInfo.tempProfileDir.dispose();
delete this.openedBrowsers[browserId];
},
async resizeWindow (browserId, width, height, currentWidth, currentHeight) {
const runtimeInfo = this.openedBrowsers[browserId];
if (runtimeInfo.config.mobile)
await cdp.updateMobileViewportSize(runtimeInfo);
else {
runtimeInfo.viewportSize.width = currentWidth;
runtimeInfo.viewportSize.height = currentHeight;
}
await cdp.resizeWindow({ width, height }, runtimeInfo);
},
async getVideoFrameData (browserId) {
return await cdp.getScreenshotData(this.openedBrowsers[browserId]);
},
async hasCustomActionForBrowser (browserId) {
const { config, client } = this.openedBrowsers[browserId];
return {
hasCloseBrowser: true,
hasResizeWindow: !!client && (config.emulation || config.headless),
hasMaximizeWindow: !!client && config.headless,
hasTakeScreenshot: !!client,
hasChromelessScreenshots: !!client,
hasGetVideoFrameData: !!client,
hasCanResizeWindowToDimensions: false
};
},
async _ensureWindowIsExpanded (browserId, { height, width, availableHeight, availableWidth, outerWidth, outerHeight }) {
if (height < MIN_AVAILABLE_DIMENSION || width < MIN_AVAILABLE_DIMENSION) {
const newHeight = Math.max(availableHeight, MIN_AVAILABLE_DIMENSION);
const newWidth = Math.max(Math.floor(availableWidth / 2), MIN_AVAILABLE_DIMENSION);
await this.resizeWindow(browserId, newWidth, newHeight, outerWidth, outerHeight);
}
}
};