UNPKG

browser-automator

Version:

Puppeteer alternative for Chrome extensions. A module for Chrome extensions that functions similarly to Puppeteer.

105 lines (104 loc) 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class RemoteElement { page; elementPath; tagName; async handleCall(key, args) { return await this.page.evaluate({ func: (elementPath, key, args) => args ? window.Self.ElementActions.handleCall(elementPath, key, args) : window.Self.ElementActions.handleCall(elementPath, key), args: args ? [this.elementPath, key, args] : [this.elementPath, key] }); } async handleGet(key) { return await this.page.evaluate({ func: (elementPath, key) => window.Self.ElementActions.handleGet(elementPath, key), args: [this.elementPath, key] }); } async handleSet(key, value) { return await this.page.evaluate({ func: (elementPath, key, value) => window.Self.ElementActions.handleSet(elementPath, key, value), args: [this.elementPath, key, value] }); } async getTagName() { if (this.tagName) return this.tagName; this.tagName = await this.handleGet('tagName'); return this.tagName; } async getInnerText() { return await this.handleGet('innerText'); } async getInnerHTML() { return await this.handleGet('innerHTML'); } async setInnerHTML(html) { return await this.handleSet('innerHTML', html); } async click() { return await this.handleCall('click'); } async focus() { return await this.handleCall('focus'); } async scrollIntoView(options) { return await this.handleCall('scrollIntoView', [options]); } async getAttribute(qualifiedName) { return await this.handleCall('getAttribute', [qualifiedName]); } async setAttribute(qualifiedName, value) { return await this.handleCall('setAttribute', [qualifiedName, value]); } async getElement(selectors, index = -1) { return await this.page.getElement(selectors, index, this.elementPath); } async getElements(selectors) { return await this.page.getElements(selectors, this.elementPath); } async input(value) { return await this.page.evaluate({ func: (value, elementPath) => { const element = window.Self.ElementActions.getElement(elementPath); if (!element) return false; window.Self.setValue(element, value); return true; }, args: [value, this.elementPath] }); } async execPaste() { return await this.page.evaluate({ func: (elementPath) => { const element = window.Self.ElementActions.getElement(elementPath); if (!element) return false; window.Self.triggerPaste(element); return true; }, args: [this.elementPath] }); } async triggerEvent(type) { return await this.page.evaluate({ func: (type, elementPath) => { const element = window.Self.ElementActions.getElement(elementPath); if (!element) return false; window.Self.triggerEvent(element, type); return true; }, args: [type, this.elementPath] }); } constructor(page, elementPath, tagName) { this.page = page; this.elementPath = elementPath; if (tagName) this.tagName = tagName; } } exports.default = RemoteElement;