Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

I can't change the Webview 4 header. Please fix it.

Post Reply
sodlf159
Posts: 90
Joined: Thu Nov 09, 2023 1:55 pm

I can't change the Webview 4 header. Please fix it.

Post by sodlf159 »

procedure TBrowserFrame.WVBrowser1WebResourceRequested(Sender: TObject;
const aWebView: ICoreWebView2;
const aArgs: ICoreWebView2WebResourceRequestedEventArgs);
var
TempArgs : TCoreWebView2WebResourceRequestedEventArgs;
RequestHeaders:ICoreWebView2HttpRequestHeaders;
begin
TempArgs:= TCoreWebView2WebResourceRequestedEventArgs.Create(aArgs);
try
TempArgs.Request.Get_Headers(RequestHeaders);
RequestHeaders.SetHeader('Accept-Language','zh-cn;q=0.8,en-US;q=0.9');

finally
TempArgs.Free ;
end;
end;

No response.
User avatar
salvadordf
Posts: 4563
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: I can't change the Webview 4 header. Please fix it.

Post by salvadordf »

Hi,

WebView4Delphi is just a WebView2 wrapper.

If setting a new header is not working you should contact Microsoft at the WebView2 feedback repository and report the issue :
https://github.com/MicrosoftEdge/WebView2Feedback/issues

It should be possible to set the language and the accept-languages header with TWVBrowserBase.Language before creating the browser.
You can also set GlobalWebView2Loader.Language before the GlobalWebView2Loader.StartWebView2 call.
sodlf159
Posts: 90
Joined: Thu Nov 09, 2023 1:55 pm

Re: I can't change the Webview 4 header. Please fix it.

Post by sodlf159 »

So does this mean that custom headers cannot be changed?
User avatar
salvadordf
Posts: 4563
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: I can't change the Webview 4 header. Please fix it.

Post by salvadordf »

The documentation states that the headers are mutable at that event but it wouldn't be the first time I see that Chromium or Edge don't allow us to modify some of them.
sodlf159
Posts: 90
Joined: Thu Nov 09, 2023 1:55 pm

Re: I can't change the Webview 4 header. Please fix it.

Post by sodlf159 »

procedure TMainForm.WVBrowser1AfterCreated(Sender: TObject);

WVBrowser1.AddWebResourceRequestedFilter('*', COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);
WVBrowser1.AddWebResourceRequestedFilterWithRequestSourceKinds('*',COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE, COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_ALL);
WVBrowser1.AddWebResourceRequestedFilterWithRequestSourceKinds('*',COREWEBVIEW2_WEB_RESOURCE_CONTEXT_MEDIA,COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_ALL);

It is solved by putting
Post Reply