CefSharp Javascript registration and execution is not working in Release 79.1.36

China☆狼群 提交于 2020-06-29 04:27:21

问题


I tried to upgrade CefSharp from Version 69.0.0.0 to 79.1.36. I could not get the Javascript interaction working. The registration changed from

this.Browser.RegisterJsObject

to

this.Browser.JavascriptObjectRepository.Register

according to https://github.com/cefsharp/CefSharp/issues/2990.

When I execute EvaluateScriptAsync, I get a response back with Status Canceled.

Trying to understand how to implement it correctly, I examined the CefSharp.WpfExample and noticed that the Javascript functionality in the example WPF application does not work either. The Execute Javascript (asynchronously) does not do anything when clicking the Run button. The Evaluate Javascript (Async) returns:

Uncaught ReferenceError: bound is not defined @ about:blank:1:0

Did the Javascript functionality break in the latest release?

Update

Here is how it is used in our code.

This is the registration

public void RegisterJavaScriptHandler(string name, object handler)
{
    try
    {
        CefSharpSettings.LegacyJavascriptBindingEnabled = true;
        this.Browser.JavascriptObjectRepository.Register(name, handler, false, new BindingOptions() { CamelCaseJavascriptNames = false });
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

This is the EvaluateScriptAsync part

public void InitializeLayers()
{
    try
    {
        int count = _mapLogic.Layers.Count();
        foreach (WMSLayer layer in _mapLogic.Layers)
        {
            if (!_loadedLayers.Contains(layer))
            {
                var script = string.Format("addWMSLayer('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}')",
                    layer.ProviderCode.Url, layer.AttributionText, layer.AttributionHref,
                    layer.Layer, layer.FormatCode.Format, layer.ServerType, layer.Res1, layer.Res2, layer.Res3, layer.Res4);

                var response = this.ECBBrowser.Browser.EvaluateScriptAsync(script, new TimeSpan(0, 0, 1));
                response.ContinueWith(t =>
                {
                    count--;
                    if (count == 0) this.initializeMap();

                });
                _loadedLayers.Add(layer);
            }
            else
            {
                count--;
                if(count == 0) this.initializeMap();
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

Update II

I now believe something has changed with the resource loading. This is what I have (unimportant parts are left out).

public class ECBSchemeHandler : IResourceHandler
{
    private string _mimeType;
    private Stream _stream;

    public bool Open(IRequest request, out bool handleRequest, ICallback callback)
    {
        var result = open(request, callback);
        handleRequest = result;
        return result;
    }

    public bool Read(Stream dataOut, out int bytesRead, IResourceReadCallback callback)
    {
        return read(dataOut, out bytesRead, callback);
    }

    public bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback)
    {
        return read(dataOut, out bytesRead, callback);
    }

    private bool open(IRequest request, ICallback callback)
    {
        var u = new Uri(request.Url);
        var file = u.Authority + u.AbsolutePath;

        var ass = Assembly.GetExecutingAssembly();
        var resourcePath = ECBConfiguration.DEFAULT_ASSEMBLY_NAMESPACE + "." + file.Replace("/", ".");

        if (ass.GetManifestResourceInfo(resourcePath) != null)
        {
            Task.Run(() =>
            {
                using (callback)
                {
                    _stream = ass.GetManifestResourceStream(resourcePath);
                    var fileExtension = Path.GetExtension(file);
                    _mimeType = ResourceHandler.GetMimeType(fileExtension);
                    callback.Continue();
                }
            });
            return true;
        }
        else
        {
            callback.Dispose();
        }           

        return false;
    }

    private bool read(Stream dataOut, out int bytesRead, IDisposable callback)
    {
        callback.Dispose();

        if (_stream == null)
        {
            bytesRead = 0;
            return false;
        }

        //Data out represents an underlying buffer (typically 32kb in size).
        var buffer = new byte[dataOut.Length];
        bytesRead = _stream.Read(buffer, 0, buffer.Length);

        dataOut.Write(buffer, 0, buffer.Length);

        return bytesRead > 0;
    }
}

}


回答1:


Using the built-in ResourceHandlers as pointed out by @amaitland solved the problem with the Javascript registration.



来源:https://stackoverflow.com/questions/61459293/cefsharp-javascript-registration-and-execution-is-not-working-in-release-79-1-36

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!