app-walk
Version:
An intuitive guided walkthrough library with UI highlighting and voice narration for web apps.
31 lines (30 loc) • 1.15 kB
JavaScript
import { convertToCss } from "./utils";
export function findElement(selectors, scope) {
let currentScope = scope;
for (const selector of selectors) {
const element = resolveElement(selector, currentScope);
if (!element) {
console.log("Failed to resolve in chain at selector:", selector.selector);
return undefined;
}
currentScope = element;
}
return currentScope;
}
export function resolveElement(selector, scope) {
var _a, _b;
const selectorString = convertToCss(selector);
if (!selectorString)
return undefined;
const index = (_a = selector.index) !== null && _a !== void 0 ? _a : 0; // 0-based to 0-based
const elements = scope.querySelectorAll(selectorString);
if (elements.length === 0) {
console.warn("No elements found for selector:", selector);
return undefined;
}
if (index >= elements.length) {
console.warn(`Index ${index + 1} out of bounds, found ${elements.length} elements`, selector);
return undefined;
}
return (_b = elements[index]) !== null && _b !== void 0 ? _b : undefined;
}