/// <summary> /// 监听网页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void FrameLoadEnd(object sender, FrameLoadEndEventArgs e) { CefSharp.Cef.GetGlobalCookieManager().VisitAllCookies(visitor);//记录cookie if (this.context.CurrentIndex < this.context.Actions.Count) { ActionInfo action = this.context.Actions[this.context.CurrentIndex]; Regex regResultPageUrl = new Regex(action.result_page_url_regex);//结果页面的正则表达式 if (this.context.Searching && e.Frame.Url.Equals(action.search_page_url)) { //进入到了查询页面 this.Log($"正在执行[{action.name}]的查询函数"); string script = ActionLoad.GetJavaScript(action); //注入配置的javascript script += "greatzee_execute_search();\r\n"; //执行查询函数 e.Frame.ExecuteJavaScriptAsync(script); this.context.Searching = false; } else if (regResultPageUrl.IsMatch(e.Frame.Url)) { //进入到了结果页面 string script = ActionLoad.GetJavaScript(action); //注入配置的javascript string ticket = Guid.NewGuid().ToString(); script += "ScriptBrige.callRegisterParameter(JSON.stringify($RegisterParameter),'" + ticket + "');\r\n"; //通过ScriptBrige.CallRegisterParameters将$RegisterParameter传递到后台 e.Frame.ExecuteJavaScriptAsync(script); this.scriptBrige.WaitTicket(ticket); //等待ScriptBrige.callRegisterParameter执行完 //将参数和Cookie拼凑在html后面,并将html页面发送给负责存储的应用程序 var html = await e.Frame.GetSourceAsync(); this.Log($"已查询到[{action.name}]的结果"); StringBuilder message = new StringBuilder(); message.AppendLine(html); message.AppendLine("\r\n"); message.AppendLine("<script name='greatzee_parameter'><![CDATA[$RegisterParameter=" + this.scriptBrige.Parameters + ";]]></script>"); message.AppendLine("<script name='greatzee_cookie'><![CDATA[$Cookie=" + new JavaScriptSerializer().Serialize(this.visitor.Cookies) + "]]></script>"); Encoding encoding = Encoding.GetEncoding(action.charset); byte[] htmlBytes = encoding.GetBytes(message.ToString()); HtmlStorage storage = new HtmlStorage(); storage.ExecuteStorage(action.storage_socket, action.storage_api, action.storage_local_directory, htmlBytes); //浏览器执行配置的greatzee_has_next_page函数 ticket = Guid.NewGuid().ToString(); script = "ScriptBrige.callHasNextPage(greatzee_has_next_page().toString(),'" + ticket + "');\r\n"; //执行greatzee_has_next_page函数 this.browser.ExecuteScriptAsync(script); this.scriptBrige.WaitTicket(ticket); //等待ScriptBrige.callHasNextPage执行完 if (this.scriptBrige.HasNextPage) { //执行分页 this.Log($"正在执行[{action.name}]的下一页"); Thread.Sleep(this.context.ActionInterval); this.browser.ExecuteScriptAsync("greatzee_goto_next_page();"); } else { this.context.Searching = true; if (this.context.CurrentIndex < this.context.Actions.Count - 1) { //执行下一个动作 this.context.CurrentIndex++; if (this.context.CurrentIndex < this.context.Actions.Count) { Thread.Sleep(this.context.ActionInterval); this.browser.Load(this.context.Actions[this.context.CurrentIndex].search_page_url); } } else { //回到第一个action重新执行 if (this.context.ExecuteInterval >= 0) { this.Log("所有动作都已执行完,休眠中"); Thread.Sleep(this.context.ExecuteInterval); this.context.CurrentIndex = 0; this.browser.Load(this.context.Actions[0].search_page_url); } } } } } }