UNPKG

multi-automator

Version:
104 lines (103 loc) 3.49 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @desc: Android Element * @author: john_chen * @date: 2025.02.15 */ const fs_1 = require("fs"); const config_1 = require("../config"); const time_1 = require("../utils/time"); const adb = __importStar(require("./adb")); const image_1 = require("../utils/image"); class Element { constructor(options) { this.device = options.device; this.bounds = options.bounds; this.attributes = options.attributes; } /** * 获取元素边界 */ async boundingBox() { return this.bounds; } /** * 获取元素属性 */ async attribute(name) { return this.attributes.getNamedItem(name).nodeValue; } /** * 点击元素 */ async tap() { const { x, y, width, height } = await this.boundingBox(); const centerX = x + width / 2; const centerY = y + height / 2; await this.device.tap(centerX, centerY); config_1.logger.info(`[Element.tap] Tapped at: (${centerX}, ${centerY})`); } /** * 输入文本 */ async input(text, options = {}) { const { wait = 2000 } = options; const defaultKeyboard = await adb.getDefaultKeyboard(this.device.id); try { // 切换输入法(会致使失去焦点) await adb.changeKeyboard(this.device.id); await (0, time_1.delay)(wait); // 点击以获取焦点 await this.tap(); await (0, time_1.delay)(wait); // 输入文本 await this.device.input(text); } catch (error) { config_1.logger.error(`[Element.input] 输入文本失败: ${error instanceof Error ? error.message : String(error)}`); throw new Error('输入文本时发生错误'); } finally { // 恢复默认输入法 await adb.changeKeyboard(this.device.id, defaultKeyboard); } } /** * 截图 */ async screenshot(path) { let res = await this.device.screenshot(); let bounds = await this.boundingBox(); config_1.logger.info(`[Element.screenshot] Bounds: ${JSON.stringify(bounds)}`); let cropped = await (0, image_1.extract)(res, bounds); if (path) { (0, fs_1.writeFileSync)(path, cropped); } return cropped; } } exports.default = Element;