Puppeteer 中新选项卡上的 SetBypassCSP

SetBypassCSP on new tab in Puppeteer

我将在 Puppeteer 中打开以下 html 页面:

<!-- head -->

<body>
    <a class="btn-tab" href="html_simple.html" target="_blank">new tab</a>
</body>

<!-- more web -->

单击该按钮将打开一个新选项卡。

在我的 javascript 代码中,我有以下内容:

const context = page.browserContext();


context.on('targetcreated', async (target) => {
    const newPage = await target.page();
    if (newPage) {
        await newPage.setBypassCSP(true);
    }
});

但是,当打开新页面时,CSP 绕过仍然被禁用(加载某些脚本时会引发内容安全错误)。我认为这是因为 targetcreated 事件是在新页面导航之后引发的,并且正如 Puppeteer 文档所述:

NOTE CSP bypassing happens at the moment of CSP initialization rather then evaluation. Usually this means that page.setBypassCSP should be called before navigating to the domain.

情况似乎如此,因为在加载这些脚本之前执行 page.reload()setBypassCSP 似乎有效。

我的问题是如何在网络打开的选项卡上将 bypassCSP 设置为 true?在 Puppeteer 中加载内容或全局设置之前是否有允许我执行此操作的事件?

根据 https://github.com/GoogleChrome/puppeteer/issues/4764 目前还不可能。在打开选项卡并将 setBypassCSP 设置为 true 后使用 .reload 的替代方法。

如果问题来自 addScriptTag,使用评估代替将是避免 CSP 而不绕过的替代方法