Xilium CefGlue - 如何同步获取 HTML 源代码?
Xilium CefGlue - How to get HTML source synchonously?
我想在 CEF3/Xilium CefGlue rom 非 UI 线程(离屏浏览器)中获取网页源代码
我这样做
internal class TestCefLoadHandler : CefLoadHandler
{
protected override void OnLoadStart(CefBrowser browser, CefFrame frame)
{
// A single CefBrowser instance can handle multiple requests for a single URL if there are frames (i.e. <FRAME>, <IFRAME>).
if (frame.IsMain)
{
Console.WriteLine("START: {0}", browser.GetMainFrame().Url);
}
}
protected override void OnLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode)
{
if (frame.IsMain)
{
MyCefStringVisitor mcsv = new MyCefStringVisitor();
frame.GetSource(mcsv);
Console.WriteLine("END: {0}, {1}", browser.GetMainFrame().Url, httpStatusCode);
}
}
}
class MyCefStringVisitor : CefStringVisitor
{
private string html;
protected override void Visit(string value)
{
html = value;
}
public string Html
{
get { return html; }
set { html = value; }
}
}
但是对
GetSource(...)
的调用是异步的,因此我需要等待调用发生,然后再尝试对结果执行任何操作。
我如何等待呼叫发生?
根据 Alex Maitland 的提示,我可以调整 CefSharp implementation (https://github.com/cefsharp/CefSharp/blob/master/CefSharp/TaskStringVisitor.cs) 来做类似的事情。
我这样做 :
protected override async void OnLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode)
{
if (frame.IsMain)
{
// MAIN CALL TAKES PLACE HERE
string HTMLsource = await browser.GetSourceAsync();
Console.WriteLine("END: {0}, {1}", browser.GetMainFrame().Url, httpStatusCode);
}
}
public class TaskStringVisitor : CefStringVisitor
{
private readonly TaskCompletionSource<string> taskCompletionSource;
public TaskStringVisitor()
{
taskCompletionSource = new TaskCompletionSource<string>();
}
protected override void Visit(string value)
{
taskCompletionSource.SetResult(value);
}
public Task<string> Task
{
get { return taskCompletionSource.Task; }
}
}
public static class CEFExtensions
{
public static Task<string> GetSourceAsync(this CefBrowser browser)
{
TaskStringVisitor taskStringVisitor = new TaskStringVisitor();
browser.GetMainFrame().GetSource(taskStringVisitor);
return taskStringVisitor.Task;
}
}
我想在 CEF3/Xilium CefGlue rom 非 UI 线程(离屏浏览器)中获取网页源代码
我这样做
internal class TestCefLoadHandler : CefLoadHandler
{
protected override void OnLoadStart(CefBrowser browser, CefFrame frame)
{
// A single CefBrowser instance can handle multiple requests for a single URL if there are frames (i.e. <FRAME>, <IFRAME>).
if (frame.IsMain)
{
Console.WriteLine("START: {0}", browser.GetMainFrame().Url);
}
}
protected override void OnLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode)
{
if (frame.IsMain)
{
MyCefStringVisitor mcsv = new MyCefStringVisitor();
frame.GetSource(mcsv);
Console.WriteLine("END: {0}, {1}", browser.GetMainFrame().Url, httpStatusCode);
}
}
}
class MyCefStringVisitor : CefStringVisitor
{
private string html;
protected override void Visit(string value)
{
html = value;
}
public string Html
{
get { return html; }
set { html = value; }
}
}
但是对
GetSource(...)的调用是异步的,因此我需要等待调用发生,然后再尝试对结果执行任何操作。
我如何等待呼叫发生?
根据 Alex Maitland 的提示,我可以调整 CefSharp implementation (https://github.com/cefsharp/CefSharp/blob/master/CefSharp/TaskStringVisitor.cs) 来做类似的事情。
我这样做 :
protected override async void OnLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode)
{
if (frame.IsMain)
{
// MAIN CALL TAKES PLACE HERE
string HTMLsource = await browser.GetSourceAsync();
Console.WriteLine("END: {0}, {1}", browser.GetMainFrame().Url, httpStatusCode);
}
}
public class TaskStringVisitor : CefStringVisitor
{
private readonly TaskCompletionSource<string> taskCompletionSource;
public TaskStringVisitor()
{
taskCompletionSource = new TaskCompletionSource<string>();
}
protected override void Visit(string value)
{
taskCompletionSource.SetResult(value);
}
public Task<string> Task
{
get { return taskCompletionSource.Task; }
}
}
public static class CEFExtensions
{
public static Task<string> GetSourceAsync(this CefBrowser browser)
{
TaskStringVisitor taskStringVisitor = new TaskStringVisitor();
browser.GetMainFrame().GetSource(taskStringVisitor);
return taskStringVisitor.Task;
}
}