getLinks
Extract all links from a webpage to streamline web scraping or data collection tasks using Playwright-based browser automation.
Instructions
Get all links from the current page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/controllers/playwright.ts:393-413 (handler)The core handler function in PlaywrightController that evaluates JavaScript on the page to extract all anchor links with href, text, and optional title.async getLinks(): Promise<Array<{href: string, text: string, title?: string}>> { try { if (!this.isInitialized()) { throw new Error('Browser not initialized'); } this.log('Getting page links'); const links = await this.state.page?.evaluate(() => { const linkElements = Array.from(document.querySelectorAll('a[href]')); return linkElements.map(link => ({ href: (link as HTMLAnchorElement).href, text: link.textContent?.trim() || '', title: link.getAttribute('title') || undefined })); }); this.log('Links retrieved:', links?.length); return links || []; } catch (error: any) { console.error('Get links error:', error); throw new BrowserError('Failed to get links', 'Check if the page is loaded'); } }
- src/server.ts:153-161 (schema)Tool schema definition for 'getLinks', specifying no input parameters.const GET_LINKS_TOOL: Tool = { name: "getLinks", description: "Get all links from the current page", inputSchema: { type: "object", properties: {}, required: [] } };
- src/server.ts:514-553 (registration)Registration of the getLinks tool in the tools object passed to MCP server capabilities.const tools = { openBrowser: OPEN_BROWSER_TOOL, navigate: NAVIGATE_TOOL, type: TYPE_TOOL, click: CLICK_TOOL, moveMouse: MOVE_MOUSE_TOOL, scroll: SCROLL_TOOL, screenshot: SCREENSHOT_TOOL, getPageSource: GET_PAGE_SOURCE_TOOL, getPageText: GET_PAGE_TEXT_TOOL, getPageTitle: GET_PAGE_TITLE_TOOL, getPageUrl: GET_PAGE_URL_TOOL, getScripts: GET_SCRIPTS_TOOL, getStylesheets: GET_STYLESHEETS_TOOL, getMetaTags: GET_META_TAGS_TOOL, getLinks: GET_LINKS_TOOL, getImages: GET_IMAGES_TOOL, getForms: GET_FORMS_TOOL, getElementContent: GET_ELEMENT_CONTENT_TOOL, getElementHierarchy: GET_ELEMENT_HIERARCHY_TOOL, executeJavaScript: EXECUTE_JAVASCRIPT_TOOL, goForward: GO_FORWARD_TOOL, hover: HOVER_TOOL, dragAndDrop: DRAG_AND_DROP_TOOL, selectOption: SELECT_OPTION_TOOL, pressKey: PRESS_KEY_TOOL, waitForText: WAIT_FOR_TEXT_TOOL, waitForSelector: WAIT_FOR_SELECTOR_TOOL, resize: RESIZE_TOOL, handleDialog: HANDLE_DIALOG_TOOL, getConsoleMessages: GET_CONSOLE_MESSAGES_TOOL, getNetworkRequests: GET_NETWORK_REQUESTS_TOOL, uploadFiles: UPLOAD_FILES_TOOL, evaluateWithReturn: EVALUATE_WITH_RETURN_TOOL, takeScreenshot: TAKE_SCREENSHOT_TOOL, mouseMove: MOUSE_MOVE_TOOL, mouseClick: MOUSE_CLICK_TOOL, mouseDrag: MOUSE_DRAG_TOOL, closeBrowser: CLOSE_BROWSER_TOOL };
- src/server.ts:726-731 (handler)MCP server request handler that dispatches 'getLinks' tool calls to the Playwright controller and formats the response.case 'getLinks': { const links = await playwrightController.getLinks(); return { content: [{ type: "text", text: JSON.stringify(links, null, 2) }] }; }