Files
2026-07-31 13:12:54 -04:00

42 lines
1.9 KiB
JavaScript

/// <reference lib="dom" />
import { z } from 'zod';
import { errorResult, jsonResult, toErrorMessage } from '../shared.js';
import { browserManager } from './browserManager.js';
export function registerBrowserInspectDomTool(server) {
server.registerTool('browser_inspect_dom', {
title: 'Browser Inspect DOM',
description: 'Query the DOM of the current page by CSS selector and return the outerHTML, inner text, and selected computed styles of the first matching element.',
inputSchema: {
selector: z.string().min(1).describe('CSS selector to query.'),
includeStyles: z
.array(z.string())
.optional()
.default(['color', 'backgroundColor', 'fontSize', 'display'])
.describe('Computed CSS properties to return.'),
},
}, async ({ selector, includeStyles }) => {
try {
const session = await browserManager.getSession();
const element = session.page.locator(selector).first();
await element.waitFor({ state: 'attached', timeout: 5000 });
const outerHTML = await element.evaluate((el) => el.outerHTML).catch(() => null);
const text = await element.innerText().catch(() => null);
const styles = await element.evaluate((el, props) => {
const computed = window.getComputedStyle(el);
return Object.fromEntries(props.map((p) => [p, computed.getPropertyValue(p)]));
}, includeStyles);
const count = await session.page.locator(selector).count();
return jsonResult({
selector,
matchCount: count,
outerHTML,
text,
styles,
});
}
catch (error) {
return errorResult(toErrorMessage(error));
}
});
}
//# sourceMappingURL=inspectDom.js.map