原文: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需要在函数名后面加上括号的如果没有参数就不能加括号。

cefsharp 与webbrowser简单对比概述的更多相关文章

  1. MongoDB中insert方法、update方法、save方法简单对比

    MongoDB中insert方法.update方法.save方法简单对比 1.update方法 该方法用于更新数据,是对文档中的数据进行更新,改变则更新,没改变则不变. 2.insert方法 该方法用 ...

  2. .NET轻量级MVC框架:Nancy入门教程(二)——Nancy和MVC的简单对比

    在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy的优势在哪里?和微软的MVC比 ...

  3. HTTPS, SPDY和 HTTP/2性能的简单对比

    中文原文:HTTPS, SPDY和 HTTP/2性能的简单对比 整理自:A Simple Performance Comparison of HTTPS, SPDY and HTTP/2 请尊重版权, ...

  4. 【转贴】Cortex系列M0-4简单对比

    转载网址:http://blog.sina.com.cn/s/blog_7dbd9c0e01018e4l.html 最近搞了块ST的Cortex-M4处理器,然后下了本文档.分享一下. 针对目前进入大 ...

  5. 简单对比Spark和Storm

    2013年参与开发了一个类似storm的自研系统, 2014年使用过spark 4个多月,对这两个系统都有一些了解. 下面是我关于这两个系统的简单对比: Spark: 1. 基于数据并行,https: ...

  6. Nancy和MVC的简单对比

    Nancy和MVC的简单对比 在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy ...

  7. [评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)

    [评测]低配环境下,PostgresQL和Mysql读写性能简单对比 原文链接:https://www.cnblogs.com/blog5277/p/10658426.html 原文作者:博客园--曲 ...

  8. 百度 OCR API 的使用以及与 Tesseract 的简单对比

    目录 百度 OCR API 初探 用 Python 调用百度 OCR API 与 Tesseract 的简单对比 百度 OCR API 初探 近日得知百度在其 APIStore 上开放了 OCR 的 ...

  9. Rx与Async Task的简单对比

    有关Reactive Extensions的介绍可见https://rx.codeplex.com/,总的来说,你可以当它是又一个异步编程的框架,它以观察者模式实现了对数据流的的“订阅”.一个列表,一 ...

随机推荐

  1. 定位导致物化视图无法快速刷新的原因 分类: H2_ORACLE 2013-08-08 23:04 335人阅读 评论(0) 收藏

    转载自:http://yangtingkun.itpub.net/post/468/13318 物化视图的快速刷新采用了增量的机制,在刷新时,只针对基表上发生变化的数据进行刷新.因此快速刷新是物化视图 ...

  2. ConcurrentHashMap 内部实现分析

    ConcurrentHashMap ConcurrentHashMap是一个线程安全的Hash Table,它的主要功能是提供了一组和HashTable功能相同但是线程安全的方法.Concurrent ...

  3. php实现求扑克牌顺子(*****)(AC)(分类:把问题分小,利于排错)

    php实现求扑克牌顺子(*****)(AC)(分类:把问题分小,利于排错) 一.总结 分类(那就可以把问题分小而逐步完成每个板块,这样是很简单的) 分类还有助于查错 二.php实现求扑克牌顺子 题目描 ...

  4. HTML Email 编写指南(转)

      作者: 阮一峰 日期: 2013年6月16日 今天,我想写一个"低技术"问题. 话说我订阅了不少了新闻邮件(Newsletter),比如JavaScript Weekly.每周 ...

  5. QueryRunner类常用的方法

    public Object query(Connection conn, String sql, Object[] params, ResultSetHandler rsh) throws SQLEx ...

  6. 【u032】均衡发展

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 神牛小R在许多方面都有着很强的能力,具体的说,他总共有m种能力,并将这些能力编号为1到m.他的能力是一 ...

  7. 阿里云centos 6.5 32位安装可视化界面的方法

    http://www.dzbfsj.com/forum.php?mod=viewthread&tid=2702 http://www.mayanpeng.cn/?p=507 http://bl ...

  8. VO对象通过groovy模板映射XML文件

    介绍 之前写过JAVA+XSLT相关的技术博客,近期研究了一个开源工具包org.codehaus.groovy,处理VO对象和XML文件映射很方便. 简言之:将VO对象中的属性(包含Collectio ...

  9. HDU2473 Junk-Mail Filter - 并查集删除操作(虚父节点)

    传送门 题意: 每次合并两份邮件,或者将某一份邮件独立出来,问最后有多少个邮件集合. 分析: 考虑初始化每个节点的祖先为一个虚父节点(i + n),虚父节点指向它自己.这样可以进行正常的合并操作. 而 ...

  10. C#反射应用

    考虑这个是因为返回的是对象集合,需要把对象集合绑定到datagridview上,绑定datagridview需要数据源,组装数据的话,用datatable添加列很麻烦,所以用反射来实现,估计可能会有多 ...