最近要写一个浏览器包裹一个网站,试了各种浏览器插件,记录一下。
第一个就是微软的WebBrowser,这个很容易,直接拖过来,然后写一下注册表调用IE11的内核显示,这个代码是抄的:
///
/// 修改注册表信息来兼容当前程序
///
///
static void SetWebBrowserFeatures(int ieVersion)
{
// don't change the registry if running in-proc inside Visual Studio
if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
return;
//获取程序及名称
var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
//得到浏览器的模式的值
UInt32 ieMode = GeoEmulationModee(ieVersion);
var featureControlRegKey = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\";
//设置浏览器对应用程序(appName)以什么模式(ieMode)运行
Registry.SetValue(featureControlRegKey + "FEATURE_BROWSER_EMULATION",
appName, ieMode, RegistryValueKind.DWord);
//为完整的Internet Explorer浏览器启用“打开”的功能
Registry.SetValue(featureControlRegKey + "FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION",
appName, 1, RegistryValueKind.DWord);
Registry.SetValue(featureControlRegKey + "FEATURE_AJAX_CONNECTIONEVENTS",
appName, 1, RegistryValueKind.DWord);
Registry.SetValue(featureControlRegKey + "FEATURE_GPU_RENDERING",
appName, 1, RegistryValueKind.DWord);
Registry.SetValue(featureControlRegKey + "FEATURE_WEBOC_DOCUMENT_ZOOM",
appName, 1, RegistryValueKind.DWord);
Registry.SetValue(featureControlRegKey + "FEATURE_NINPUT_LEGACYMODE",
appName, 0, RegistryValueKind.DWord);
}
///
/// 获取浏览器的版本
///
///
static int GetBrowserVersion()
{
int browserVersion = 0;
using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
RegistryKeyPermissionCheck.ReadSubTree,
System.Security.AccessControl.RegistryRights.QueryValues))
{
var version = ieKey.GetValue("svcVersion");
if (null == version)
{
version = ieKey.GetValue("Version");
if (null == version)
throw new ApplicationException("Microsoft Internet Explorer is required!");
}
int.TryParse(version.ToString().Split('.')[0], out browserVersion);
}
//如果小于7
if (browserVersion < 7)
{
throw new ApplicationException("不支持的浏览器版本!");
}
return browserVersion;
}
///
/// 通过版本得到浏览器模式的值
///
///
///
static UInt32 GeoEmulationModee(int browserVersion)
{
UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode.
switch (browserVersion)
{
case 7:
mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
break;
case 8:
mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
break;
case 9:
mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
break;
case 10:
mode = 10000; // Internet Explorer 10.
break;
case 11:
mode = 11000; // Internet Explorer 11
break;
}
return mode;
}
然后在Form的Load里面写:
SetWebBrowserFeatures(11);//内核为11
就OK了,然后就是处理新窗口的问题了,这个有两种情况,一个是网页内容的新窗口,主要是target的处理,添加事件处理函数:
/防止在新窗口打开链接
private void browser_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
//替换掉网页中链接的新窗口属性
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//this.Text = this.browser.Document.Title;
foreach (HtmlElement links in this.browser.Document.Links)
{
links.SetAttribute("target", "_self");
}
foreach (HtmlElement form in this.browser.Document.Forms)
{
form.SetAttribute("target", "_self");
}
}
还有一种是js的window.open,这个这样处理:在Form_Load里面写
string jsHtml= "window.open=function(url, title, prop){window.external.jumpUrl(url);}";
mshtml.IHTMLDocument2 doc = browser.Document.DomDocument as mshtml.IHTMLDocument2;
mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2;
win.execScript(jsHtml, "javascript");
需要引用Microsoft.mshtml,其中jumpUrl是程序自身的一个函数,处理传递过来的 Url,通常是直接load一下:
public void jumpUrl(string url)
{
browser.Navigate(url);
}
要从js里面调用函数,需要引用:
using System.Runtime.InteropServices;
然后在类前面写:
[System.Runtime.InteropServices.ComVisible(true)]
public partial class mainForm : Form
{
.......
然后就是各种加载导航操作什么的,这个没什么好说的。
一些坑:
1、这个对js的支持非常弱,尤其是用了jquery的操作,不确定性太多;
2、虽然用了IE内核,但并不是全部支持IE的功能,感觉也就是60~70的样子;
3、对客户机的环境依赖太多,需要装IE的高版本,FrameWorks、VS支持什么的。
总之,不推荐包装复杂的站点,否则就是噩梦。