UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

219 lines (214 loc) 6.15 kB
'use strict'; var tslib_es6 = require('./tslib.es6-01322ba9.js'); var DEFAULT_TIMEOUT = 6000; function waitEle(element, page, timeout) { if (timeout === void 0) { timeout = DEFAULT_TIMEOUT; } return tslib_es6.__awaiter(this, void 0, void 0, function () { var dom; return tslib_es6.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2,, 3]); return [4 /*yield*/, page.waitForSelector(element, { timeout: timeout })]; case 1: dom = _a.sent(); return [3 /*break*/, 3]; case 2: _a.sent(); return [3 /*break*/, 3]; case 3: return [2 /*return*/, dom]; } }); }); } function clickBtn(btn) { return tslib_es6.__awaiter(this, void 0, void 0, function () { return tslib_es6.__generator(this, function (_a) { switch (_a.label) { case 0: if (!btn) return [3 /*break*/, 2]; return [4 /*yield*/, btn.evaluate(function (b) { return b.click(); })]; case 1: _a.sent(); return [2 /*return*/, true]; case 2: return [2 /*return*/, false]; } }); }); } function findAndClick(element, page, timeout) { if (timeout === void 0) { timeout = DEFAULT_TIMEOUT; } return tslib_es6.__awaiter(this, void 0, void 0, function () { var btn; return tslib_es6.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, waitEle(element, page, timeout)]; case 1: btn = _a.sent(); return [4 /*yield*/, clickBtn(btn)]; case 2: _a.sent(); return [2 /*return*/, btn]; } }); }); } function justWait(time) { return new Promise(function (resolve) { return setTimeout(resolve, time); }); } function closeBlankPage(browser) { return tslib_es6.__awaiter(this, void 0, void 0, function () { var BLANK_PAGE_HREF, target, page; return tslib_es6.__generator(this, function (_a) { switch (_a.label) { case 0: BLANK_PAGE_HREF = 'about:blank'; return [4 /*yield*/, browser.waitForTarget(function (t) { return t.url() === BLANK_PAGE_HREF; }, { timeout: DEFAULT_TIMEOUT })]; case 1: target = _a.sent(); if (!target) return [2 /*return*/]; return [4 /*yield*/, target.page()]; case 2: page = _a.sent(); if (!page) return [2 /*return*/]; page.close(); return [2 /*return*/]; } }); }); } function getRect(element, page) { return tslib_es6.__awaiter(this, void 0, void 0, function () { return tslib_es6.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(typeof element === 'string')) return [3 /*break*/, 2]; return [4 /*yield*/, waitEle(element, page)]; case 1: element = _a.sent(); _a.label = 2; case 2: return [4 /*yield*/, page.evaluate(function (ele) { var _a = ele.getBoundingClientRect(), top = _a.top, left = _a.left, bottom = _a.bottom, right = _a.right, x = _a.x, y = _a.y, width = _a.width, height = _a.height; return { top: top, left: left, bottom: bottom, right: right, x: x, y: y, width: width, height: height }; }, element)]; case 3: return [2 /*return*/, _a.sent()]; } }); }); } function getInnerText(element, page) { return tslib_es6.__awaiter(this, void 0, void 0, function () { var ele, text; return tslib_es6.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, waitEle(element, page)]; case 1: ele = _a.sent(); if (!ele) return [2 /*return*/, null]; return [4 /*yield*/, page.evaluate(function (ele) { return ele.innerText; }, ele)]; case 2: text = _a.sent(); return [2 /*return*/, text]; } }); }); } function findListItemAndClick(_a) { var page = _a.page, element = _a.element, innerText = _a.innerText; return tslib_es6.__awaiter(this, void 0, void 0, function () { return tslib_es6.__generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, page.evaluate(function (_a) { var element = _a.element, innerText = _a.innerText; var buttons = document.querySelectorAll(element); var btn = Array.from(buttons).find(function (item) { return item.innerText === innerText; }); if (btn) { btn.click(); } }, { element: element, innerText: innerText })]; case 1: _b.sent(); return [2 /*return*/]; } }); }); } function getHref(page) { return tslib_es6.__awaiter(this, void 0, void 0, function () { return tslib_es6.__generator(this, function (_a) { return [2 /*return*/, page.evaluate(function () { return location.href; })]; }); }); } var element = /*#__PURE__*/Object.freeze({ __proto__: null, waitEle: waitEle, clickBtn: clickBtn, findAndClick: findAndClick, justWait: justWait, closeBlankPage: closeBlankPage, getRect: getRect, getInnerText: getInnerText, findListItemAndClick: findListItemAndClick, getHref: getHref }); exports.clickBtn = clickBtn; exports.closeBlankPage = closeBlankPage; exports.element = element; exports.findAndClick = findAndClick; exports.findListItemAndClick = findListItemAndClick; exports.getHref = getHref; exports.getInnerText = getInnerText; exports.getRect = getRect; exports.justWait = justWait; exports.waitEle = waitEle;