问题描述
我正在使用Chrome DevTools协议API函数来监听javascriptDialogOpening
事件.下面是代码.
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);
});
}
我收到一个错误:MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏.添加了11个Page.javascriptDialogOpening
侦听器.使用generator.setMaxListeners()增加限制
I got an error: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 Page.javascriptDialogOpening
listeners added. Use emitter.setMaxListeners() to increase limit
推荐答案
通过将javascriptDialogOpening
事件注册放置在处理程序中,您将一次又一次添加处理程序.因为每次导航事件发生,您都要重新添加此事件处理程序.
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.
这里真正想要的是将javascriptDialogOpening
事件注册放入初始化代码中.如果您这样做,它将仅被注册一次,并且您将不再遇到此问题.
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});
});
这篇关于Chrome DevTools协议:如何退订Page.javascriptDialogOpening事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!