Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “禁用”;“内容安全策略”;在CEF/CefSharp_Javascript_C#_Chromium Embedded_Cefsharp - Fatal编程技术网

Javascript “禁用”;“内容安全策略”;在CEF/CefSharp

Javascript “禁用”;“内容安全策略”;在CEF/CefSharp,javascript,c#,chromium-embedded,cefsharp,Javascript,C#,Chromium Embedded,Cefsharp,我正在使用CefSharp进行网页的屏幕外渲染,我想在截图之前执行一段JavaScript代码 在某些网页(例如)上使用browser.EvaluateScriptAsync(“document.body.scrollHeight”)会导致以下错误: Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in t

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

在某些网页(例如)上使用
browser.EvaluateScriptAsync(“document.body.scrollHeight”)
会导致以下错误:

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
这显然是由于内容安全策略头导致的


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

CSP限制仅在您评估脚本时适用,如果您只是执行JavaScript,则限制不适用。 您可以使用ExecuteJavaScriptAsync。 如果需要返回值,可以使用
GetMainFrame().ExecuteJavaScriptAsync(“myRegistrObject.retVal(document.body.scrollHeight)”


retVal
必须接受字符串或int的适当类型。

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

我用一种残酷的方式解决了我的问题,使用代理来禁用CSP检查。 代理将元标记http equiv=“content security policy”替换为一个伪标记以禁用它。请注意,我没有更改字符串长度以避免诸如更改“Content length”http头之类的副作用

代理人:

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

启动代理的代码:

        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();
cefSettings.CefCommandLineArgs.Add("proxy-server", "http://localhost:8123");
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"));
            }
        }
    }
要使用代理,请执行以下操作:

        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();
cefSettings.CefCommandLineArgs.Add("proxy-server", "http://localhost:8123");

您可能需要对此进行自己的研究,理论上您可以修改
标题
,并修改
CSP
(也可以选择删除它们)。请参见,您也可以尝试一些命令行开关。CEF也支持它们。需要注意的是,
CEF
只实现这些命令行开关的一个子集。您必须检查源代码以查看是否实现了某个特定的源代码。您找到解决方案了吗?我仍然遇到这个问题。奇怪的是,你可以在控制台上运行
eval
,因此应该可以对某些脚本禁用CSP。不幸的是,我从未找到解决此问题的方法,我最终不需要使用它。如果你解决了问题,请随时发布解决方案,我肯定会接受!实际上,如果您试图在实现CSP的链接中注入代码,这种方法就不起作用。