簡體   English   中英

WebView2代理C++

[英]WebView2 proxy 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);

所以我唯一需要的是提供通過 C++ 為 WebView2 設置代理的解決方案。


我有ICoreWebView2接口,但它沒有EnsureCoreWebView2Async方法。 另一方面,我有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());

而不是SERVER放 ip 地址或其他東西。


我測試過,它可以工作,但似乎有一個錯誤(或功能):你不能創建兩個或多個具有不同運行參數的 webview。

對於那些希望指定 webview 環境選項並且在 C++ 中找不到類似於CoreWebView2EnvironmentOptions的 CoreWebView2EnvironmentOptions 的人。 訪問 ICoreWebView2EnvironmentOptions 的文檔頁面,您應該會找到這一行:

A default implementation is provided in WebView2EnvironmentOptions.h

包括 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