chore: use persistent profile by default (#41)

Fixes https://github.com/microsoft/playwright-mcp/issues/29
This commit is contained in:
Pavel Feldman
2025-03-26 15:02:45 -07:00
committed by GitHub
parent 5345a7b4df
commit 6ff4500211
11 changed files with 122 additions and 69 deletions

View File

@@ -36,7 +36,7 @@ export const navigate: ToolFactory = snapshot => ({
},
handle: async (context, params) => {
const validatedParams = navigateSchema.parse(params);
const page = await context.ensurePage();
const page = await context.createPage();
await page.goto(validatedParams.url, { waitUntil: 'domcontentloaded' });
// Cap load event to 5 seconds, the page is operational at this point.
await page.waitForLoadState('load', { timeout: 5000 }).catch(() => {});
@@ -60,10 +60,7 @@ export const goBack: ToolFactory = snapshot => ({
inputSchema: zodToJsonSchema(goBackSchema),
},
handle: async context => {
return await runAndWait(context, 'Navigated back', async () => {
const page = await context.ensurePage();
await page.goBack();
}, snapshot);
return await runAndWait(context, 'Navigated back', async page => page.goBack(), snapshot);
},
});
@@ -76,10 +73,7 @@ export const goForward: ToolFactory = snapshot => ({
inputSchema: zodToJsonSchema(goForwardSchema),
},
handle: async context => {
return await runAndWait(context, 'Navigated forward', async () => {
const page = await context.ensurePage();
await page.goForward();
}, snapshot);
return await runAndWait(context, 'Navigated forward', async page => page.goForward(), snapshot);
},
});
@@ -95,8 +89,7 @@ export const wait: Tool = {
},
handle: async (context, params) => {
const validatedParams = waitSchema.parse(params);
const page = await context.ensurePage();
await page.waitForTimeout(Math.min(10000, validatedParams.time * 1000));
await new Promise(f => setTimeout(f, Math.min(10000, validatedParams.time * 1000)));
return {
content: [{
type: 'text',
@@ -133,7 +126,7 @@ export const pdf: Tool = {
inputSchema: zodToJsonSchema(pdfSchema),
},
handle: async context => {
const page = await context.ensurePage();
const page = await context.existingPage();
const fileName = path.join(os.tmpdir(), `/page-${new Date().toISOString()}.pdf`);
await page.pdf({ path: fileName });
return {

View File

@@ -29,7 +29,7 @@ export const screenshot: Tool = {
},
handle: async context => {
const page = await context.ensurePage();
const page = await context.existingPage();
const screenshot = await page.screenshot({ type: 'jpeg', quality: 50, scale: 'css' });
return {
content: [{ type: 'image', data: screenshot.toString('base64'), mimeType: 'image/jpeg' }],
@@ -55,7 +55,7 @@ export const moveMouse: Tool = {
handle: async (context, params) => {
const validatedParams = moveMouseSchema.parse(params);
const page = await context.ensurePage();
const page = await context.existingPage();
await page.mouse.move(validatedParams.x, validatedParams.y);
return {
content: [{ type: 'text', text: `Moved mouse to (${validatedParams.x}, ${validatedParams.y})` }],

View File

@@ -30,7 +30,7 @@ export const snapshot: Tool = {
},
handle: async context => {
return await captureAriaSnapshot(await context.ensurePage());
return await captureAriaSnapshot(await context.existingPage());
},
};

View File

@@ -72,7 +72,7 @@ async function waitForCompletion<R>(page: playwright.Page, callback: () => Promi
}
export async function runAndWait(context: Context, status: string, callback: (page: playwright.Page) => Promise<any>, snapshot: boolean = false): Promise<ToolResult> {
const page = await context.ensurePage();
const page = await context.existingPage();
await waitForCompletion(page, () => callback(page));
return snapshot ? captureAriaSnapshot(page, status) : {
content: [{ type: 'text', text: status }],