Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoreWebView2_WebResourceRequested Not respons to ajax call. #1160

Closed
IrakliLomidze opened this issue Apr 7, 2021 · 4 comments
Closed

CoreWebView2_WebResourceRequested Not respons to ajax call. #1160

IrakliLomidze opened this issue Apr 7, 2021 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@IrakliLomidze
Copy link

Description
CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
Does not work with Ajax call.

CoreWebView2_WebResourceRequested Receives request but, it did not send back to the ajax caller.

Is it possible that CoreWebView2_WebResourceRequested acts as a webserver?

private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
{
if (e.Request.Uri.Trim().ToLower().StartsWith("http://history.codex.application/deletehistoryitem") == true)
{
// Do Somting
e.Response = environment.CreateWebResourceResponse(null, 200, "", "");
}
}

HTML

$.ajax({

        url : `http://history.codex.application/deletehistoryitem?event_id=${event_id}`,
        type : 'GET',
        dataType:'json',
        success : function(data, status, xhr) {              
            alert(data);
            alert(status);
            alert(xhr);
            if (xhr == 200)
            {

            }

        },
        error : function(request,error) {       
            alert("Cant Delete History: ");
        }
    });

....

public async Task InitializeAsync()
{
await webView21.EnsureCoreWebView2Async(null);

        webView21.CoreWebView2.Settings.AreDevToolsEnabled = true;// false;
        webView21.CoreWebView2.Settings.IsScriptEnabled = true;
        //webView21.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
        webView21.CoreWebView2.Settings.IsStatusBarEnabled = false;
        webView21.CoreWebView2.Settings.IsBuiltInErrorPageEnabled = false;
        webView21.CoreWebView2.Settings.IsWebMessageEnabled = true;
        webView21.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;// false;
        webView21.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;

        environment = await CoreWebView2Environment.CreateAsync();
    }

private async void CoreWebView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
await webView21.CoreWebView2.ExecuteScriptAsync(_codexApps.GetInitialScript());

        if (_codexApps.IsResponsive() == true)
        {

            webView21.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
            

            webView21.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
        }
    }
@IrakliLomidze IrakliLomidze added the bug Something isn't working label Apr 7, 2021
@champnic
Copy link
Member

Hey @IrakliLomidze - yes, part of the intent with WebResourceRequested is to be able to short-circuit a network call and instead provide the response from the host app. At what point in your scenario is this not working?

@champnic champnic self-assigned this Apr 10, 2021
@IrakliLomidze
Copy link
Author

CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
is reciving request but when I create response it does not go to back HTML code.

In Webview2 Devtools Ajax call, I have network status pending.
Do I need a specific configuration to able to send a response back to HTML?

But I think the response is sending somewhere else because on an HTML page (Dev Tools shows that my js code still awaiting response)
Steps:
private CoreWebView2Environment environment;
...
environment = await CoreWebView2Environment.CreateAsync(userDataFolder: MYDIR);
await webView21.EnsureCoreWebView2Async(environment);

...
webView21.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
webView21.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;

...

e.Response = environment.CreateWebResourceResponse(null, 200, "", "");
....


Environment : Windows 10 / 20H1, .NET 4.8 WinForm / VS 2019 Latest Uptate / WebView Latest Neget Update

@IrakliLomidze
Copy link
Author

I have found solution:

  1. e.Response = environment.CreateWebResourceResponse(null, 200, "", ""); Will never work with Ajax
    Use e.Response = environment.CreateWebResourceResponse(null, 200, "OK", ""); Insted

And most important that is not documented in WebView2 Documentation, When the page is loaded from the Local File system.
And you made calls to some HTTP or HTTPS resources there is an active CORE restriction policy of Chromium.

Do not forget disable CORE Policy:
var op = new CoreWebView2EnvironmentOptions("--disable-web-security");
environment = await CoreWebView2Environment.CreateAsync(options :op);

And champnic Please made possible custom URLs. like myappname:\action\params ...

@champnic
Copy link
Member

Glad you've got it working @IrakliLomidze! We're are tracking custom urls in #173.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants