node.jselectrongoogle-chrome-devtoolsnode-inspectorchrome-devtools-protocol

Access Electron API from a completely different system process


I am looking for a way to capture a screenshot of a VS Code extension host window. This window is opened when my extension's tests are being run (this is coming from the default Yeoman template for VS Code extensions).

I cannot use my extension's context as the extension only has access to the VS Code API and VS Code runs extensions in a different Node process from the Electron one (main or renderer). IPC is used through the API instead of running extensions in-process.

I can run this code in an Electron renderer process (using DevTools or as a part of the Electron application's script) to capture the Electron window:

const electron = require('electron');
electron.remote.getCurrentWindow().capturePage(image => {
  //electron.clipboard.writeImage(image);
  electron.clipboard.writeText(image.toDataURL());
  console.log('Data URL is in clipboard.');
});

I have verified that placing this in VS Code window DevTools will produce the correct Data URI.

In order to be able to do this from a different Node process, knowing only the Code window PID, I figured I would attach a debugger to the VS Code extension host Electron window and using CDP I would issue a Runtime.evaluate call to run the above code as if it was entered into the DevTools.

However, I am struggling with attaching the debugger. There are generally two ways to do it:

I am interested in Windows right now, so I issue the Windows line from a new Node process. What should happen upon successful debugger attachment is the target process should print out something like this:

Debugger listening on ws://127.0.0.1:9229/cf49bcfe-d922-4f89-b438-57755d254198
For help see https://nodejs.org/en/docs/inspector

However in my case, this only works if I start the barebones Electron app with --inspect and then issue process._debugProcess(proc.pid);, without --inspect it doesn't throw an error, but doesn't attach the debugger either.


Solution

  • process._debugProcess now works for me with Electron 5.