我使用 cefsharp 在循环内的浏览器环境(控制台)中执行一些 JavaScript 命令。
例如,当我想运行类似Goog_AdSense_
(与“Google AdSense”应用程序相关的)命令时,cefsharp 崩溃并且循环的其余部分将不会执行。
我知道 cefsharp 使用IRequestHandler
接口调用OnRenderProcessTerminated
方法来显示错误,但想知道如何从这里开始!
如何执行和运行循环的其余部分?
class InternalBrowser
{
void CheckJS(AppSignature app, InternalBrowser browser, Scripts scripts)
{
foreach (var script in scripts)
browser.RunScript(script);
}
public bool RunScript(string script, InternalBrowser browser)
{
Task<JavascriptResponse> task = browser.EvaluateScriptAsync(script);
task.Wait();
bool result = task.Result.Success && (task.Result.Result != null);
return result;
}
}
class RequestHandler : IRequestHandler
{
public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
{
}
public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{
return false;
}
public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
{
}
public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
{
//Here is the error display
}
public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
{
return false;
}
public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
{
}
public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
var t = response.ResponseHeaders;
return false;
}
public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
return false;
}
public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
return CefReturnValue.Continue;
}
public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
return false;
}
public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
return false;
}
public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
return false;
}
}
谷歌浏览器控制台中的一个示例: