chore: allow specifying output dir (#285)

Ref: https://github.com/microsoft/playwright-mcp/issues/279
This commit is contained in:
Pavel Feldman
2025-04-28 16:35:33 -07:00
committed by GitHub
parent 6e76d5e550
commit 697a69a8c2
6 changed files with 52 additions and 11 deletions

View File

@@ -14,8 +14,12 @@
* limitations under the License.
*/
import fs from 'fs';
import net from 'net';
import os from 'os';
import path from 'path';
import { sanitizeForFilePath } from './tools/utils';
import type { Config } from '../config';
import type { LaunchOptions, BrowserContextOptions } from 'playwright';
@@ -84,3 +88,10 @@ async function findFreePort() {
server.on('error', reject);
});
}
export async function outputFile(config: Config, name: string): Promise<string> {
const result = config.outputDir ?? os.tmpdir();
await fs.promises.mkdir(result, { recursive: true });
const fileName = sanitizeForFilePath(name);
return path.join(result, fileName);
}

View File

@@ -14,14 +14,11 @@
* limitations under the License.
*/
import os from 'os';
import path from 'path';
import { z } from 'zod';
import { defineTool } from './tool';
import { sanitizeForFilePath } from './utils';
import * as javascript from '../javascript';
import { outputFile } from '../config';
const pdf = defineTool({
capability: 'pdf',
@@ -34,7 +31,7 @@ const pdf = defineTool({
handle: async context => {
const tab = context.currentTabOrDie();
const fileName = path.join(os.tmpdir(), sanitizeForFilePath(`page-${new Date().toISOString()}`)) + '.pdf';
const fileName = await outputFile(context.config, `page-${new Date().toISOString()}'.pdf'`);
const code = [
`// Save page as ${fileName}`,

View File

@@ -14,14 +14,11 @@
* limitations under the License.
*/
import path from 'path';
import os from 'os';
import { z } from 'zod';
import { sanitizeForFilePath } from './utils';
import { defineTool } from './tool';
import * as javascript from '../javascript';
import { outputFile } from '../config';
import type * as playwright from 'playwright';
@@ -232,7 +229,7 @@ const screenshot = defineTool({
const tab = context.currentTabOrDie();
const snapshot = tab.snapshotOrDie();
const fileType = params.raw ? 'png' : 'jpeg';
const fileName = path.join(os.tmpdir(), sanitizeForFilePath(`page-${new Date().toISOString()}`)) + `.${fileType}`;
const fileName = await outputFile(context.config, `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;