我正在尝试收集Chrome浏览器日志:浏览器发出的警告,例如弃用和干预。例如,对于网站https://uriyaa.wixsite.com/corvid-cli2:
A cookie associated with a cross-site resource at http://wix.com/ was set without the `SameSite` attribute.
A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`.
You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
我认为以下代码可以解决问题,但是它只能捕获由页面代码生成的日志。
(async ()=> {
const browser = await puppeteer.launch({dumpio: true});
const page = await browser.newPage();
page.on('console', msg => {
for (let i = 0; i < msg._args.length; ++i)
console.log(`${i}: ${msg._args[i]}`);
});
await page.goto('https://uriyaa.wixsite.com/corvid-cli2', {waitUntil: 'networkidle2', timeout: 20000});
await page.screenshot({path: 'screenshot.png'});
await browser.close();
})();
bellow与我认为不相关,因为Reportingobserver不会在没有sameSite的Cookie上捕获镶边信息: 通过阅读该主题,我进入了https://developers.google.com/web/updates/2018/07/reportingobserver,但我不确定如何使用它,例如在浏览器控制台不起作用的示例中。
我不确定应该在哪个上下文中使用观察者代码,或者不确定浏览器是否需要标记来激活报告API。或者,如果这是解决问题的方法。
欢迎您。
答案 0 :(得分:1)
大概,'console'
事件仅捕获页面中的console.log()
和类似的调用。但是看来您可以使用CDPSession
通过Log Domain从浏览器捕获警告。不幸的是,它仅适用于强大的浏览器:
'use strict';
const puppeteer = require('puppeteer');
(async function main() {
try {
const browser = await puppeteer.launch({ headless: false });
const [page] = await browser.pages();
const cdp = await page.target().createCDPSession();
await cdp.send('Log.enable');
cdp.on('Log.entryAdded', async ({ entry }) => {
console.log(entry);
});
await page.goto('https://uriyaa.wixsite.com/corvid-cli2');
} catch (err) {
console.error(err);
}
})();
其中一项:
{
source: 'other',
level: 'warning',
text: 'A cookie associated with a cross-site resource at http://www.wix.com/ was set without the `SameSite` attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.',
timestamp: 1589058118372.802,
url: 'https://uriyaa.wixsite.com/corvid-cli2'
}
答案 1 :(得分:1)
当您启动 Puppeteer 浏览器时,在选项对象中,您应该将 dumpio
设置为 true
:
await puppeteer.launch({ dumpio: true });
这基本上将“将浏览器进程 stdout 和 stderr 导入 process.stdout 和 process.stderr”,这意味着它将浏览器日志重定向到您正在运行的任何主进程、服务器等。
您可以在此处查看启动 Puppeteer 时可以使用的此选项和其他启动选项:https://www.puppeteersharp.com/api/PuppeteerSharp.LaunchOptions.html