UNPKG

12.3 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [0, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37Object.defineProperty(exports, "__esModule", { value: true });
38var CDP = require("chrome-remote-interface");
39var chrome_launcher_1 = require("chrome-launcher");
40var local_runtime_1 = require("./local-runtime");
41var util_1 = require("../util");
42var LocalChrome = /** @class */ (function () {
43 function LocalChrome(options) {
44 if (options === void 0) { options = {}; }
45 this.options = options;
46 this.runtimeClientPromise = this.initRuntimeClient();
47 }
48 LocalChrome.prototype.initRuntimeClient = function () {
49 return __awaiter(this, void 0, void 0, function () {
50 var client, _a, _b, viewport, runtime;
51 return __generator(this, function (_c) {
52 switch (_c.label) {
53 case 0:
54 if (!this.options.launchChrome) return [3 /*break*/, 2];
55 return [4 /*yield*/, this.startChrome()];
56 case 1:
57 _a = _c.sent();
58 return [3 /*break*/, 4];
59 case 2: return [4 /*yield*/, this.connectToChrome()];
60 case 3:
61 _a = _c.sent();
62 _c.label = 4;
63 case 4:
64 client = _a;
65 _b = this.options.viewport, viewport = _b === void 0 ? {} : _b;
66 return [4 /*yield*/, util_1.setViewport(client, viewport)];
67 case 5:
68 _c.sent();
69 runtime = new local_runtime_1.default(client, this.options);
70 return [2 /*return*/, { client: client, runtime: runtime }];
71 }
72 });
73 });
74 };
75 LocalChrome.prototype.startChrome = function () {
76 return __awaiter(this, void 0, void 0, function () {
77 var port, _a, target;
78 return __generator(this, function (_b) {
79 switch (_b.label) {
80 case 0:
81 port = this.options.cdp.port;
82 _a = this;
83 return [4 /*yield*/, chrome_launcher_1.launch({
84 logLevel: this.options.debug ? 'info' : 'silent',
85 chromeFlags: [
86 // Do not render scroll bars
87 '--hide-scrollbars',
88 // The following options copied verbatim from https://github.com/GoogleChrome/chrome-launcher/blob/master/src/flags.ts
89 // Disable built-in Google Translate service
90 '--disable-translate',
91 // Disable all chrome extensions entirely
92 '--disable-extensions',
93 // Disable various background network services, including extension updating,
94 // safe browsing service, upgrade detector, translate, UMA
95 '--disable-background-networking',
96 // Disable fetching safebrowsing lists, likely redundant due to disable-background-networking
97 '--safebrowsing-disable-auto-update',
98 // Disable syncing to a Google account
99 '--disable-sync',
100 // Disable reporting to UMA, but allows for collection
101 '--metrics-recording-only',
102 // Disable installation of default apps on first run
103 '--disable-default-apps',
104 // Mute any audio
105 '--mute-audio',
106 // Skip first run wizards
107 '--no-first-run',
108 ],
109 port: port,
110 })];
111 case 1:
112 _a.chromeInstance = _b.sent();
113 return [4 /*yield*/, CDP.New({
114 port: port,
115 })];
116 case 2:
117 target = _b.sent();
118 return [4 /*yield*/, CDP({ target: target, port: port })];
119 case 3: return [2 /*return*/, _b.sent()];
120 }
121 });
122 });
123 };
124 LocalChrome.prototype.connectToChrome = function () {
125 return __awaiter(this, void 0, void 0, function () {
126 var _a, host, port, target;
127 return __generator(this, function (_b) {
128 switch (_b.label) {
129 case 0:
130 _a = this.options.cdp, host = _a.host, port = _a.port;
131 return [4 /*yield*/, CDP.New({
132 port: port,
133 host: host,
134 })];
135 case 1:
136 target = _b.sent();
137 return [4 /*yield*/, CDP({ target: target, host: host, port: port })];
138 case 2: return [2 /*return*/, _b.sent()];
139 }
140 });
141 });
142 };
143 LocalChrome.prototype.setViewport = function (client) {
144 return __awaiter(this, void 0, void 0, function () {
145 var _a, viewport, config, _b, host, port, versionResult, isHeadless, _c, _d;
146 return __generator(this, function (_e) {
147 switch (_e.label) {
148 case 0:
149 _a = this.options.viewport, viewport = _a === void 0 ? {} : _a;
150 config = {
151 deviceScaleFactor: 1,
152 mobile: false,
153 scale: viewport.scale || 1,
154 fitWindow: false,
155 };
156 _b = this.options.cdp, host = _b.host, port = _b.port;
157 return [4 /*yield*/, CDP.Version({ host: host, port: port })];
158 case 1:
159 versionResult = _e.sent();
160 isHeadless = versionResult['User-Agent'].includes('Headless');
161 if (!(viewport.height && viewport.width)) return [3 /*break*/, 2];
162 config.height = viewport.height;
163 config.width = viewport.width;
164 return [3 /*break*/, 6];
165 case 2:
166 if (!isHeadless) return [3 /*break*/, 3];
167 // just apply default value in headless mode to maintain original browser viewport
168 config.height = 900;
169 config.width = 1440;
170 return [3 /*break*/, 6];
171 case 3:
172 _c = config;
173 return [4 /*yield*/, util_1.evaluate(client, (function () { return window.innerHeight; }).toString())];
174 case 4:
175 _c.height = _e.sent();
176 _d = config;
177 return [4 /*yield*/, util_1.evaluate(client, (function () { return window.innerWidth; }).toString())];
178 case 5:
179 _d.width = _e.sent();
180 _e.label = 6;
181 case 6: return [4 /*yield*/, client.Emulation.setDeviceMetricsOverride(config)];
182 case 7:
183 _e.sent();
184 return [4 /*yield*/, client.Emulation.setVisibleSize({
185 width: config.width,
186 height: config.height,
187 })];
188 case 8:
189 _e.sent();
190 return [2 /*return*/];
191 }
192 });
193 });
194 };
195 LocalChrome.prototype.process = function (command) {
196 return __awaiter(this, void 0, void 0, function () {
197 var runtime;
198 return __generator(this, function (_a) {
199 switch (_a.label) {
200 case 0: return [4 /*yield*/, this.runtimeClientPromise];
201 case 1:
202 runtime = (_a.sent()).runtime;
203 return [4 /*yield*/, runtime.run(command)];
204 case 2: return [2 /*return*/, (_a.sent())];
205 }
206 });
207 });
208 };
209 LocalChrome.prototype.close = function () {
210 return __awaiter(this, void 0, void 0, function () {
211 var client, _a, host, port;
212 return __generator(this, function (_b) {
213 switch (_b.label) {
214 case 0: return [4 /*yield*/, this.runtimeClientPromise];
215 case 1:
216 client = (_b.sent()).client;
217 if (!this.options.cdp.closeTab) return [3 /*break*/, 3];
218 _a = this.options.cdp, host = _a.host, port = _a.port;
219 return [4 /*yield*/, CDP.Close({ host: host, port: port, id: client.target.id })];
220 case 2:
221 _b.sent();
222 _b.label = 3;
223 case 3:
224 if (this.chromeInstance) {
225 this.chromeInstance.kill();
226 }
227 return [4 /*yield*/, client.close()];
228 case 4:
229 _b.sent();
230 return [2 /*return*/];
231 }
232 });
233 });
234 };
235 return LocalChrome;
236}());
237exports.default = LocalChrome;
238//# sourceMappingURL=local.js.map
\No newline at end of file