1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
11 | return new (P || (P = Promise))(function (resolve, reject) {
|
12 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
13 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
14 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
15 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
16 | });
|
17 | };
|
18 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
19 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
20 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
21 | function verb(n) { return function (v) { return step([n, v]); }; }
|
22 | function step(op) {
|
23 | if (f) throw new TypeError("Generator is already executing.");
|
24 | while (_) try {
|
25 | if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
|
26 | if (y = 0, t) op = [0, t.value];
|
27 | switch (op[0]) {
|
28 | case 0: case 1: t = op; break;
|
29 | case 4: _.label++; return { value: op[1], done: false };
|
30 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
31 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
32 | default:
|
33 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
34 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
35 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
36 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
37 | if (t[2]) _.ops.pop();
|
38 | _.trys.pop(); continue;
|
39 | }
|
40 | op = body.call(thisArg, _);
|
41 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
42 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
43 | }
|
44 | };
|
45 | Object.defineProperty(exports, "__esModule", { value: true });
|
46 | var fs = require("fs");
|
47 | var os = require("os");
|
48 | var path = require("path");
|
49 | var cuid = require("cuid");
|
50 | var CDP = require("chrome-remote-interface");
|
51 | var AWS = require("aws-sdk");
|
52 | exports.version = (function () {
|
53 | if (fs.existsSync(path.join(__dirname, '../package.json'))) {
|
54 |
|
55 | return require('../package.json').version;
|
56 | }
|
57 | else {
|
58 |
|
59 | return require('../../package.json').version;
|
60 | }
|
61 | })();
|
62 | function setViewport(client, viewport) {
|
63 | if (viewport === void 0) { viewport = { width: 1, height: 1, scale: 1 }; }
|
64 | return __awaiter(this, void 0, void 0, function () {
|
65 | var config, host, port, versionResult, isHeadless, _a, _b;
|
66 | return __generator(this, function (_c) {
|
67 | switch (_c.label) {
|
68 | case 0:
|
69 | config = {
|
70 | deviceScaleFactor: 1,
|
71 | mobile: false,
|
72 | scale: viewport.scale || 1,
|
73 | fitWindow: false,
|
74 | };
|
75 | host = client.host, port = client.port;
|
76 | return [4 , CDP.Version({ host: host, port: port })];
|
77 | case 1:
|
78 | versionResult = _c.sent();
|
79 | isHeadless = versionResult['User-Agent'].includes('Headless');
|
80 | if (!(viewport.height && viewport.width)) return [3 , 2];
|
81 | config.height = viewport.height;
|
82 | config.width = viewport.width;
|
83 | return [3 , 6];
|
84 | case 2:
|
85 | if (!isHeadless) return [3 , 3];
|
86 |
|
87 | config.height = 900;
|
88 | config.width = 1440;
|
89 | return [3 , 6];
|
90 | case 3:
|
91 | _a = config;
|
92 | return [4 , evaluate(client, (function () { return window.innerHeight; }).toString())];
|
93 | case 4:
|
94 | _a.height = _c.sent();
|
95 | _b = config;
|
96 | return [4 , evaluate(client, (function () { return window.innerWidth; }).toString())];
|
97 | case 5:
|
98 | _b.width = _c.sent();
|
99 | _c.label = 6;
|
100 | case 6: return [4 , client.Emulation.setDeviceMetricsOverride(config)];
|
101 | case 7:
|
102 | _c.sent();
|
103 | return [4 , client.Emulation.setVisibleSize({
|
104 | width: config.width,
|
105 | height: config.height,
|
106 | })];
|
107 | case 8:
|
108 | _c.sent();
|
109 | return [2 ];
|
110 | }
|
111 | });
|
112 | });
|
113 | }
|
114 | exports.setViewport = setViewport;
|
115 | function waitForNode(client, selector, waitTimeout) {
|
116 | return __awaiter(this, void 0, void 0, function () {
|
117 | var _this = this;
|
118 | var Runtime, getNode, result, start_1;
|
119 | return __generator(this, function (_a) {
|
120 | switch (_a.label) {
|
121 | case 0:
|
122 | Runtime = client.Runtime;
|
123 | getNode = "selector => {\n return document.querySelector(selector)\n }";
|
124 | return [4 , Runtime.evaluate({
|
125 | expression: "(" + getNode + ")(`" + selector + "`)",
|
126 | })];
|
127 | case 1:
|
128 | result = _a.sent();
|
129 | if (result.result.value === null) {
|
130 | start_1 = new Date().getTime();
|
131 | return [2 , new Promise(function (resolve, reject) {
|
132 | var interval = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
|
133 | var result;
|
134 | return __generator(this, function (_a) {
|
135 | switch (_a.label) {
|
136 | case 0:
|
137 | if (new Date().getTime() - start_1 > waitTimeout) {
|
138 | clearInterval(interval);
|
139 | reject(new Error("wait(\"" + selector + "\") timed out after " + waitTimeout + "ms"));
|
140 | }
|
141 | return [4 , Runtime.evaluate({
|
142 | expression: "(" + getNode + ")(`" + selector + "`)",
|
143 | })];
|
144 | case 1:
|
145 | result = _a.sent();
|
146 | if (result.result.value !== null) {
|
147 | clearInterval(interval);
|
148 | resolve();
|
149 | }
|
150 | return [2 ];
|
151 | }
|
152 | });
|
153 | }); }, 500);
|
154 | })];
|
155 | }
|
156 | else {
|
157 | return [2 ];
|
158 | }
|
159 | return [2 ];
|
160 | }
|
161 | });
|
162 | });
|
163 | }
|
164 | exports.waitForNode = waitForNode;
|
165 | function wait(timeout) {
|
166 | return __awaiter(this, void 0, void 0, function () {
|
167 | return __generator(this, function (_a) {
|
168 | return [2 , new Promise(function (resolve, reject) { return setTimeout(resolve, timeout); })];
|
169 | });
|
170 | });
|
171 | }
|
172 | exports.wait = wait;
|
173 | function waitForPromise(promise, waitTimeout, label) {
|
174 | return __awaiter(this, void 0, void 0, function () {
|
175 | return __generator(this, function (_a) {
|
176 | return [2 , new Promise(function (resolve, reject) {
|
177 | var fullfilled = false;
|
178 | setTimeout(function () {
|
179 | fullfilled = true;
|
180 | reject(new Error("wait(" + (label || 'Promise') + ") timed out after " + waitTimeout + "ms"));
|
181 | }, waitTimeout);
|
182 | return promise
|
183 | .then(function (res) { return (fullfilled ? void 0 : resolve(res)); })
|
184 | .catch(function (err) { return (fullfilled ? void 0 : reject(err)); });
|
185 | })];
|
186 | });
|
187 | });
|
188 | }
|
189 | exports.waitForPromise = waitForPromise;
|
190 | function eventToPromise() {
|
191 | var resolve;
|
192 | var promise = new Promise(function (res) {
|
193 | resolve = res;
|
194 | });
|
195 | return {
|
196 | onEvent: function () {
|
197 | var args = [];
|
198 | for (var _i = 0; _i < arguments.length; _i++) {
|
199 | args[_i] = arguments[_i];
|
200 | }
|
201 | resolve(args.length > 1 ? args : args[0]);
|
202 | },
|
203 | fired: function () {
|
204 | return promise;
|
205 | },
|
206 | };
|
207 | }
|
208 | exports.eventToPromise = eventToPromise;
|
209 | function nodeExists(client, selector) {
|
210 | return __awaiter(this, void 0, void 0, function () {
|
211 | var Runtime, exists, expression, result;
|
212 | return __generator(this, function (_a) {
|
213 | switch (_a.label) {
|
214 | case 0:
|
215 | Runtime = client.Runtime;
|
216 | exists = "selector => {\n return !!document.querySelector(selector)\n }";
|
217 | expression = "(" + exists + ")(`" + selector + "`)";
|
218 | return [4 , Runtime.evaluate({
|
219 | expression: expression,
|
220 | })];
|
221 | case 1:
|
222 | result = _a.sent();
|
223 | return [2 , result.result.value];
|
224 | }
|
225 | });
|
226 | });
|
227 | }
|
228 | exports.nodeExists = nodeExists;
|
229 | function getClientRect(client, selector) {
|
230 | return __awaiter(this, void 0, void 0, function () {
|
231 | var Runtime, code, expression, result;
|
232 | return __generator(this, function (_a) {
|
233 | switch (_a.label) {
|
234 | case 0:
|
235 | Runtime = client.Runtime;
|
236 | code = "selector => {\n const element = document.querySelector(selector)\n if (!element) {\n return undefined\n }\n\n const rect = element.getBoundingClientRect()\n return JSON.stringify({\n left: rect.left,\n top: rect.top,\n right: rect.right,\n bottom: rect.bottom,\n height: rect.height,\n width: rect.width,\n })\n }";
|
237 | expression = "(" + code + ")(`" + selector + "`)";
|
238 | return [4 , Runtime.evaluate({ expression: expression })];
|
239 | case 1:
|
240 | result = _a.sent();
|
241 | if (!result.result.value) {
|
242 | throw new Error("No element found for selector: " + selector);
|
243 | }
|
244 | return [2 , JSON.parse(result.result.value)];
|
245 | }
|
246 | });
|
247 | });
|
248 | }
|
249 | exports.getClientRect = getClientRect;
|
250 | function click(client, selector, scale) {
|
251 | return __awaiter(this, void 0, void 0, function () {
|
252 | var clientRect, Input, options;
|
253 | return __generator(this, function (_a) {
|
254 | switch (_a.label) {
|
255 | case 0: return [4 , getClientRect(client, selector)];
|
256 | case 1:
|
257 | clientRect = _a.sent();
|
258 | Input = client.Input;
|
259 | options = {
|
260 | x: Math.round((clientRect.left + clientRect.width / 2) * scale),
|
261 | y: Math.round((clientRect.top + clientRect.height / 2) * scale),
|
262 | button: 'left',
|
263 | clickCount: 1,
|
264 | };
|
265 | return [4 , Input.dispatchMouseEvent(__assign({}, options, { type: 'mousePressed' }))];
|
266 | case 2:
|
267 | _a.sent();
|
268 | return [4 , Input.dispatchMouseEvent(__assign({}, options, { type: 'mouseReleased' }))];
|
269 | case 3:
|
270 | _a.sent();
|
271 | return [2 ];
|
272 | }
|
273 | });
|
274 | });
|
275 | }
|
276 | exports.click = click;
|
277 | function focus(client, selector) {
|
278 | return __awaiter(this, void 0, void 0, function () {
|
279 | var DOM, dom, node;
|
280 | return __generator(this, function (_a) {
|
281 | switch (_a.label) {
|
282 | case 0:
|
283 | DOM = client.DOM;
|
284 | return [4 , DOM.getDocument()];
|
285 | case 1:
|
286 | dom = _a.sent();
|
287 | return [4 , DOM.querySelector({
|
288 | nodeId: dom.root.nodeId,
|
289 | selector: selector,
|
290 | })];
|
291 | case 2:
|
292 | node = _a.sent();
|
293 | return [4 , DOM.focus(node)];
|
294 | case 3:
|
295 | _a.sent();
|
296 | return [2 ];
|
297 | }
|
298 | });
|
299 | });
|
300 | }
|
301 | exports.focus = focus;
|
302 | function evaluate(client, fn) {
|
303 | var args = [];
|
304 | for (var _i = 2; _i < arguments.length; _i++) {
|
305 | args[_i - 2] = arguments[_i];
|
306 | }
|
307 | return __awaiter(this, void 0, void 0, function () {
|
308 | var Runtime, jsonArgs, argStr, expression, result;
|
309 | return __generator(this, function (_a) {
|
310 | switch (_a.label) {
|
311 | case 0:
|
312 | Runtime = client.Runtime;
|
313 | jsonArgs = JSON.stringify(args);
|
314 | argStr = jsonArgs.substr(1, jsonArgs.length - 2);
|
315 | expression = "\n (() => {\n const expressionResult = (" + fn + ")(" + argStr + ");\n if (expressionResult && expressionResult.then) {\n expressionResult.catch((error) => { throw new Error(error); });\n return expressionResult;\n }\n return Promise.resolve(expressionResult);\n })();\n ";
|
316 | return [4 , Runtime.evaluate({
|
317 | expression: expression,
|
318 | returnByValue: true,
|
319 | awaitPromise: true,
|
320 | })];
|
321 | case 1:
|
322 | result = _a.sent();
|
323 | if (result && result.exceptionDetails) {
|
324 | throw new Error(result.exceptionDetails.exception.value ||
|
325 | result.exceptionDetails.exception.description);
|
326 | }
|
327 | if (result && result.result) {
|
328 | return [2 , result.result.value];
|
329 | }
|
330 | return [2 , null];
|
331 | }
|
332 | });
|
333 | });
|
334 | }
|
335 | exports.evaluate = evaluate;
|
336 | function type(client, text, selector) {
|
337 | return __awaiter(this, void 0, void 0, function () {
|
338 | var Input, i, char, options;
|
339 | return __generator(this, function (_a) {
|
340 | switch (_a.label) {
|
341 | case 0:
|
342 | if (!selector) return [3 , 3];
|
343 | return [4 , focus(client, selector)];
|
344 | case 1:
|
345 | _a.sent();
|
346 | return [4 , wait(500)];
|
347 | case 2:
|
348 | _a.sent();
|
349 | _a.label = 3;
|
350 | case 3:
|
351 | Input = client.Input;
|
352 | i = 0;
|
353 | _a.label = 4;
|
354 | case 4:
|
355 | if (!(i < text.length)) return [3 , 7];
|
356 | char = text[i];
|
357 | options = {
|
358 | type: 'char',
|
359 | text: char,
|
360 | unmodifiedText: char,
|
361 | };
|
362 | return [4 , Input.dispatchKeyEvent(options)];
|
363 | case 5:
|
364 | _a.sent();
|
365 | _a.label = 6;
|
366 | case 6:
|
367 | i++;
|
368 | return [3 , 4];
|
369 | case 7: return [2 ];
|
370 | }
|
371 | });
|
372 | });
|
373 | }
|
374 | exports.type = type;
|
375 | function press(client, keyCode, count, modifiers) {
|
376 | return __awaiter(this, void 0, void 0, function () {
|
377 | var Input, options, i;
|
378 | return __generator(this, function (_a) {
|
379 | switch (_a.label) {
|
380 | case 0:
|
381 | Input = client.Input;
|
382 | if (count === undefined) {
|
383 | count = 1;
|
384 | }
|
385 | options = {
|
386 | nativeVirtualKeyCode: keyCode,
|
387 | windowsVirtualKeyCode: keyCode,
|
388 | };
|
389 | if (modifiers) {
|
390 | options['modifiers'] = modifiers;
|
391 | }
|
392 | i = 0;
|
393 | _a.label = 1;
|
394 | case 1:
|
395 | if (!(i < count)) return [3 , 5];
|
396 | return [4 , Input.dispatchKeyEvent(__assign({}, options, { type: 'rawKeyDown' }))];
|
397 | case 2:
|
398 | _a.sent();
|
399 | return [4 , Input.dispatchKeyEvent(__assign({}, options, { type: 'keyUp' }))];
|
400 | case 3:
|
401 | _a.sent();
|
402 | _a.label = 4;
|
403 | case 4:
|
404 | i++;
|
405 | return [3 , 1];
|
406 | case 5: return [2 ];
|
407 | }
|
408 | });
|
409 | });
|
410 | }
|
411 | exports.press = press;
|
412 | function getValue(client, selector) {
|
413 | return __awaiter(this, void 0, void 0, function () {
|
414 | var Runtime, browserCode, expression, result;
|
415 | return __generator(this, function (_a) {
|
416 | switch (_a.label) {
|
417 | case 0:
|
418 | Runtime = client.Runtime;
|
419 | browserCode = "selector => {\n return document.querySelector(selector).value\n }";
|
420 | expression = "(" + browserCode + ")(`" + selector + "`)";
|
421 | return [4 , Runtime.evaluate({
|
422 | expression: expression,
|
423 | })];
|
424 | case 1:
|
425 | result = _a.sent();
|
426 | return [2 , result.result.value];
|
427 | }
|
428 | });
|
429 | });
|
430 | }
|
431 | exports.getValue = getValue;
|
432 | function scrollTo(client, x, y) {
|
433 | return __awaiter(this, void 0, void 0, function () {
|
434 | var Runtime, browserCode, expression;
|
435 | return __generator(this, function (_a) {
|
436 | switch (_a.label) {
|
437 | case 0:
|
438 | Runtime = client.Runtime;
|
439 | browserCode = "(x, y) => {\n return window.scrollTo(x, y)\n }";
|
440 | expression = "(" + browserCode + ")(" + x + ", " + y + ")";
|
441 | return [4 , Runtime.evaluate({
|
442 | expression: expression,
|
443 | })];
|
444 | case 1:
|
445 | _a.sent();
|
446 | return [2 ];
|
447 | }
|
448 | });
|
449 | });
|
450 | }
|
451 | exports.scrollTo = scrollTo;
|
452 | function scrollToElement(client, selector) {
|
453 | return __awaiter(this, void 0, void 0, function () {
|
454 | var clientRect;
|
455 | return __generator(this, function (_a) {
|
456 | switch (_a.label) {
|
457 | case 0: return [4 , getClientRect(client, selector)];
|
458 | case 1:
|
459 | clientRect = _a.sent();
|
460 | return [2 , scrollTo(client, clientRect.left, clientRect.top)];
|
461 | }
|
462 | });
|
463 | });
|
464 | }
|
465 | exports.scrollToElement = scrollToElement;
|
466 | function setHtml(client, html) {
|
467 | return __awaiter(this, void 0, void 0, function () {
|
468 | var Page, frameId;
|
469 | return __generator(this, function (_a) {
|
470 | switch (_a.label) {
|
471 | case 0:
|
472 | Page = client.Page;
|
473 | return [4 , Page.getResourceTree()];
|
474 | case 1:
|
475 | frameId = (_a.sent()).frameTree.frame.id;
|
476 | return [4 , Page.setDocumentContent({ frameId: frameId, html: html })];
|
477 | case 2:
|
478 | _a.sent();
|
479 | return [2 ];
|
480 | }
|
481 | });
|
482 | });
|
483 | }
|
484 | exports.setHtml = setHtml;
|
485 | function getCookies(client, nameOrQuery) {
|
486 | return __awaiter(this, void 0, void 0, function () {
|
487 | var Network, fn, url, result, cookies, filteredCookies;
|
488 | return __generator(this, function (_a) {
|
489 | switch (_a.label) {
|
490 | case 0:
|
491 | Network = client.Network;
|
492 | fn = function () { return location.href; };
|
493 | return [4 , evaluate(client, "" + fn)];
|
494 | case 1:
|
495 | url = (_a.sent());
|
496 | return [4 , Network.getCookies([url])];
|
497 | case 2:
|
498 | result = _a.sent();
|
499 | cookies = result.cookies;
|
500 | if (typeof nameOrQuery !== 'undefined' && typeof nameOrQuery === 'string') {
|
501 | filteredCookies = cookies.filter(function (cookie) { return cookie.name === nameOrQuery; });
|
502 | return [2 , filteredCookies];
|
503 | }
|
504 | return [2 , cookies];
|
505 | }
|
506 | });
|
507 | });
|
508 | }
|
509 | exports.getCookies = getCookies;
|
510 | function getAllCookies(client) {
|
511 | return __awaiter(this, void 0, void 0, function () {
|
512 | var Network, result;
|
513 | return __generator(this, function (_a) {
|
514 | switch (_a.label) {
|
515 | case 0:
|
516 | Network = client.Network;
|
517 | return [4 , Network.getAllCookies()];
|
518 | case 1:
|
519 | result = _a.sent();
|
520 | return [2 , result.cookies];
|
521 | }
|
522 | });
|
523 | });
|
524 | }
|
525 | exports.getAllCookies = getAllCookies;
|
526 | function setCookies(client, cookies) {
|
527 | return __awaiter(this, void 0, void 0, function () {
|
528 | var Network, _i, cookies_1, cookie;
|
529 | return __generator(this, function (_a) {
|
530 | switch (_a.label) {
|
531 | case 0:
|
532 | Network = client.Network;
|
533 | _i = 0, cookies_1 = cookies;
|
534 | _a.label = 1;
|
535 | case 1:
|
536 | if (!(_i < cookies_1.length)) return [3 , 4];
|
537 | cookie = cookies_1[_i];
|
538 | return [4 , Network.setCookie(__assign({}, cookie, { url: cookie.url ? cookie.url : getUrlFromCookie(cookie) }))];
|
539 | case 2:
|
540 | _a.sent();
|
541 | _a.label = 3;
|
542 | case 3:
|
543 | _i++;
|
544 | return [3 , 1];
|
545 | case 4: return [2 ];
|
546 | }
|
547 | });
|
548 | });
|
549 | }
|
550 | exports.setCookies = setCookies;
|
551 | function setExtraHTTPHeaders(client, headers) {
|
552 | return __awaiter(this, void 0, void 0, function () {
|
553 | var Network;
|
554 | return __generator(this, function (_a) {
|
555 | switch (_a.label) {
|
556 | case 0:
|
557 | Network = client.Network;
|
558 | return [4 , Network.setExtraHTTPHeaders({ headers: headers })];
|
559 | case 1:
|
560 | _a.sent();
|
561 | return [2 ];
|
562 | }
|
563 | });
|
564 | });
|
565 | }
|
566 | exports.setExtraHTTPHeaders = setExtraHTTPHeaders;
|
567 | function mousedown(client, selector, scale) {
|
568 | return __awaiter(this, void 0, void 0, function () {
|
569 | var clientRect, Input, options;
|
570 | return __generator(this, function (_a) {
|
571 | switch (_a.label) {
|
572 | case 0: return [4 , getClientRect(client, selector)];
|
573 | case 1:
|
574 | clientRect = _a.sent();
|
575 | Input = client.Input;
|
576 | options = {
|
577 | x: Math.round((clientRect.left + clientRect.width / 2) * scale),
|
578 | y: Math.round((clientRect.top + clientRect.height / 2) * scale),
|
579 | button: 'left',
|
580 | clickCount: 1,
|
581 | };
|
582 | return [4 , Input.dispatchMouseEvent(__assign({}, options, { type: 'mousePressed' }))];
|
583 | case 2:
|
584 | _a.sent();
|
585 | return [2 ];
|
586 | }
|
587 | });
|
588 | });
|
589 | }
|
590 | exports.mousedown = mousedown;
|
591 | function mouseup(client, selector, scale) {
|
592 | return __awaiter(this, void 0, void 0, function () {
|
593 | var clientRect, Input, options;
|
594 | return __generator(this, function (_a) {
|
595 | switch (_a.label) {
|
596 | case 0: return [4 , getClientRect(client, selector)];
|
597 | case 1:
|
598 | clientRect = _a.sent();
|
599 | Input = client.Input;
|
600 | options = {
|
601 | x: Math.round((clientRect.left + clientRect.width / 2) * scale),
|
602 | y: Math.round((clientRect.top + clientRect.height / 2) * scale),
|
603 | button: 'left',
|
604 | clickCount: 1,
|
605 | };
|
606 | return [4 , Input.dispatchMouseEvent(__assign({}, options, { type: 'mouseReleased' }))];
|
607 | case 2:
|
608 | _a.sent();
|
609 | return [2 ];
|
610 | }
|
611 | });
|
612 | });
|
613 | }
|
614 | exports.mouseup = mouseup;
|
615 | function getUrlFromCookie(cookie) {
|
616 | var domain = cookie.domain.slice(1, cookie.domain.length);
|
617 | return "https://" + domain;
|
618 | }
|
619 | function deleteCookie(client, name, url) {
|
620 | return __awaiter(this, void 0, void 0, function () {
|
621 | var Network;
|
622 | return __generator(this, function (_a) {
|
623 | switch (_a.label) {
|
624 | case 0:
|
625 | Network = client.Network;
|
626 | return [4 , Network.deleteCookie({ cookieName: name, url: url })];
|
627 | case 1:
|
628 | _a.sent();
|
629 | return [2 ];
|
630 | }
|
631 | });
|
632 | });
|
633 | }
|
634 | exports.deleteCookie = deleteCookie;
|
635 | function clearCookies(client) {
|
636 | return __awaiter(this, void 0, void 0, function () {
|
637 | var Network;
|
638 | return __generator(this, function (_a) {
|
639 | switch (_a.label) {
|
640 | case 0:
|
641 | Network = client.Network;
|
642 | return [4 , Network.clearBrowserCookies()];
|
643 | case 1:
|
644 | _a.sent();
|
645 | return [2 ];
|
646 | }
|
647 | });
|
648 | });
|
649 | }
|
650 | exports.clearCookies = clearCookies;
|
651 | function getBoxModel(client, selector) {
|
652 | return __awaiter(this, void 0, void 0, function () {
|
653 | var DOM, documentNodeId, nodeId, model;
|
654 | return __generator(this, function (_a) {
|
655 | switch (_a.label) {
|
656 | case 0:
|
657 | DOM = client.DOM;
|
658 | return [4 , DOM.getDocument()];
|
659 | case 1:
|
660 | documentNodeId = (_a.sent()).root.nodeId;
|
661 | return [4 , DOM.querySelector({
|
662 | selector: selector,
|
663 | nodeId: documentNodeId,
|
664 | })];
|
665 | case 2:
|
666 | nodeId = (_a.sent()).nodeId;
|
667 | return [4 , DOM.getBoxModel({ nodeId: nodeId })];
|
668 | case 3:
|
669 | model = (_a.sent()).model;
|
670 | return [2 , model];
|
671 | }
|
672 | });
|
673 | });
|
674 | }
|
675 | exports.getBoxModel = getBoxModel;
|
676 | function boxModelToViewPort(model, scale) {
|
677 | return {
|
678 | x: model.content[0],
|
679 | y: model.content[1],
|
680 | width: model.width,
|
681 | height: model.height,
|
682 | scale: scale,
|
683 | };
|
684 | }
|
685 | exports.boxModelToViewPort = boxModelToViewPort;
|
686 | function screenshot(client, selector) {
|
687 | return __awaiter(this, void 0, void 0, function () {
|
688 | var Page, captureScreenshotOptions, model, screenshot;
|
689 | return __generator(this, function (_a) {
|
690 | switch (_a.label) {
|
691 | case 0:
|
692 | Page = client.Page;
|
693 | captureScreenshotOptions = {
|
694 | format: 'png',
|
695 | fromSurface: true,
|
696 | clip: undefined,
|
697 | };
|
698 | if (!selector) return [3 , 2];
|
699 | return [4 , getBoxModel(client, selector)];
|
700 | case 1:
|
701 | model = _a.sent();
|
702 | captureScreenshotOptions.clip = boxModelToViewPort(model, 1);
|
703 | _a.label = 2;
|
704 | case 2: return [4 , Page.captureScreenshot(captureScreenshotOptions)];
|
705 | case 3:
|
706 | screenshot = _a.sent();
|
707 | return [2 , screenshot.data];
|
708 | }
|
709 | });
|
710 | });
|
711 | }
|
712 | exports.screenshot = screenshot;
|
713 | function html(client) {
|
714 | return __awaiter(this, void 0, void 0, function () {
|
715 | var DOM, nodeId, outerHTML;
|
716 | return __generator(this, function (_a) {
|
717 | switch (_a.label) {
|
718 | case 0:
|
719 | DOM = client.DOM;
|
720 | return [4 , DOM.getDocument()];
|
721 | case 1:
|
722 | nodeId = (_a.sent()).root.nodeId;
|
723 | return [4 , DOM.getOuterHTML({ nodeId: nodeId })];
|
724 | case 2:
|
725 | outerHTML = (_a.sent()).outerHTML;
|
726 | return [2 , outerHTML];
|
727 | }
|
728 | });
|
729 | });
|
730 | }
|
731 | exports.html = html;
|
732 | function htmlUrl(client) {
|
733 | return __awaiter(this, void 0, void 0, function () {
|
734 | var DOM, nodeId, outerHTML;
|
735 | return __generator(this, function (_a) {
|
736 | switch (_a.label) {
|
737 | case 0:
|
738 | DOM = client.DOM;
|
739 | return [4 , DOM.getDocument()];
|
740 | case 1:
|
741 | nodeId = (_a.sent()).root.nodeId;
|
742 | return [4 , DOM.getOuterHTML({ nodeId: nodeId })];
|
743 | case 2:
|
744 | outerHTML = (_a.sent()).outerHTML;
|
745 | return [2 , outerHTML];
|
746 | }
|
747 | });
|
748 | });
|
749 | }
|
750 | exports.htmlUrl = htmlUrl;
|
751 | function pdf(client, options) {
|
752 | return __awaiter(this, void 0, void 0, function () {
|
753 | var Page, pdf;
|
754 | return __generator(this, function (_a) {
|
755 | switch (_a.label) {
|
756 | case 0:
|
757 | Page = client.Page;
|
758 | return [4 , Page.printToPDF(options)];
|
759 | case 1:
|
760 | pdf = _a.sent();
|
761 | return [2 , pdf.data];
|
762 | }
|
763 | });
|
764 | });
|
765 | }
|
766 | exports.pdf = pdf;
|
767 | function clearInput(client, selector) {
|
768 | return __awaiter(this, void 0, void 0, function () {
|
769 | var Input, text, optionsDelete, optionsBackspace, i;
|
770 | return __generator(this, function (_a) {
|
771 | switch (_a.label) {
|
772 | case 0: return [4 , wait(500)];
|
773 | case 1:
|
774 | _a.sent();
|
775 | return [4 , focus(client, selector)];
|
776 | case 2:
|
777 | _a.sent();
|
778 | Input = client.Input;
|
779 | return [4 , getValue(client, selector)];
|
780 | case 3:
|
781 | text = _a.sent();
|
782 | optionsDelete = {
|
783 | nativeVirtualKeyCode: 46,
|
784 | windowsVirtualKeyCode: 46,
|
785 | };
|
786 | optionsBackspace = {
|
787 | nativeVirtualKeyCode: 8,
|
788 | windowsVirtualKeyCode: 8,
|
789 | };
|
790 | i = 0;
|
791 | _a.label = 4;
|
792 | case 4:
|
793 | if (!(i < text.length)) return [3 , 8];
|
794 | return [4 , Input.dispatchKeyEvent(__assign({}, optionsDelete, { type: 'rawKeyDown' }))];
|
795 | case 5:
|
796 | _a.sent();
|
797 | Input.dispatchKeyEvent(__assign({}, optionsDelete, { type: 'keyUp' }));
|
798 | return [4 , Input.dispatchKeyEvent(__assign({}, optionsBackspace, { type: 'rawKeyDown' }))];
|
799 | case 6:
|
800 | _a.sent();
|
801 | Input.dispatchKeyEvent(__assign({}, optionsBackspace, { type: 'keyUp' }));
|
802 | _a.label = 7;
|
803 | case 7:
|
804 | i++;
|
805 | return [3 , 4];
|
806 | case 8: return [2 ];
|
807 | }
|
808 | });
|
809 | });
|
810 | }
|
811 | exports.clearInput = clearInput;
|
812 | function setFileInput(client, selector, files) {
|
813 | return __awaiter(this, void 0, void 0, function () {
|
814 | var DOM, dom, node;
|
815 | return __generator(this, function (_a) {
|
816 | switch (_a.label) {
|
817 | case 0:
|
818 | DOM = client.DOM;
|
819 | return [4 , DOM.getDocument()];
|
820 | case 1:
|
821 | dom = _a.sent();
|
822 | return [4 , DOM.querySelector({
|
823 | nodeId: dom.root.nodeId,
|
824 | selector: selector,
|
825 | })];
|
826 | case 2:
|
827 | node = _a.sent();
|
828 | return [4 , DOM.setFileInputFiles({ files: files, nodeId: node.nodeId })];
|
829 | case 3: return [2 , _a.sent()];
|
830 | }
|
831 | });
|
832 | });
|
833 | }
|
834 | exports.setFileInput = setFileInput;
|
835 | function getDebugOption() {
|
836 | if (process &&
|
837 | process.env &&
|
838 | process.env['DEBUG'] &&
|
839 | process.env['DEBUG'].includes('chromeless')) {
|
840 | return true;
|
841 | }
|
842 | return false;
|
843 | }
|
844 | exports.getDebugOption = getDebugOption;
|
845 | function writeToFile(data, extension, filePathOverride) {
|
846 | var filePath = filePathOverride || path.join(os.tmpdir(), cuid() + "." + extension);
|
847 | fs.writeFileSync(filePath, Buffer.from(data, 'base64'));
|
848 | return filePath;
|
849 | }
|
850 | exports.writeToFile = writeToFile;
|
851 | function getS3BucketName() {
|
852 | return process.env['CHROMELESS_S3_BUCKET_NAME'];
|
853 | }
|
854 | function getS3BucketUrl() {
|
855 | return process.env['CHROMELESS_S3_BUCKET_URL'];
|
856 | }
|
857 | function getS3ObjectKeyPrefix() {
|
858 | return process.env['CHROMELESS_S3_OBJECT_KEY_PREFIX'] || '';
|
859 | }
|
860 | function getS3FilesPermissions() {
|
861 | return process.env['CHROMELESS_S3_OBJECT_ACL'] || 'public-read';
|
862 | }
|
863 | function isS3Configured() {
|
864 | return getS3BucketName() && getS3BucketUrl();
|
865 | }
|
866 | exports.isS3Configured = isS3Configured;
|
867 | var s3ContentTypes = {
|
868 | 'image/png': {
|
869 | extension: 'png',
|
870 | },
|
871 | 'application/pdf': {
|
872 | extension: 'pdf',
|
873 | },
|
874 | };
|
875 | function uploadToS3(data, contentType) {
|
876 | return __awaiter(this, void 0, void 0, function () {
|
877 | var s3ContentType, s3Path, s3;
|
878 | return __generator(this, function (_a) {
|
879 | switch (_a.label) {
|
880 | case 0:
|
881 | s3ContentType = s3ContentTypes[contentType];
|
882 | if (!s3ContentType) {
|
883 | throw new Error("Unknown S3 Content type " + contentType);
|
884 | }
|
885 | s3Path = "" + getS3ObjectKeyPrefix() + cuid() + "." + s3ContentType.extension;
|
886 | s3 = new AWS.S3();
|
887 | return [4 , s3
|
888 | .putObject({
|
889 | Bucket: getS3BucketName(),
|
890 | Key: s3Path,
|
891 | ContentType: contentType,
|
892 | ACL: getS3FilesPermissions(),
|
893 | Body: Buffer.from(data, 'base64'),
|
894 | })
|
895 | .promise()];
|
896 | case 1:
|
897 | _a.sent();
|
898 | return [2 , "https://" + getS3BucketUrl() + "/" + s3Path];
|
899 | }
|
900 | });
|
901 | });
|
902 | }
|
903 | exports.uploadToS3 = uploadToS3;
|
904 |
|
\ | No newline at end of file |