Page 1 of 1

Try to block GIF content(not URL) but don't know how.

Posted: Tue Feb 18, 2025 5:42 pm
by nbuyer
Hello,

I recently ran into a problem where I wanted to block some ads in my app's browser, usually by blocking the URL with a regex or deleting certain elements with js. However, there is a special case where the URL doesn't have a special gif extension or anything like that, and there is no attribute labeled image, but it gets a stream of GIF content and causes the browser to display them.
I've tried many ways, and found that the loading process is only triggered in BeforeResourceLoad and GetResourceHandler events. I was thinking of doing it in GetResourceResponseFilter but it doesn't get triggered.

I have 2 questions.
1. In this case, in which event can I read out the response stream data first, and then replace it with my own if it's a GIF? CustomResponseFilter.OnFilter?
2. Right now the only way I can do this is to match URLs in BeforeResourceLoad, which is not a good way that needs the user to blacklist it for each random URL. Is there a better way to do this?

Thanks for your attention and help!

Re: Try to block GIF content(not URL) but don't know how.

Posted: Sat Feb 22, 2025 9:12 am
by salvadordf
Hi,

Try using TChromiumCore.OnResourceResponse and compare the response.MimeType value to "image/gif". Read the code comments for that event to know if CEF still allows you to stop/redirect that request.

The hardest way to block a GIF would be using a filter. The ResponseFilterBrowser demo shows how to replace an image in the TResponseFilterBrowserFrm.ReplaceLogo procedure. You can inspect the buffer to detect the GIF file header :
https://en.wikipedia.org/wiki/List_of_file_signatures

Re: Try to block GIF content(not URL) but don't know how.

Posted: Mon Feb 24, 2025 4:42 am
by nbuyer
Thanks for the reply, you are always so friendly.

I just tested it again in detail and found that it was triggering at OnResourceResponse() instead of not triggering as I said before, but the URL had changed and I didn't notice it.

So it's something that can be handled in OnResourceResponse() as you said.
Thanks!