@agentic-intelligence/dom-engine
Version:
Agentic DOM Intelligence - A lightweight TypeScript library for DOM analysis and manipulation, designed for web automation and AI agents
81 lines • 2.91 kB
JavaScript
/**
* DOM element analyzer
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getElementText = getElementText;
exports.getSiblingText = getSiblingText;
exports.isElementVisible = isElementVisible;
exports.hasSvgIcon = hasSvgIcon;
const helpers_1 = require("../utils/helpers");
/**
* Gets the text of an element according to its specific type
* @param element - DOM element
* @returns Extracted text from the element
*/
function getElementText(element) {
const constructorName = element.constructor.name;
const textExtractors = {
HTMLInputElement: () => {
const input = element;
return [
input.placeholder && `Placeholder: ${input.placeholder}`,
input.value && `Value: ${input.value}`
].filter(Boolean).join(' | ');
},
HTMLTextAreaElement: () => {
const textarea = element;
return textarea.value || '';
},
HTMLSelectElement: () => {
const select = element;
return select.selectedOptions[0]?.textContent || '';
},
DEFAULT: () => element.textContent || ''
};
const extractor = textExtractors[constructorName] || textExtractors.DEFAULT;
return (0, helpers_1.cleanText)(extractor());
}
/**
* Gets the text of immediate siblings of an element
* @param element - DOM element
* @returns Object with left and right sibling text
*/
function getSiblingText(element) {
return {
leftBrother: element.previousElementSibling ? getElementText(element.previousElementSibling) : '',
rightBrother: element.nextElementSibling ? getElementText(element.nextElementSibling) : ''
};
}
/**
* Checks if an element is visible on screen
* @param element - DOM element to check
* @returns true if the element is visible, false otherwise
*/
function isElementVisible(element) {
const rect = element.getBoundingClientRect();
const style = window.getComputedStyle(element);
return (
// In viewport
rect.top >= 0 && rect.left >= 0 &&
rect.bottom <= window.innerHeight && rect.right <= window.innerWidth &&
// Not hidden by CSS
style.display !== 'none' && style.visibility !== 'hidden' &&
style.opacity !== '0' && !element.hidden &&
// Has size
rect.width > 0 && rect.height > 0);
}
/**
* Checks if a button element has SVG icon children (for icon-only buttons)
* @param element - Button element to check
* @returns true if the button has SVG children, false otherwise
*/
function hasSvgIcon(element) {
// Only check buttons
if (element.constructor.name !== 'HTMLButtonElement' && element.getAttribute('role') !== 'button') {
return false;
}
// Check all descendants for SVG elements
return element.querySelector('svg') !== null;
}
//# sourceMappingURL=element-analyzer.js.map
;