1

我对 c# 和 CefSharp 有点陌生 :-) 所以对我来说很酷!

我的目标是调用一个 url,然后探索 DOM 以检索数据,然后处理这些数据以用于管理目的。我已经实现了该检索,并且我的 js 代码使用 EvaluateScriptAsync 运行良好。

现在我想跟踪数据刷新。页面由 websocket 通信更新,然后更新 DOM 对象模型中的值。

我认为使用 RequestHandler 并调整 GetResourceResponseFilter;但我无法做到这一点,似乎我实施的方法从未被调用......我使用从cefsharp项目站点下载的最小解决方案。我只是在这段代码的末尾添加了两行:

 public BrowserForm()
        {
            InitializeComponent();

            Text = "CefSharp";
            WindowState = FormWindowState.Maximized;

            browser = new ChromiumWebBrowser("www.google.com");
            toolStripContainer.ContentPanel.Controls.Add(browser);

            browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
            browser.LoadingStateChanged += OnLoadingStateChanged;
            browser.ConsoleMessage += OnBrowserConsoleMessage;
            browser.StatusMessage += OnBrowserStatusMessage;
            browser.TitleChanged += OnBrowserTitleChanged;
            browser.AddressChanged += OnBrowserAddressChanged;
            // Add this  for request handler
            Handler.RequestHandler requestHandler = new MyBasicRequestHandler();
            browser.RequestHandler = requestHandler;

我的测试处理程序看起来像这样!

public class MyBasicRequestHandler : CefSharp.Handler.RequestHandler
        {
            public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
            {
                MessageBox.Show ("OnResourceResponse");
                return false;
            }
            public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
            {
                MessageBox.Show("OnProtocolExecution : "+url);
                return false;
            }
            public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
            {
                MessageBox.Show("GetResourceResponseFilter : " + request.ReferrerUrl);
                return null;
            }
        }

但是什么都没有被解雇!

任何帮助或建议将不胜感激。

谢谢!

4

1 回答 1

1

感谢 amaitland 的友好评论,此代码有效。我只向从 github 提取的 CefSharp.Minimal Example.Winforms 解决方案添加了几行来注册 RequestHandler。

public BrowserForm()
        {
            InitializeComponent();

            Text = "CefSharp";
            WindowState = FormWindowState.Maximized;

            browser = new ChromiumWebBrowser("https://cefsharp.github.io/");
            toolStripContainer.ContentPanel.Controls.Add(browser);

            browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
            browser.LoadingStateChanged += OnLoadingStateChanged;
            browser.ConsoleMessage += OnBrowserConsoleMessage;
            browser.StatusMessage += OnBrowserStatusMessage;
            browser.TitleChanged += OnBrowserTitleChanged;
            browser.AddressChanged += OnBrowserAddressChanged;
            // Add this  for request handler
            Handler.RequestHandler requestHandler = new MyBasicRequestHandler();
            browser.RequestHandler = requestHandler;

            var version = string.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
               Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);

            var bitness = Environment.Is64BitProcess ? "x64" : "x86";
            var environment = String.Format("Environment: {0}", bitness);

            DisplayOutput(string.Format("{0}, {1}", version, environment));
        }

然后我为 MyBasicRequestHandler 和 MyCustomResourceRequestHandler 类添加代码如下:

public class MyCustomResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler
        {
            private readonly System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

            protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
            {
                return new CefSharp.ResponseFilter.StreamResponseFilter(memoryStream);
            }

            protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
            {
                //You can now get the data from the stream
                var bytes = memoryStream.ToArray();

                if (response.Charset == "utf-8")
                {
                    var str = System.Text.Encoding.UTF8.GetString(bytes);
                    Console.WriteLine("In OnResourceLoadComplete : " + str.Substring(0,10) + " <...>");
                }
                else
                {
                    //Deal with different encoding here
                }
            }
        }

        public class MyBasicRequestHandler : CefSharp.Handler.RequestHandler
        {
            protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
            {
                Console.WriteLine("In GetResourceRequestHandler : " + request.Url);
                //Only intercept specific Url's
                if (request.Url == "http://cefsharp.github.io/" || request.Url == "https://cefsharp.github.io/")
                {
                    return new MyCustomResourceRequestHandler();
                }
                //Default behaviour, url will be loaded normally.
                return null;
            }

        }

启动应用程序并很高兴在 VisualStudio 控制台输出中看到这一点:

In GetResourceRequestHandler : https://cefsharp.github.io/
In OnResourceLoadComplete : <!DOCTYPE  <...>
In GetResourceRequestHandler : https://fonts.googleapis.com/css?family=Ubuntu:400,500
In GetResourceRequestHandler : https://cefsharp.github.io/stylesheets/stylesheet.css
In GetResourceRequestHandler : https://cefsharp.github.io/stylesheets/github-light.css
In GetResourceRequestHandler : https://sidecar.gitter.im/dist/sidecar.v1.js
In GetResourceRequestHandler : https://cefsharp.github.io/images/logo2.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram1.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram2.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram3.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram4.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram5.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram7.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram6.png
In GetResourceRequestHandler : https://cefsharp.github.io/stylesheets/print.css
In GetResourceRequestHandler : https://cefsharp.github.io/images/top-header.png
In GetResourceRequestHandler : https://fonts.gstatic.com/s/ubuntu/v14/4iCs6KVjbNBYlgoKfw72.woff2
In GetResourceRequestHandler : https://fonts.gstatic.com/s/ubuntu/v14/4iCv6KVjbNBYlgoCjC3jsGyN.woff2

所以我的问题解决了!再次感谢亲爱的amaitland ;-)

于 2020-03-24T23:09:48.207 回答