UNPKG

12.2 kBJavaScriptView Raw
1"use strict";
2var __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};
10var __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};
18var __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};
45Object.defineProperty(exports, "__esModule", { value: true });
46var local_1 = require("./chrome/local");
47var remote_1 = require("./chrome/remote");
48var queue_1 = require("./queue");
49var util_1 = require("./util");
50var util_2 = require("util");
51var Chromeless = /** @class */ (function () {
52 function Chromeless(options, copyInstance) {
53 if (options === void 0) { options = {}; }
54 if (copyInstance) {
55 this.queue = copyInstance.queue;
56 this.lastReturnPromise = copyInstance.lastReturnPromise;
57 return;
58 }
59 var mergedOptions = __assign({ debug: util_1.getDebugOption(), waitTimeout: 10000, remote: false, implicitWait: true, scrollBeforeClick: false, launchChrome: true }, options, { viewport: __assign({ scale: 1 }, options.viewport), cdp: __assign({ host: process.env['CHROMELESS_CHROME_HOST'] || 'localhost', port: parseInt(process.env['CHROMELESS_CHROME_PORT'], 10) || 9222, secure: false, closeTab: true }, options.cdp) });
60 var chrome = mergedOptions.remote
61 ? new remote_1.default(mergedOptions)
62 : new local_1.default(mergedOptions);
63 this.queue = new queue_1.default(chrome);
64 this.lastReturnPromise = Promise.resolve(undefined);
65 }
66 Chromeless.prototype.then = function (onFulfill, onReject) {
67 return this.lastReturnPromise.then(onFulfill, onReject);
68 };
69 Chromeless.prototype.catch = function (onrejected) {
70 return this.lastReturnPromise.catch(onrejected);
71 };
72 Chromeless.prototype.goto = function (url, timeout) {
73 this.queue.enqueue({ type: 'goto', url: url, timeout: timeout });
74 return this;
75 };
76 Chromeless.prototype.setUserAgent = function (useragent) {
77 this.queue.enqueue({ type: 'setUserAgent', useragent: useragent });
78 return this;
79 };
80 Chromeless.prototype.click = function (selector) {
81 this.queue.enqueue({ type: 'click', selector: selector });
82 return this;
83 };
84 Chromeless.prototype.wait = function (firstArg) {
85 var args = [];
86 for (var _i = 1; _i < arguments.length; _i++) {
87 args[_i - 1] = arguments[_i];
88 }
89 switch (typeof firstArg) {
90 case 'number': {
91 this.queue.enqueue({ type: 'wait', timeout: firstArg });
92 break;
93 }
94 case 'string': {
95 this.queue.enqueue({
96 type: 'wait',
97 selector: firstArg,
98 timeout: args[0],
99 });
100 break;
101 }
102 case 'function': {
103 this.queue.enqueue({ type: 'wait', fn: firstArg, args: args });
104 break;
105 }
106 default:
107 throw new Error("Invalid wait arguments: " + firstArg + " " + args);
108 }
109 return this;
110 };
111 Chromeless.prototype.clearCache = function () {
112 this.queue.enqueue({ type: 'clearCache' });
113 return this;
114 };
115 Chromeless.prototype.clearStorage = function (origin, storageTypes) {
116 this.queue.enqueue({ type: 'clearStorage', origin: origin, storageTypes: storageTypes });
117 return this;
118 };
119 Chromeless.prototype.focus = function (selector) {
120 this.queue.enqueue({ type: 'focus', selector: selector });
121 return this;
122 };
123 Chromeless.prototype.press = function (keyCode, count, modifiers) {
124 this.queue.enqueue({ type: 'press', keyCode: keyCode, count: count, modifiers: modifiers });
125 return this;
126 };
127 Chromeless.prototype.type = function (input, selector) {
128 this.queue.enqueue({ type: 'type', input: input, selector: selector });
129 return this;
130 };
131 Chromeless.prototype.back = function () {
132 throw new Error('Not implemented yet');
133 };
134 Chromeless.prototype.forward = function () {
135 throw new Error('Not implemented yet');
136 };
137 Chromeless.prototype.refresh = function () {
138 throw new Error('Not implemented yet');
139 };
140 Chromeless.prototype.mousedown = function (selector) {
141 this.queue.enqueue({ type: 'mousedown', selector: selector });
142 return this;
143 };
144 Chromeless.prototype.mouseup = function (selector) {
145 this.queue.enqueue({ type: 'mouseup', selector: selector });
146 return this;
147 };
148 Chromeless.prototype.mouseover = function () {
149 throw new Error('Not implemented yet');
150 };
151 Chromeless.prototype.scrollTo = function (x, y) {
152 this.queue.enqueue({ type: 'scrollTo', x: x, y: y });
153 return this;
154 };
155 Chromeless.prototype.scrollToElement = function (selector) {
156 this.queue.enqueue({ type: 'scrollToElement', selector: selector });
157 return this;
158 };
159 Chromeless.prototype.setViewport = function (options) {
160 this.queue.enqueue({ type: 'setViewport', options: options });
161 return this;
162 };
163 Chromeless.prototype.setHtml = function (html) {
164 this.queue.enqueue({ type: 'setHtml', html: html });
165 return this;
166 };
167 Chromeless.prototype.setExtraHTTPHeaders = function (headers) {
168 this.queue.enqueue({ type: 'setExtraHTTPHeaders', headers: headers });
169 return this;
170 };
171 Chromeless.prototype.evaluate = function (fn) {
172 var args = [];
173 for (var _i = 1; _i < arguments.length; _i++) {
174 args[_i - 1] = arguments[_i];
175 }
176 this.lastReturnPromise = this.queue.process({
177 type: 'returnCode',
178 fn: fn.toString(),
179 args: args,
180 });
181 return new Chromeless({}, this);
182 };
183 Chromeless.prototype.inputValue = function (selector) {
184 this.lastReturnPromise = this.queue.process({
185 type: 'returnInputValue',
186 selector: selector,
187 });
188 return new Chromeless({}, this);
189 };
190 Chromeless.prototype.exists = function (selector) {
191 this.lastReturnPromise = this.queue.process({
192 type: 'returnExists',
193 selector: selector,
194 });
195 return new Chromeless({}, this);
196 };
197 Chromeless.prototype.screenshot = function (selector, options) {
198 if (typeof selector === 'object') {
199 options = selector;
200 selector = undefined;
201 }
202 this.lastReturnPromise = this.queue.process({
203 type: 'returnScreenshot',
204 selector: selector,
205 options: options,
206 });
207 return new Chromeless({}, this);
208 };
209 Chromeless.prototype.html = function () {
210 this.lastReturnPromise = this.queue.process({ type: 'returnHtml' });
211 return new Chromeless({}, this);
212 };
213 Chromeless.prototype.htmlUrl = function () {
214 this.lastReturnPromise = this.queue.process({
215 type: 'returnHtmlUrl',
216 });
217 return new Chromeless({}, this);
218 };
219 Chromeless.prototype.pdf = function (options) {
220 this.lastReturnPromise = this.queue.process({
221 type: 'returnPdf',
222 options: options,
223 });
224 return new Chromeless({}, this);
225 };
226 Chromeless.prototype.cookies = function (nameOrQuery) {
227 if (typeof nameOrQuery !== 'undefined' && typeof nameOrQuery !== 'string') {
228 throw new Error('Querying cookies is not implemented yet');
229 }
230 this.lastReturnPromise = this.queue.process({
231 type: 'cookies',
232 nameOrQuery: nameOrQuery,
233 });
234 return new Chromeless({}, this);
235 };
236 Chromeless.prototype.allCookies = function () {
237 this.lastReturnPromise = this.queue.process({
238 type: 'allCookies',
239 });
240 return new Chromeless({}, this);
241 };
242 Chromeless.prototype.setCookies = function (nameOrCookies, value) {
243 this.queue.enqueue({ type: 'setCookies', nameOrCookies: nameOrCookies, value: value });
244 return this;
245 };
246 Chromeless.prototype.deleteCookies = function (name, url) {
247 if (typeof name === 'undefined') {
248 throw new Error('Cookie name should be defined.');
249 }
250 if (typeof url === 'undefined') {
251 throw new Error('Cookie url should be defined.');
252 }
253 this.queue.enqueue({ type: 'deleteCookies', name: name, url: url });
254 return this;
255 };
256 Chromeless.prototype.clearCookies = function () {
257 this.queue.enqueue({ type: 'clearCookies' });
258 return this;
259 };
260 Chromeless.prototype.clearInput = function (selector) {
261 this.queue.enqueue({ type: 'clearInput', selector: selector });
262 return this;
263 };
264 Chromeless.prototype.setFileInput = function (selector, files) {
265 if (!util_2.isArray(files)) {
266 files = [files];
267 }
268 this.queue.enqueue({ type: 'setFileInput', selector: selector, files: files });
269 return this;
270 };
271 Chromeless.prototype.end = function () {
272 return __awaiter(this, void 0, void 0, function () {
273 var result;
274 return __generator(this, function (_a) {
275 switch (_a.label) {
276 case 0: return [4 /*yield*/, this.lastReturnPromise];
277 case 1:
278 result = _a.sent();
279 return [4 /*yield*/, this.queue.end()];
280 case 2:
281 _a.sent();
282 return [2 /*return*/, result];
283 }
284 });
285 });
286 };
287 return Chromeless;
288}());
289exports.default = Chromeless;
290//# sourceMappingURL=api.js.map
\No newline at end of file