简体   繁体   English

WebView2代理C++

[英]WebView2 proxy C++

I found this thread on GitHub, but seems that the code is not C++:我在 GitHub 上找到了这个线程,但似乎代码不是 C++:

WebView2 _webView2 = new WebView2();

CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions();

// Set a proxy pac for the browser   
//      options.AdditionalBrowserArguments = "--proxy-pac-url=http://myproxy.com/my.pac";

// Set the proxy for the browser
options.AdditionalBrowserArguments = "--proxy-server=\"foopy:99\"";

// Create the environment manually
CoreWebView2Environment env = await CoreWebView2Environment.CreateAsync(null, null, options);
await _webView2 .EnsureCoreWebView2Async(env);

So the only what am I asking for is to provide the solution for setting up a proxy for WebView2 via C++.所以我唯一需要的是提供通过 C++ 为 WebView2 设置代理的解决方案。


I have ICoreWebView2 interface, but it doesn't have EnsureCoreWebView2Async method.我有ICoreWebView2接口,但它没有EnsureCoreWebView2Async方法。 In another hand, I have CoreWebView2EnvironmentOptions class.另一方面,我有CoreWebView2EnvironmentOptions class。

auto opt = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();

opt->put_AdditionalBrowserArguments(L"--proxy-server=\"SERVER\"");

CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, opt.Get(), Microsoft::WRL::Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hwnd](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {

...

}).Get());

Instead of SERVER put ip address or something else.而不是SERVER放 ip 地址或其他东西。


I tested, it works, but seems there is a bug (or feature): you can't create two or more webviews with different run-arguments.我测试过,它可以工作,但似乎有一个错误(或功能):你不能创建两个或多个具有不同运行参数的 webview。

To those looking to specify webview environment options and cannot find CoreWebView2EnvironmentOptions in C++ similar to what is available in.Net.对于那些希望指定 webview 环境选项并且在 C++ 中找不到类似于CoreWebView2EnvironmentOptions的 CoreWebView2EnvironmentOptions 的人。 Visit documentation page for ICoreWebView2EnvironmentOptions and you should find this line:访问 ICoreWebView2EnvironmentOptions 的文档页面,您应该会找到这一行:

A default implementation is provided in WebView2EnvironmentOptions.h

Include WebView2EnvironmentOptions.h and create CoreWebView2EnvironmentOptions as below包括 WebView2EnvironmentOptions.h 并创建 CoreWebView2EnvironmentOptions 如下

#include <WebView2EnvironmentOptions.h>

auto options = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
// Use options however you want. I simply added an argument to autoplay videos.
options->put_AdditionalBrowserArguments(L"--autoplay-policy=no-user-gesture-required");

HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(
        subFolder, m_userDataFolder.c_str(), options.Get(),
        Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
            this, &AppWindow::OnCreateEnvironmentCompleted)
            .Get());

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

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