HP.*_*HP. 6 javascript node.js google-chrome-devtools google-chrome-headless
我在这里发布这个问题:
https://github.com/cyrus-and/chrome-remote-interface/issues/105
但我似乎无法console.log在Mac终端输出.它可能在Chrome Devtools窗口内,我看不到.
那么如何通过Runtime.evaluate表达式在Mac终端中获得console.log输出?
我的代码如下:
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
const file = require('fs');
(async function() {
async function launchChrome() {
return await chromeLauncher.launch({
chromeFlags: [
'--headless',
'--disable-gpu'
]
});
}
const chrome = await launchChrome();
const protocol = await CDP({
port: chrome.port
});
const {
DOM,
Network,
Page,
Emulation,
Runtime
} = protocol;
await Promise.all([Network.enable(), Page.enable(), Runtime.enable(), DOM.enable()]);
Page.navigate({url: 'https://www.chromestatus.com/'});
Page.loadEventFired(async () => {
const result = await Runtime.evaluate({expression: 'console.log(\'aaa\')'});
protocol.close();
chrome.kill();
});
})();
Run Code Online (Sandbox Code Playgroud)
msc*_*ker 10
我一直在对此进行一些研究;以下是我的一些发现:
评估时:
const result = await Runtime.evaluate({ expression: 'console.log("aaa")' });
Run Code Online (Sandbox Code Playgroud)
结果总是:
{ 结果:{ 类型:'未定义' } }
但是,以下表达式:
const result = await Runtime.evaluate({expression: 'window.location.toString()'});
Run Code Online (Sandbox Code Playgroud)
返回:
{ 结果:{ 类型:'字符串',值:' https://www.chromestatus.com/features ' } }
现在,如果我在不调用它的情况下评估该函数:
const result = await Runtime.evaluate({ expression: 'console.log' });
Run Code Online (Sandbox Code Playgroud)
结果设置为:
{ result: { type: 'function', className: 'Function', description: 'function log() { [native code] }', objectId: '{"injectedScriptId":2,"id":1}' } }
于是我又做了一些挖掘,发现每次console.log求值时调用,Console对象上的messageAdded事件总是被触发。
因此,我继续启用了 Console 对象,并向 messageAdded 事件添加了一个侦听器,现在我可以按预期成功捕获控制台条目。
这就是我所做的:
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
const file = require('fs');
(async function() {
async function launchChrome() {
return await chromeLauncher.launch({
chromeFlags: [
'--headless',
'--disable-gpu'
]
});
}
const chrome = await launchChrome();
const protocol = await CDP({
port: chrome.port
});
const {
DOM,
Network,
Page,
Emulation,
Runtime,
Console
} = protocol;
await Promise.all([Network.enable(), Page.enable(), DOM.enable(), Runtime.enable(), Console.enable()]);
await Page.navigate({url: 'https://www.chromestatus.com/'});
// REMARKS: messageAdded is fired every time a new console message is added
Console.messageAdded((result) => {
console.log(result);
});
Page.loadEventFired(async () => {
const result = await Runtime.evaluate({ expression: 'console.log("aaa")' });
// REMARKS: When evaluating console.log, result.result.value is undefined.
console.log(result);
protocol.close();
chrome.kill();
});
})();
/* Output from listening on messageAdded:
{ message:
{ source: 'console-api',
level: 'log',
text: 'aaa',
url: '',
line: 1,
column: 9 } }
*/
Run Code Online (Sandbox Code Playgroud)
我从Chrome DevTools Protocol Viewer - Console得到了详细信息
从文档:
已添加 Console.message
添加新控制台消息时发出。
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
4913 次 |
| 最近记录: |