我正在 Nodejs 中使用 Chrome 和 Chrome DevTools 协议(https://chromedevtools.github.io/devtools-protocol/)开发网络自动化系统。
调用 Runtime.consoleAPICalled 方法工作正常,但是当我打开已启动 Chrome 的 devtools 时,它不再工作。
无论 Chrome 开发工具如何,如何调用 API?
const CDP = require('chrome-remote-interface');
const chromeLauncher = require('chrome-launcher');
(async function launch() {
const chrome = await chromeLauncher.launch({
port: 9222,
chromeFlags: [
'--window-size=1024,768',
'--disable-gpu'
]
});
const protocol = await CDP({ port: chrome.port });
const { Page } = protocol;
const { Runtime } = protocol;
await Page.enable();
await Runtime.enable();
Runtime.consoleAPICalled(function(params) {
// Not working when I open Chrome devtools.
console.log('Runtime.consoleAPICalled', params);
});
Page.loadEventFired(async() => {
await Runtime.evaluate({
"expression": `
setInterval(function () {
console.log(new Date());
}, 1000);
`
});
});
Page.navigate({
url: 'https://www.google.com'
});
})();