您现在的位置是:首页 > 文章详情

cefsharp 与webbrowser简单对比概述

日期:2018-09-10点击:443
原文: cefsharp 与webbrowser简单对比概述

有个项目需要做个简单浏览器,从网上了解到几个相关的组件有winform自带的IE内核的WebBrowser,有第三方组件谷歌内核的webkit、cefsharp、chromiumfx等。

一开始是用的webbrowser 后来发现有些css样式是不兼容的于是又开始研究webkit这个有些css样式同样不能用,然后才开始研究cefsharp,从网上下载了57版本的但总是运行不起来后来发现装上vs中C++公共工具才行(注意即使装上vcredist_x86这个运行库也是不行),这个你不能要求人家客户装vs啊,后来也没找到到底是哪个动态库的问题。之后就下载了47版本。

按键事件

   不同于webbrowser通过事件PreviewKeyDown捕获键盘按键,cefsharp是通过实现接口IKeyboardHandler赋值委托KeyboardHandler来实现的

 web.KeyboardHandler = new CefKeyboardHandler(m_cell as CellControl);
 public class CefKeyboardHandler : IKeyboardHandler { private CellControl m_cell; /// <summary> /// 命名代理 /// </summary> /// <param name="con"></param> /// <param name="text"></param> private delegate void PreKeyEventHandler(object sender,object webBrowser,int windowsKeyCode); public CefKeyboardHandler(CellControl cell) { m_cell = cell; } public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey) { return true; } public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut) { m_cell.Invoke(new PreKeyEventHandler(PreKeyEvent),m_cell, browserControl, windowsKeyCode); return true; } private void PreKeyEvent(object sender, object webBrowser, int windowsKeyCode) { CellControl cell = sender as CellControl; cell.ControlPreviewKeyDown(webBrowser , new System.Windows.Forms.PreviewKeyDownEventArgs((Keys)windowsKeyCode)); if (windowsKeyCode == (int)(Keys.Delete)) { cell.Controls.Clear(); } }



代理设置

   公司网络是需要代理才能上外网,webbrowser可以自动获取已经设置好的代理,cefsharp网上说可以通过setting.CefCommandLineArgs.Add("--proxy-server""http://127.0.0.1:8877");这个方式设置代理,可是不知道为啥我这边一直不行,后来发现一个通过实现接口IRequestHandler赋值委托RequestHandler来设置代理可行

 webBrowser.RequestHandler = new CefRequest();
 public class CefRequest : IRequestHandler { // // 摘要: // Called when the browser needs credentials from the user. // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object // // frame: // The frame object that needs credentials (This will contain the URL that is being // requested.) // // isProxy: // indicates whether the host is a proxy server // // host: // hostname // // port: // port number // // realm: // realm // // scheme: // scheme // // callback: // Callback interface used for asynchronous continuation of authentication requests. // // 返回结果: // Return true to continue the request and call CefAuthCallback::Continue() when // the authentication information is available. Return false to cancel the request. public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) { if(isProxy) { if (string.IsNullOrWhiteSpace(ProxyThornInfo.Name)) { ProxyThornInfo.InitInfo(ProxyThornInfo.DefaultFile); } } //callback.Cancel(); callback.Continue(ProxyThornInfo.Name, ProxyThornInfo.PassWord); return true; } // // 摘要: // Called before browser navigation. If the navigation is allowed CefSharp.IWebBrowser.FrameLoadStart // and CefSharp.IWebBrowser.FrameLoadEnd will be called. If the navigation is canceled // CefSharp.IWebBrowser.LoadError will be called with an ErrorCode value of CefSharp.CefErrorCode.Aborted. // // 参数: // browserControl: // the ChromiumWebBrowser control // // browser: // the browser object // // frame: // The frame the request is coming from // // request: // the request object - cannot be modified in this callback // // isRedirect: // has the request been redirected // // 返回结果: // Return true to cancel the navigation or false to allow the navigation to proceed. public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect) { return false; } // // 摘要: // Called before a resource request is loaded. For async processing return CefSharp.CefReturnValue.ContinueAsync // and execute CefSharp.IRequestCallback.Continue(System.Boolean) or CefSharp.IRequestCallback.Cancel // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object // // frame: // The frame object // // request: // the request object - can be modified in this callback. // // callback: // Callback interface used for asynchronous continuation of url requests. // // 返回结果: // To cancel loading of the resource return CefSharp.CefReturnValue.Cancel or CefSharp.CefReturnValue.Continue // to allow the resource to load normally. For async return CefSharp.CefReturnValue.ContinueAsync public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { return CefReturnValue.Continue; } // // 摘要: // Called on the CEF IO thread when a resource response is received. To allow the // resource to load normally return false. To redirect or retry the resource modify // request (url, headers or post body) and return true. The response object cannot // be modified in this callback. // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object // // frame: // The frame that is being redirected. // // request: // the request object // // response: // the response object - cannot be modified in this callback // // 返回结果: // To allow the resource to load normally return false. To redirect or retry the // resource modify request (url, headers or post body) and return true. public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { return false; } // // 摘要: // Called to handle requests for URLs with an invalid SSL certificate. Return true // and call CefSharp.IRequestCallback.Continue(System.Boolean) either in this method // or at a later time to continue or cancel the request. If CefSettings.IgnoreCertificateErrors // is set all invalid certificates will be accepted without calling this method. // // 参数: // browserControl: // the ChromiumWebBrowser control // // browser: // the browser object // // errorCode: // the error code for this invalid certificate // // requestUrl: // the url of the request for the invalid certificate // // sslInfo: // ssl certificate information // // callback: // Callback interface used for asynchronous continuation of url requests. If empty // the error cannot be recovered from and the request will be canceled automatically. // // 返回结果: // Return false to cancel the request immediately. Return true and use CefSharp.IRequestCallback // to execute in an async fashion. public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback) { return true; } // // 摘要: // Called on the UI thread before OnBeforeBrowse in certain limited cases where // navigating a new or different browser might be desirable. This includes user-initiated // navigation that might open in a special way (e.g. links clicked via middle-click // or ctrl + left-click) and certain types of cross-origin navigation initiated // from the renderer process (e.g. navigating the top-level frame to/from a file // URL). // // 参数: // browserControl: // the ChromiumWebBrowser control // // browser: // the browser object // // frame: // The frame object // // targetUrl: // target url // // targetDisposition: // The value indicates where the user intended to navigate the browser based on // standard Chromium behaviors (e.g. current tab, new tab, etc). // // userGesture: // The value will be true if the browser navigated via explicit user gesture (e.g. // clicking a link) or false if it navigated automatically (e.g. via the DomContentLoaded // event). // // 返回结果: // Return true to cancel the navigation or false to allow the navigation to proceed // in the source browser's top-level frame. public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture) { return false; } // // 摘要: // Called when a plugin has crashed // // 参数: // browserControl: // the ChromiumWebBrowser control // // browser: // the browser object // // pluginPath: // path of the plugin that crashed public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) { } // // 摘要: // Called on the UI thread to handle requests for URLs with an unknown protocol // component. SECURITY WARNING: YOU SHOULD USE THIS METHOD TO ENFORCE RESTRICTIONS // BASED ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION. // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object // // url: // the request url // // 返回结果: // return to true to attempt execution via the registered OS protocol handler, if // any. Otherwise return false. public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) { return true; } // // 摘要: // Called when JavaScript requests a specific storage quota size via the webkitStorageInfo.requestQuota // function. For async processing return true and execute CefSharp.IRequestCallback.Continue(System.Boolean) // at a later time to grant or deny the request or CefSharp.IRequestCallback.Cancel // to cancel. // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object // // originUrl: // the origin of the page making the request // // newSize: // is the requested quota size in bytes // // callback: // Callback interface used for asynchronous continuation of url requests. // // 返回结果: // Return false to cancel the request immediately. Return true to continue the request // and call CefSharp.IRequestCallback.Continue(System.Boolean) either in this method // or at a later time to grant or deny the request. public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback) { return true; } // // 摘要: // Called when the render process terminates unexpectedly. // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object // // status: // indicates how the process terminated. public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) { } // // 摘要: // Called on the browser process UI thread when the render view associated with // browser is ready to receive/handle IPC messages in the render process. // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) { } // // 摘要: // Called on the CEF IO thread when a resource load has completed. // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object // // frame: // The frame that is being redirected. // // request: // the request object - cannot be modified in this callback // // response: // the response object - cannot be modified in this callback // // status: // indicates the load completion status // // receivedContentLength: // is the number of response bytes actually read. public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { } // // 摘要: // Called on the IO thread when a resource load is redirected. The CefSharp.IRequest.Url // parameter will contain the old URL and other request-related information. // // 参数: // browserControl: // The ChromiumWebBrowser control // // browser: // the browser object // // frame: // The frame that is being redirected. // // request: // the request object - cannot be modified in this callback // // newUrl: // the new URL and can be changed if desired public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl) { }
其他的ChromiumWebBrowser.Handler同上

JavaScript互调

    在webbrowser中通过设置  

[System.Runtime.InteropServices.ComVisible(true)]

public class JavaScripAction

webBrowser1.ObjectForScripting = new JavaScripAction();
就可在javascrip中通过window.external来调用C#类JavaScriptAction中的方法,在C#中通过来调用javascrip中的函数
web.RegisterJsObject("objbine", java, false);这里的objbine是我们自己起的这个注册的名字实体,注意不能和java中特殊含义的文字重复例如external否则没有效果

 

webBrowser1.Document.InvokeScript("Messageaa", objects);

在cefsharp中是通过注册来实现java与C#类的绑定的

JavaScripAction java = new JavaScripAction(dataSvc, bbShow, pdpcShowBb);

这样在javascrip中可以通过

window.objbine.InvokeFunc('0','1','cunction','1,2,3');

这种方式来调用C#类JavaScriptaction中的方法InvokFunc方法了,需要注意的是如果

在C#中调用JavaScript中的函数如下

web.ExecuteScriptAsync(string methodname,params object[] args)

需要注意的是如果JavaScript中的函数名是没有参数的这里的methodname需要在函数名后面加上括号的如果没有参数就不能加括号。







原文链接:https://yq.aliyun.com/articles/677459
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章