extract
Extract text, HTML, or screenshots from web pages using browser automation while avoiding bot detection.
Instructions
Extract information from the current page as text, html, or screenshot
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| browserId | Yes | Browser ID from a previous browse operation | |
| pageId | Yes | Page ID from a previous browse operation | |
| type | Yes | Type of content to extract |
Implementation Reference
- src/index.ts:217-293 (registration)Registration of the 'extract' tool using server.tool, including name, description, schema, and handler function.server.tool( "extract", "Extract information from the current page as text, html, or screenshot", { browserId: z.string().describe("Browser ID from a previous browse operation"), pageId: z.string().describe("Page ID from a previous browse operation"), type: z.enum(["text", "html", "screenshot"]).describe("Type of content to extract") }, async ({ browserId, pageId, type }: { browserId: string; pageId: string; type: "text" | "html" | "screenshot" }) => { try { // Get the browser instance and page const instance = browserInstances.get(browserId); if (!instance) { throw new Error(`Browser instance not found: ${browserId}`); } const page = instance.pages.get(pageId); if (!page) { throw new Error(`Page not found: ${pageId}`); } let extractedContent = ''; let screenshotPath = ''; // Extract content based on requested type switch (type) { case "text": // Get visible text with stealth isolation extractedContent = await page.evaluate(` Array.from(document.querySelectorAll('body, body *')) .filter(element => { const style = window.getComputedStyle(element); return style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0'; }) .map(element => element.textContent) .filter(text => text && text.trim().length > 0) .join('\\n') `) as string; break; case "html": // Get HTML content extractedContent = await page.content(); break; case "screenshot": // Take a screenshot screenshotPath = path.join(TEMP_DIR, `screenshot-${pageId}-${Date.now()}.png`); await page.screenshot({ path: screenshotPath }); extractedContent = `Screenshot saved to: ${screenshotPath}`; break; } return { content: [ { type: "text", text: type === "text" || type === "screenshot" ? extractedContent.substring(0, 2000) + (extractedContent.length > 2000 ? '...' : '') : `Extracted HTML content (${extractedContent.length} characters). First 100 characters:\n${extractedContent.substring(0, 100)}...` } ] }; } catch (error) { return { content: [ { type: "text", text: `Failed to extract content: ${error}` } ] }; } } );
- src/index.ts:225-292 (handler)Handler function for the 'extract' tool that retrieves text, HTML, or screenshot from the specified page using Patchright/Playwright.async ({ browserId, pageId, type }: { browserId: string; pageId: string; type: "text" | "html" | "screenshot" }) => { try { // Get the browser instance and page const instance = browserInstances.get(browserId); if (!instance) { throw new Error(`Browser instance not found: ${browserId}`); } const page = instance.pages.get(pageId); if (!page) { throw new Error(`Page not found: ${pageId}`); } let extractedContent = ''; let screenshotPath = ''; // Extract content based on requested type switch (type) { case "text": // Get visible text with stealth isolation extractedContent = await page.evaluate(` Array.from(document.querySelectorAll('body, body *')) .filter(element => { const style = window.getComputedStyle(element); return style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0'; }) .map(element => element.textContent) .filter(text => text && text.trim().length > 0) .join('\\n') `) as string; break; case "html": // Get HTML content extractedContent = await page.content(); break; case "screenshot": // Take a screenshot screenshotPath = path.join(TEMP_DIR, `screenshot-${pageId}-${Date.now()}.png`); await page.screenshot({ path: screenshotPath }); extractedContent = `Screenshot saved to: ${screenshotPath}`; break; } return { content: [ { type: "text", text: type === "text" || type === "screenshot" ? extractedContent.substring(0, 2000) + (extractedContent.length > 2000 ? '...' : '') : `Extracted HTML content (${extractedContent.length} characters). First 100 characters:\n${extractedContent.substring(0, 100)}...` } ] }; } catch (error) { return { content: [ { type: "text", text: `Failed to extract content: ${error}` } ] }; } }
- src/index.ts:221-224 (schema)Input schema for the 'extract' tool defined using Zod, specifying browserId, pageId, and type parameters.browserId: z.string().describe("Browser ID from a previous browse operation"), pageId: z.string().describe("Page ID from a previous browse operation"), type: z.enum(["text", "html", "screenshot"]).describe("Type of content to extract") },