CefSharp custom SchemeHandler

后端 未结 2 1539
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 16:13

Iam using CefSharp\'s SchemeHandler in order to grab resources from my C# project like .css, .js or .png files using a custom url for

相关标签:
2条回答
  • 2020-12-30 17:04

    If you simply need to return a string, then you can use ResourceHandler.FromString(html, mimeType). For this you just need to implement the ISchemeHandlerFactory.

    https://github.com/cefsharp/CefSharp/blob/cefsharp/47/CefSharp/ResourceHandler.cs#L98

    Example reading from a file https://github.com/cefsharp/CefSharp/blob/cefsharp/47/CefSharp.Example/CefSharpSchemeHandlerFactory.cs#L17 which can be translated to reading from a string quite simply.

    0 讨论(0)
  • 2020-12-30 17:09

    Since Cefsharp changed a bit in last few months here is an updated and easier way of handling 'file' protocol. I wrote blog post on this matter.

    What you want to add is your scheme handler and its factory:

    using System;
    using System.IO;
    using CefSharp;
    
    namespace MyProject.CustomProtocol
    {
        public class CustomProtocolSchemeHandler : ResourceHandler
        {
            // Specifies where you bundled app resides.
            // Basically path to your index.html
            private string frontendFolderPath;
    
            public CustomProtocolSchemeHandler()
            {
                frontendFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./bundle/");
            }
    
            // Process request and craft response.
            public override bool ProcessRequestAsync(IRequest request, ICallback callback)
            {
                var uri = new Uri(request.Url);
                var fileName = uri.AbsolutePath;
    
                var requestedFilePath = frontendFolderPath + fileName;
    
                if (File.Exists(requestedFilePath))
                {
                    byte[] bytes = File.ReadAllBytes(requestedFilePath);
                    Stream = new MemoryStream(bytes);
    
                    var fileExtension = Path.GetExtension(fileName);
                    MimeType = GetMimeType(fileExtension);
    
                    callback.Continue();
                    return true;
                }
    
                callback.Dispose();
                return false;
            }
        }
    
        public class CustomProtocolSchemeHandlerFactory : ISchemeHandlerFactory
        {
            public const string SchemeName = "customFileProtocol";
    
            public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
            {
                return new CustomProtocolSchemeHandler();
            }
        }
    }
    

    And then register it before calling Cef.Initialize:

    var settings = new CefSettings
    {
      BrowserSubprocessPath = GetCefExecutablePath()
    };
    
    settings.RegisterScheme(new CefCustomScheme
    {
      SchemeName = CustomProtocolSchemeHandlerFactory.SchemeName,
      SchemeHandlerFactory = new CustomProtocolSchemeHandlerFactory()
    });
    
    0 讨论(0)
提交回复
热议问题