gpt4 book ai didi

javascript - 在 CEF/CefSharp 中禁用 "Content Security Policy"

转载 作者:太空宇宙 更新时间:2023-11-03 15:18:47 25 4
gpt4 key购买 nike

我正在使用 CefSharp 进行网页的离屏渲染,我想在截取渲染页面的屏幕截图之前执行一段 JavaScript 代码。

不幸的是,在某些网页上使用 browser.EvaluateScriptAsync("document.body.scrollHeight")(例如 https://github.com/ )会导致以下错误:

Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src assets-cdn.github.com".

@ undefined:0:-1

这显然是由于内容安全策略 header 而发生的。

根据 this answer , 可以禁用 Firefox 中的安全性。是否也可以在 CEF 中禁用它?或者是否有可能以任何其他方式执行脚本?

最佳答案

我刚刚遇到了同样的问题,使用 cefsharp 在我不拥有的网站上测试一些 javascript 代码。最近的 cefsharp 版本拒绝使用 ExecuteJavascriptAsync() 处理此类请求。

我用粗暴的方式解决了我的问题,使用代理来禁用 CSP 检查。代理将元标记 http-equiv="content-security-policy"替换为虚拟标记以禁用它。请注意,我没有更改字符串长度以避免副作用,例如更改“Content-Length”http header 。

代理:https://github.com/justcoding121/Titanium-Web-Proxy

在这里你可以找到我的快速和肮脏的代码。

启动代理的代码:

        var proxyServer = new ProxyServer();

var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8123, true)
{
};

// An explicit endpoint is where the client knows about the existence of a proxy
// So client sends request in a proxy friendly manner
proxyServer.AddEndPoint(explicitEndPoint);
proxyServer.Start();

proxyServer.BeforeResponse 事件:

    private async Task Proxy_BeforeResponse(object sender, Titanium.Web.Proxy.EventArguments.SessionEventArgs e)
{

if (e.HttpClient.Response.Headers.Headers.ContainsKey("Content-Type"))
{
if (e.HttpClient.Response.Headers.Headers["Content-Type"].Value.Contains("text/html"))
{
string bodyString = await e.GetResponseBodyAsString();
e.SetResponseBodyString(bodyString.Replace("content-security-policy", "content-unsecure-policy"));
}
}
}

使用代理的cefsharp开关:

cefSettings.CefCommandLineArgs.Add("proxy-server", "http://localhost:8123");

关于javascript - 在 CEF/CefSharp 中禁用 "Content Security Policy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37978454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com