chore: support custom filename in screenshot function (#349)

This commit is contained in:
おがどら
2025-05-09 03:04:18 +09:00
committed by GitHub
parent 09ba7989c3
commit 85c85bd2fb
5 changed files with 116 additions and 10 deletions

View File

@@ -20,6 +20,10 @@ import { defineTool } from './tool.js';
import * as javascript from '../javascript.js';
import { outputFile } from '../config.js';
const pdfSchema = z.object({
filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
});
const pdf = defineTool({
capability: 'pdf',
@@ -27,13 +31,13 @@ const pdf = defineTool({
name: 'browser_pdf_save',
title: 'Save as PDF',
description: 'Save page as PDF',
inputSchema: z.object({}),
inputSchema: pdfSchema,
type: 'readOnly',
},
handle: async context => {
handle: async (context, params) => {
const tab = context.currentTabOrDie();
const fileName = await outputFile(context.config, `page-${new Date().toISOString()}.pdf`);
const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.pdf`);
const code = [
`// Save page as ${fileName}`,

View File

@@ -220,6 +220,7 @@ const selectOption = defineTool({
const screenshotSchema = z.object({
raw: z.boolean().optional().describe('Whether to return without compression (in PNG format). Default is false, which returns a JPEG image.'),
filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
element: z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'),
ref: z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'),
}).refine(data => {
@@ -243,7 +244,7 @@ const screenshot = defineTool({
const tab = context.currentTabOrDie();
const snapshot = tab.snapshotOrDie();
const fileType = params.raw ? 'png' : 'jpeg';
const fileName = await outputFile(context.config, `page-${new Date().toISOString()}.${fileType}`);
const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
const options: playwright.PageScreenshotOptions = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName };
const isElementScreenshot = params.element && params.ref;