How to get resources loaded by webpage with CefSharp

Zeokat picture Zeokat · Feb 28, 2017 · Viewed 8.4k times · Source

I saw similar questions posted and without answers. I'm new using CefSharp but i need to know if i can get all resources loaded by a webpage, for example images, fonts, etc.

The official documentation seems a little obscure about how to achieve this.

To be more clear, the scenario is quite simple. When you load a webpage with CefSharp, this webpage contains resources like images, javascript files, font files, etc. I just need to grab all this resources to explore them.

Thanks in advance for your help and sorry for my english.

Answer

VorTechS picture VorTechS · Mar 1, 2017

You're not being entirely clear about what it is you want to do with the resources.

Do you want just the list of resources? Or do you want the content of the resources?

Either way, implementing an IRequestHandler is the way to go.

        _myChromiumBrowser = new CefSharp.WinForms.ChromiumWebBrowser("http://somedomain.com")
        {
            RequestHandler = new MyRequestHandler()
        };


public class MyRequestHandler : IRequestHandler
{

    public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
    {
        return false;
    }

    public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
    {
                    // You can check the Request object for the URL Here
        return false;
    }

    public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
    {
                    // You can also check the URL here
        callback.Dispose();
        return CefReturnValue.Continue;
    }

    public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
    {
        callback.Dispose();
        return false;
    }

    public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
    {
        return false;
    }

    public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
    {
    }

    public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
    {
        return false;
    }

    public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
    {
        callback.Dispose();
        return false;
    }

    public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
    {
    }

    public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
    {
    }

    public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
    {       
                 // You can also check the request URL here
    }

    public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
    {
    }

    public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
    {
        return false;
    }


    public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
    {
        return null;
    }


    public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
    {
    }

    public bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)
    {
        callback.Dispose();
        return false;
    }

...in fact, any of the functions above which have an IRequest parameter can be used to check the URL. (The above is based on the version 55 of CEFSharp, so you will need to implement the handler according to the version you are implementing). The above is also a blank template which can be used as is in your project, leaving CEFSharp / Chromium web browser returning the appropriate content.

Apparently, you can find some source code to get the appropriate content within the CEFSharp source code, I haven't looked that deep into it (as I haven't needed to for my purposes). You might find it easier, if getting the content is what you are after, to grab the content yourself by raising the appropriate HttpRequests and reading the responses using standard methods.

https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx