WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

Allow changing the web resource response

Open cdz1980 opened this issue 3 years ago • 7 comments

hope to implement functions similar to cef IResponseFilter.Filter . Get the data from the server and return it to the browser after modification

It is troublesome to replace in WebResourceRequested

AB#38785742

cdz1980 avatar Apr 12 '22 00:04 cdz1980

Thanks for the feature request @cdz1980 - I've linked it to the item on our backlog tracking this work.

champnic avatar Apr 13 '22 03:04 champnic

@cdz1980 Can you share what scenario you are trying to accomplish by editing the response?

champnic avatar Apr 13 '22 03:04 champnic

In my scenario, it's ad-blocking of YouTube. You can block ads by removing 2 elements (adPlacements and playerAds) from JSON. And there's 2 ways to do it.

  1. Inject JavaScript and remove.
  2. Rewrite on WebResourceRequested.

1 is what all the browser extensions are doing. But I prefer C#. While negligible, it is also undesirable for performance. I'm currently using 2, because I'm already using it to block ads on other sites. But there's some issues.

  1. I have to request manually. That's a problem because...
    1. The code become complex. I'm not sure I didn't make a mistake.
    2. Method, content and headers are not compatible with what HttpClient is using. So I have to convert manually. For example, I have to set Content-Type to HttpRequestMessage.Content.Headers and others to HttpRequestMessage.Headers. It's a little weird.
    3. HttpClient is not using HTTP/3 by default. There may be other similar performance issue in the future and now that I don't know about.
  2. It doesn't fire on "https://www.youtube.com/watch?v=...". It works fine on "https://www.youtube.com/youtubei/v1/player". I don't know why (I've added AddWebResourceRequestedFilter("*", Microsoft.Web.WebView2.Core.CoreWebView2WebResourceContext.All); of course. And it's not cached.). WebResourceResponseReceived do not have that issue but I can't change the response.

So I want to change the response on WebResourceResponseReceived. In that case it should be very easy. But if Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequest have something like RequestAsync(), that should be fine for almost all the case.

kurema avatar Aug 29 '22 12:08 kurema

Other Issues mentioned that some headers are missing in WebResourceRequested (I didn't notice). WebResourceResponseReceived can also handle the case like data: and maybe cached data.

And one more. There's no reliable way to know which page is the source of the CoreWebView2. CoreWebView2.Source is not updated when WebView2WebResourceRequested is fired for first time. But there may be workaround and it's a bit off topic.

kurema avatar Aug 29 '22 12:08 kurema

I would like to be able to do this. If a response is of type pdf but does not have a Content-Disposition header which tells the browser to download it, I'd like to add it.

darbid avatar Sep 12 '22 17:09 darbid

Sometimes you need to change the content of the web page to other languages, or prompt the content. You know, the web page is usually not perfect

------------------ 原始邮件 ------------------ 发件人: "MicrosoftEdge/WebView2Feedback" @.>; 发送时间: 2022年4月13日(星期三) 中午11:20 @.>; 抄送: "逍遥 @.@.>; 主题: Re: [MicrosoftEdge/WebView2Feedback] Allow changing the web resource response (Issue #2355)

@cdz1980 Can you share what scenario you are trying to accomplish by editing the response?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

cdz1980 avatar Oct 11 '22 09:10 cdz1980

That's also desirable feature for us, we authenticate through browser, but after that calls are not made through web pages, after we are authenticated server sends generic page and not what our client wants to show, we would rather have custom message that makes sense to users

viktoriya-lysenko avatar Aug 27 '24 05:08 viktoriya-lysenko

Has this been implemented yet, and could you provide me with an example?

thanhlouis avatar Oct 09 '24 16:10 thanhlouis