简体   繁体   English

有没有办法在 Windows WebView2 组件中进行自定义资源加载? 例如,像自定义 URL 方案?

[英]Is there a way to do custom resource loading in the Windows WebView2 component? Like a custom URL scheme, for instance?

I'm looking into using the WebView2 component for rendering some UI things on Windows, and I have a question about resource loading: for loading "normal" resources (ie HTML, CSS, images, JavaScript, whatever), the component mostly takes care of handling the loading of those resources itself. I'm looking into using the WebView2 component for rendering some UI things on Windows, and I have a question about resource loading: for loading "normal" resources (ie HTML, CSS, images, JavaScript, whatever), the component mostly takes care处理这些资源本身的加载。 But I wonder if there is a way to hook into that loading process and control it yourself with WebView2?但我想知道是否有一种方法可以连接到该加载过程并使用 WebView2 自己控制它?

As an example: say you want to load an image that is procedurally generated on native side of the WebView2, ie it is not in a file or on a server.例如:假设您要加载在 WebView2 的本机端程序生成的图像,即它不在文件或服务器上。 How would you do that?你会怎么做? Another example would be if you stored all your resources in a zip file or a SQLite database or something like that, and you wanted the WebView2 component to load resources directly from it (with you writing the "bridge" code yourself), how would you do that?另一个示例是,如果您将所有资源存储在 zip 文件或 SQLite 数据库或类似的数据库中,并且您希望 WebView2 组件直接从中加载资源(您自己编写“桥”代码),您将如何去做?

The reason I'm asking is because on macOS, WKWebView provides exactly this functionality by allowing you to create custom url schemes and hooking up handlers to them .我问的原因是因为在 macOS 上,WKWebView 通过允许您创建自定义 url 方案并将处理程序连接到它们来提供此功能。 This is exactly what I want, and it allows you to do something like this in your HTML:正是我想要的,它允许您在 HTML 中执行以下操作:

<script type="text/javascript" src="my-scheme://test.js"/>

And on the Objective-C side, you can do a thing like this (leaving out boilerplate for hooking up my-scheme to this handler, this is the meat of the code for handling the response):在 Objective-C 方面,你可以做这样的事情(省略了将my-scheme连接到这个处理程序的样板,这是处理响应的代码的核心):

        const char* data = "function testFunction() { return 18; }";

        [task didReceiveResponse: [[NSURLResponse alloc]
                                   initWithURL: task.request.URL
                                   MIMEType: @"text/javascript"
                                   expectedContentLength: strlen(data)
                                   textEncodingName: @"ASCII"]];
        
        [task didReceiveData: [NSData
                               dataWithBytes: data
                               length:strlen(data)]];
        
        [task didFinish];

Ie by registering the custom url scheme handler, I could send over my C string there as a JavaScript file.即通过注册自定义 url 方案处理程序,我可以将我的 C 字符串作为 JavaScript 文件发送到那里。 It doesn't have to be a hard-coded C string, obviously, as I mentioned the most relevant uses for me would be to provide procedurally generated resources, as well as loading things that are not necessarily stored as files (or on a web server).它不一定是硬编码的 C 字符串,显然,正如我提到的,对我来说最相关的用途是提供程序生成的资源,以及加载不一定存储为文件的东西(或在 web服务器)。 How would you do this with WebView2?您将如何使用 WebView2 执行此操作?

One way I have sent any type of file from native code to a browser is by converting the file to Base64 and then sending it to the browser.我将任何类型的文件从本机代码发送到浏览器的一种方法是将文件转换为 Base64,然后将其发送到浏览器。 In the case of WebView2 you could use ExecuteScriptAsync .在 WebView2 的情况下, 您可以使用 ExecuteScriptAsync Once the Base64 string is received you could have javascript code (which you have previously injected) to convert it into a blob/file and then you can add it to any part of the DOM you want.收到 Base64 字符串后,您可以使用 javascript 代码(您之前已注入)将其转换为 blob/文件,然后您可以将其添加到您想要的 DOM 的任何部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM