chrome-debugging-client
An async/await friendly Chrome debugging client with TypeScript support, designed with automation in mind.
Table of Contents
Features
- Promise API for async/await (most debugger commands are meant to be sequential).
- TypeScript support and uses "devtools-protocol" types, allowing you to pick a protocol version.
- Launches Chrome with a new temp user data folder so Chrome launches an isolated instance. (regardless if you already have Chrome open).
- Opens Chrome with a pipe message transport to the browser connection and supports attaching flattened session connections to targets.
- Supports cancellation in a way that avoids unhandled rejections, and allows you to add combine additional cancellation concerns.
- Supports seeing protocol debug messages with
DEBUG=chrome-debugging-client:*
- Use with race-cancellation library to add timeouts or other cancellation concerns to tasks using the connection.
- The library was designed to be careful about not floating promises (promises are chained immediately after being created, combining concurrent promises with all or race), this avoids unhandled rejections.
Examples
Print URL as PDF
#!/usr/bin/env nodeconst writeFileSync = ;const spawnChrome = ; /** * Print a url to a PDF file. * @param url * @param file */ { const chrome = ; try const browser = chromeconnection; // we create with a target of about:blank so that we can // setup Page events before navigating to url const targetId = await browser; const page = await browser; // enable events for Page domain await page; // concurrently wait until load and navigate await Promiseall page page ; const data = await page; ; // attempt graceful close await chrome; finally // kill process if hasn't exited await chrome; console;} if processargvlength < 4 console; console; process; ;
Node Debugging
#!/usr/bin/env nodeconst spawnWithWebSocket = ; { const script = `const obj = { hello: "world", }; console.log("end"); `; // start node requesting it break on start at debug port that // is available const node = await ; { const connection = node; // Setup console api handler connection; // We requested Node to break on start, so we runIfWaitingForDebugger // and wait for it to break at the start of our script. // These commands must be sent concurrently with // the pause event setup. const callFrames: location: scriptId reason = await Promiseall connection connection connection connection ; // Right now we are paused at the start of the script console; //= paused: Break on start console; await connection; console; const breakpoint = await Promiseall connection connection ; const callFrames: location callFrameId = breakpoint; console; console; const result = await connection; console; //= {"hello":"world"} console; await Promiseall connection connection ; } try await ; // Node is still alive here and waiting for the debugger to disconnect console; node; // Node should exit on its own after the websocket closes console; await node; console; finally // kill process if still alive await node; } ;