@@ -58,6 +58,18 @@ function getNetworkMultiplierFromString(condition: string | null): number {
5858 return 1 ;
5959}
6060
61+ function getExtensionFromMimeType ( mimeType : string ) {
62+ switch ( mimeType ) {
63+ case 'image/png' :
64+ return 'png' ;
65+ case 'image/jpeg' :
66+ return 'jpeg' ;
67+ case 'image/webp' :
68+ return 'webp' ;
69+ }
70+ throw new Error ( `No mapping for Mime type ${ mimeType } .` ) ;
71+ }
72+
6173export class McpContext implements Context {
6274 browser : Browser ;
6375 logger : Debugger ;
@@ -346,18 +358,29 @@ export class McpContext implements Context {
346358 const dir = await fs . mkdtemp (
347359 path . join ( os . tmpdir ( ) , 'chrome-devtools-mcp-' ) ,
348360 ) ;
349- const ext =
350- mimeType === 'image/png'
351- ? 'png'
352- : mimeType === 'image/jpeg'
353- ? 'jpg'
354- : 'webp' ;
355- const filename = path . join ( dir , `screenshot.${ ext } ` ) ;
361+
362+ const filename = path . join (
363+ dir ,
364+ `screenshot.${ getExtensionFromMimeType ( mimeType ) } ` ,
365+ ) ;
356366 await fs . writeFile ( filename , data ) ;
357367 return { filename} ;
358368 } catch ( err ) {
359369 this . logger ( err ) ;
360- throw new Error ( 'Could not save a screenshot to a file' ) ;
370+ throw new Error ( 'Could not save a screenshot to a file' , { cause : err } ) ;
371+ }
372+ }
373+ async saveFile (
374+ data : Uint8Array < ArrayBufferLike > ,
375+ filename : string ,
376+ ) : Promise < { filename : string } > {
377+ try {
378+ const filePath = path . resolve ( filename ) ;
379+ await fs . writeFile ( filePath , data ) ;
380+ return { filename} ;
381+ } catch ( err ) {
382+ this . logger ( err ) ;
383+ throw new Error ( 'Could not save a screenshot to a file' , { cause : err } ) ;
361384 }
362385 }
363386
0 commit comments