2

I am using the Chrome DevTools Protocol API functions to listen to the javascriptDialogOpening event. Below is the code.

async navigate(url: URL, target: any) {

  const { Page } = target.connection;

  // Enable the required protocol features
  await Page.enable();

  // Add a listener and handler for JavaScript dialogs
  Page.javascriptDialogOpening(async(event: Protocol.Page.JavascriptDialogOpeningEvent) => {

  // Respond with a confirmation to all dialogs
  const dialogResponse: Protocol.Page.HandleJavaScriptDialogRequest = {
      'accept': true
    };

  Page.handleJavaScriptDialog(dialogResponse);
  });
}

I got an error: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 Page.javascriptDialogOpening listeners added. Use emitter.setMaxListeners() to increase limit

Pradeep Gill
  • 318
  • 2
  • 9

1 Answers1

1

By putting the javascriptDialogOpening event registration in the navigate handler, you are adding the handler over and over again. Because every time a navigation event happens, you are re-adding this event handler.

What you really want here is to put the javascriptDialogOpening event registration in your initialization code. If you do this, it will be registered only once and you will no longer have this issue.

CDP({'host': host, 'port': port}, (client) => {
// ...
    Page.javascriptDialogOpening((params) => {
      Page.handleJavaScriptDialog({'accept': true});
    });
Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152