CefSharp禁止弹出新窗体,在同一窗口打开链接,或者在新Tab页打开链接,并且支持带type="POST" target="_blank"的链接
2021-01-25 06:12
标签:eth def import database eve send geturl rect oca
说明:在同一窗口打开链接,只要稍加改造就可以实现,这里实现的是在新Tab页打开链接,并且支持带type="POST" target="_blank"的链接 github和bitbucket上相关问题: 1、WPF empty POST data when using custom popup https://github.com/cefsharp/CefSharp/issues/1267 2、CefLifeSpanHandler, customized OnBeforePopup problem https://bitbucket.org/chromiumembedded/cef/issues/1949/ 解决(CefSharp版本75.1.143.0): 一、实现IRequestHandler接口 二、实现ILifeSpanHandler接口 三、扩展ChromiumWebBrowser 四、封装ExtChromiumBrowser(BrowserCtrl控件) 五、MainWindow测试代码 六、测试html代码post.html 七、测试后台代码 八、测试前台cshtml代码 九:关键代码段: 1、RequestHandler类中获取并保存PostData 2、CefLifeSpanHandler类中创建IRequest 说明:OnBeforePopup方法要return false,用BrowserPopupWin和windowInfo.SetAsChild方法弹出一个不可见的窗体,这样才能拿到PostData 3、在BrowserCtrl控件中用LoadRequest方法打开新的URL,并把post数据带过去 十、效果图: 完整代码下载:https://files-cdn.cnblogs.com/files/s0611163/CefSharpDemo.zip 源码说明:为了减少源码压缩包的大小,代码中没有依赖的CefSharp文件,请自己下载(使用x86版本),用于测试的网页后台代码也没有,请自己制作测试后台 CefSharp禁止弹出新窗体,在同一窗口打开链接,或者在新Tab页打开链接,并且支持带type="POST" target="_blank"的链接 标签:eth def import database eve send geturl rect oca 原文地址:https://www.cnblogs.com/lonelyxmas/p/12034569.htmlusing CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
namespace CefSharpDemo
{
public class RequestHandler : IRequestHandler
{
private ExtChromiumBrowser _browser;
public RequestHandler(ExtChromiumBrowser browser)
{
_browser = browser;
}
public bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
return false;
}
public IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
if (request.Method.ToUpper() == "POST" && request.PostData != null)
{
if (request.PostData.Elements.Count > 0)
{
_browser.PostData = new byte[request.PostData.Elements[0].Bytes.Length];
request.PostData.Elements[0].Bytes.CopyTo(_browser.PostData, 0);
}
}
return null;
}
public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
return false;
}
public bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
return false;
}
public bool OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
return false;
}
public void OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, string pluginPath)
{
}
public bool OnQuotaRequest(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
return false;
}
public void OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status)
{
}
public void OnRenderViewReady(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public bool OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)
{
return false;
}
}
}
using CefSharp;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Utils;
namespace CefSharpDemo
{
public class CefLifeSpanHandler : CefSharp.ILifeSpanHandler
{
private static LimitedTaskScheduler _scheduler = new LimitedTaskScheduler(2);
public CefLifeSpanHandler()
{
}
public bool DoClose(IWebBrowser browserControl, CefSharp.IBrowser browser)
{
if (browser.IsDisposed || browser.IsPopup)
{
return false;
}
return true;
}
public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
{
}
public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
{
}
public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
var chromiumWebBrowser = (ExtChromiumBrowser)browserControl;
chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
{
BrowserPopupWin win = new BrowserPopupWin();
win.ShowInTaskbar = false;
win.Height = 0;
win.Width = 0;
win.Show();
IntPtr handle = new WindowInteropHelper(win).Handle;
windowInfo.SetAsChild(handle);
_scheduler.Run(() =>
{
WaitUtil.Wait(() => chromiumWebBrowser.PostData);
IRequest request = null;
if (chromiumWebBrowser.PostData != null)
{
request = frame.CreateRequest();
request.Url = targetUrl;
request.Method = "POST";
request.InitializePostData();
var element = request.PostData.CreatePostDataElement();
element.Bytes = chromiumWebBrowser.PostData;
request.PostData.AddElement(element);
chromiumWebBrowser.PostData = null;
}
chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
{
NewWindowEventArgs e = new NewWindowEventArgs(targetUrl, request);
chromiumWebBrowser.OnNewWindow(e);
}));
chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
{
win.Close();
}));
});
}));
newBrowser = null;
return false;
}
}
}
using CefSharp.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CefSharpDemo
{
public class ExtChromiumBrowser : ChromiumWebBrowser
{
public byte[] PostData { get; set; }
public ExtChromiumBrowser()
: base()
{
this.LifeSpanHandler = new CefLifeSpanHandler();
this.DownloadHandler = new DownloadHandler(this);
this.MenuHandler = new MenuHandler();
this.KeyboardHandler = new KeyboardHandler();
this.RequestHandler = new RequestHandler(this);
}
public event EventHandler
using CefSharp;
using CefSharp.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Utils;
namespace CefSharpDemo
{
///
using CefSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Utils;
namespace CefSharpDemo
{
///
DOCTYPE html>
html>
head>
title>CefSharpDemotitle>
meta charset="utf-8" />
meta http-equiv="X-UA-Compatible" content="IE=edge">
meta name="viewport" content="width=device-width, initial-scale=1">
style type="text/css">
style>
script type="text/javascript">
script>
head>
body>
form method="post" action="http://localhost:1209/netcms/" target="_blank">
span>name:span>input type="text" name="name" value="测试名称" />
span>code:span>input type="text" name="code" value="测试编码" />
button type="submit">Post提交button>
form>
body>
html>
public ActionResult index()
{
string name = Request.Params["name"];
string code = Request.Params["code"];
ViewBag.name = name;
ViewBag.code = code;
return View();
}
@using Models;
@{
Layout = "~/Views/Shared/_SiteLayout.cshtml";
}
div style="font-size:50px; height:1200px;">
span>name:span>span>@ViewBag.namespan>br />span>code:span>span>@ViewBag.codespan>
div>
public IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
if (request.Method.ToUpper() == "POST" && request.PostData != null)
{
if (request.PostData.Elements.Count > 0)
{
_browser.PostData = new byte[request.PostData.Elements[0].Bytes.Length];
request.PostData.Elements[0].Bytes.CopyTo(_browser.PostData, 0);
}
}
return null;
}
public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
var chromiumWebBrowser = (ExtChromiumBrowser)browserControl;
chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
{
BrowserPopupWin win = new BrowserPopupWin();
win.ShowInTaskbar = false;
win.Height = 0;
win.Width = 0;
win.Show();
IntPtr handle = new WindowInteropHelper(win).Handle;
windowInfo.SetAsChild(handle);
_scheduler.Run(() =>
{
WaitUtil.Wait(() => chromiumWebBrowser.PostData);
IRequest request = null;
if (chromiumWebBrowser.PostData != null)
{
request = frame.CreateRequest();
request.Url = targetUrl;
request.Method = "POST";
request.InitializePostData();
var element = request.PostData.CreatePostDataElement();
element.Bytes = chromiumWebBrowser.PostData;
request.PostData.AddElement(element);
chromiumWebBrowser.PostData = null;
}
chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
{
NewWindowEventArgs e = new NewWindowEventArgs(targetUrl, request);
chromiumWebBrowser.OnNewWindow(e);
}));
chromiumWebBrowser.Dispatcher.Invoke(new Action(() =>
{
win.Close();
}));
});
}));
newBrowser = null;
return false;
}
if (Request == null)
{
_browser.Load(Url);
}
else
{
_browser.Load(Url);
_browser.GetMainFrame().LoadRequest(Request);
Request = null;
}
文章标题:CefSharp禁止弹出新窗体,在同一窗口打开链接,或者在新Tab页打开链接,并且支持带type="POST" target="_blank"的链接
文章链接:http://soscw.com/index.php/essay/46664.html