Jump to content
Sign in to follow this  
vhanla

How can I set WebView2 environment options?

Recommended Posts

I need to change language and disable CSP in WebView2, I've read it can be done on CoreWebView2EnvironmentOptions, there is a sample for C# at StackOverflow

CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions("--disable-web-security");
CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(null, null, options);
// EnsureCoreWebView2Async must be called before any other call
// to EnsureCoreWebView2Async and before setting the Source property
// since these will both cause initialization of the CoreWebView2 property
// but using a default CoreWebView2Environment rather than your custom one.
await webview2.EnsureCoreWebView2Async(environment);

And in Edge.pas there is InitializeWebView which creates it, but it passes nil to CoreWebView2EnvironmentOptions argument.

  var hr := CreateCoreWebView2EnvironmentWithOptions(PChar(BrowserExecutableFolder), PChar(UserDataFolder),

  nil,

    Callback<HResult, ICoreWebView2Environment>.CreateAs<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
      CreateEnvironmentCompleted));

So, I'm trying to pass that parameter instead of nil, how can I create/initialize ICoreWebView2EnvironmentOptions with those custom arguments?
 

I'm injecting JS code (like custom user scripts/extensions to one of my projects) and it is blocked each time due to content security policy. Maybe I'm not in the right direction, but I guess this is the reason it doesn't load/inject scripts from public CDNs.

I don't know how to initialize it, official documentation has some examples, like this at https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2environmentoptions?view=webview2-1.0.961.33
 

auto options = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
    CHECK_FAILURE(options->put_AllowSingleSignOnUsingOSPrimaryAccount(
        m_AADSSOEnabled ? TRUE : FALSE));
    if (!m_language.empty())
        CHECK_FAILURE(options->put_Language(m_language.c_str()));

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

Can you help me, please? I guess it is not using CoInitialize(IID_ICoreWebView2EnvironmentOptions,.... etc), I'm lost here. As of now, I'm still using to CEF4Delphi, but I guess WebView2 might be more useful since user's won't worry on updating as it is part of Edge Browser, which comes in Windows, so that might be more convenient. 😁

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×