29Wx状态初始化

This commit is contained in:
wule committed 2026-06-11 15:17:50 +08:00
1 parent 9fd5579fcd
commit 40c702883c
322 files changed
+155553

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
using AllBase;
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace _29WxOrder.Helper
{
public class CEFHelper
{
// 鼠标左键按下,移动,移动...左键抬起。
public static void CefMouseDrag(ChromiumWebBrowser webBrowser, int x1, int y1, int x2, int y2)
{
Thread.Sleep(100);
var host = webBrowser.GetBrowser().GetHost();
var rx = -6 + Randomx.GetId(13);
x2 = x2 + rx;
// 随机按下点的位置(正负6像素),提高仿真性
int x = x1 + rx;
int y = y1 - 6 + Randomx.GetId(13);
// 按下鼠标左键
host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.None);
Thread.Sleep(100 + Randomx.GetId(300));
// 滑动光标
int times = -20;
while (x < x2 + 4 - Randomx.GetId(9))
{
times++;
x += (25 + Randomx.GetId(15)) / 8;
if (times % 5 == 0)
y = y - 1 + Randomx.GetId(3);
Thread.Sleep(6 + Math.Abs(times) / 5 + Randomx.GetId(16));
host.SendMouseMoveEvent((int)x, (int)y, false, CefEventFlags.LeftMouseButton);
}
if (x < x2 - 1)
while (x < x2)
{
times++;
x += (8 + Randomx.GetId(8)) / 8;
if (times % 5 == 0)
y = y - 1 + Randomx.GetId(3);
Thread.Sleep(6 + Math.Abs(times) / 5 + Randomx.GetId(16));
host.SendMouseMoveEvent((int)x, (int)y, false, CefEventFlags.LeftMouseButton);
}
else if (x > x2 + 1)
while (x > x2)
{
times++;
x -= (8 + Randomx.GetId(8)) / 8;
if (times % 5 == 0)
y = y - 1 + Randomx.GetId(3);
Thread.Sleep(6 + Math.Abs(times) / 5 + Randomx.GetId(16));
host.SendMouseMoveEvent((int)x, (int)y, false, CefEventFlags.LeftMouseButton);
}
// 如果最后位置偏差较多,再做一次调整
x = x2 - 1 + Randomx.GetId(3);
x = x2;
Thread.Sleep(18 + Math.Abs(times) / 5 + Randomx.GetId(16));
Thread.Sleep(18 + Math.Abs(times) / 5 + Randomx.GetId(16));
host.SendMouseMoveEvent((int)x, (int)y, false, CefEventFlags.LeftMouseButton);
// 抬起鼠标左键
Thread.Sleep(300 + Randomx.GetId(500));
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None);
}
}
}
@@ -0,0 +1,197 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
using CefSharp;
using NaiBa.Util;
using NaiBa.Util.Extension;
using System.IO;
using System.Web;
using CefSharp.Handler;
using CefSharp.ResponseFilter;
using System.Collections.Concurrent;
using NaiBa.Util.Helper;
namespace _29WxOrder.Helper
{
public class BaseResourceRequestHandler : ResourceRequestHandler
{
public string UserAgent = string.Empty;
public Func<object, string, string, bool> BeforeResourceLoadEvent; // sender, requestUrl, postData, true(表示取消执行)
public Action<object, string, string, string> ResourceLoadCompleteEvent; // sender, requestUrl, postData, responseData
public Action<object, string, string, string> ResourceRedirectEvent; // sender, requestUrl, postData, redirectUrl
public string[] ResourceContentTypeRegister = new string[] { "application/json", "javascript", "application/x-www-form-urlencoded", "text/html", "text/plain" };
public string GetCharSet(string charSet)
{
if (string.IsNullOrEmpty(charSet))
{
charSet = "UTF-8";
}
return charSet;
}
public string GetPostData(IRequest request)
{
string postData = string.Empty;
if (request.PostData != null && request.PostData.Elements.Any())
{
for (int i = 0; i < request.PostData.Elements.Count; i++)
{
try
{
string charset = GetCharSet(request.GetCharSet());
try
{
if (request.PostData.Elements[i].Type != PostDataElementType.Empty)
postData += request.PostData.Elements[i].GetBody(charset);
}
catch (Exception ex) { }
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
}
}
return postData;
}
}
public class CustomResourceRequestHandler : BaseResourceRequestHandler
{
private ConcurrentDictionary<ulong, MemoryStream> responseDict = new ConcurrentDictionary<ulong, MemoryStream>();
protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
Uri url;
if (Uri.TryCreate(request.Url, UriKind.Absolute, out url) == false)
{
//If we're unable to parse the Uri then cancel the request
// avoid throwing any exceptions here as we're being called by unmanaged code
return CefReturnValue.Cancel;
}
if (!string.IsNullOrEmpty(UserAgent))
{
var headers = request.Headers;
headers["User-Agent"] = UserAgent;
request.Headers = headers;
}
//NOTE: If you do not wish to implement this method returning false is the default behaviour
// We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource.
//callback.Dispose();
//return false;
//NOTE: When executing the callback in an async fashion need to check to see if it's disposed
if (!callback.IsDisposed)
{
using (callback)
{
if (request.Method == "POST")
{
try
{
string postData = GetPostData(request);
bool? result = BeforeResourceLoadEvent?.Invoke(chromiumWebBrowser, request.Url, postData);
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
}
}
}
return CefReturnValue.Continue;
}
protected override void OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{
try
{
if (ResourceRedirectEvent != null)
{
string postData = GetPostData(request);
ResourceRedirectEvent.Invoke(chromiumWebBrowser, request.Url, postData, newUrl);
}
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
}
protected override bool OnProtocolExecution(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return request.Url.StartsWith("mailto");
}
protected override bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
//NOTE: You cannot modify the response, only the request
// You can now access the headers
//var headers = response.Headers;
return false;
}
protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
try
{
string contentType = response.Headers["Content-Type"];
if (string.IsNullOrEmpty(contentType))
{
return null;
}
if (ResourceContentTypeRegister.Contains(contentType) || ResourceContentTypeRegister.Where(p => contentType.Contains(p)).Any())
{
if (ResourceLoadCompleteEvent != null)
{
MemoryStream memoryStream = new MemoryStream();
responseDict.TryAdd(request.Identifier, memoryStream);
return new StreamResponseFilter(memoryStream);
}
}
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
return null;
}
protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
try
{
if (responseDict.ContainsKey(request.Identifier))
{
MemoryStream memoryStream = null;
responseDict.TryRemove(request.Identifier, out memoryStream);
if (memoryStream != null)
{
byte[] data = memoryStream.ToArray();
string charset = GetCharSet(response.Charset);
var responseData = Encoding.GetEncoding(charset).GetString(data);
string postData = GetPostData(request);
ResourceLoadCompleteEvent.Invoke(chromiumWebBrowser, request.Url, postData, responseData);
}
}
}
catch (Exception ex)
{
LogHelper.WriteWithTime(ex);
}
}
}
}
+82
View File
@@ -0,0 +1,82 @@
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _29WxOrder.Helper
{
public enum PopWindowType
{
NewWindow,
SelfWindow,
NewTitleWindow,
}
public class PopData
{
public string Url { get; set; }
}
public class LifeSpanHandlerLocal : ILifeSpanHandler
{
private PopWindowType popWindowType = PopWindowType.NewWindow;
public event EventHandler<PopData> OnPop;
public ChromiumWebBrowser parent;
public LifeSpanHandlerLocal()
{
}
public LifeSpanHandlerLocal(PopWindowType _popWindowType)
{
popWindowType = _popWindowType;
}
public bool DoClose(IWebBrowser browserControl, IBrowser browser)
{
return false;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
if (!browser.IsDisposed && browser.IsPopup)
{
//GlobalFormConfig.MainFormInstance.InvokeOnUiThreadIfRequired(() =>
//{
// // 修改窗体左上角的图标
// IntPtr handle = browser.GetHost().GetWindowHandle();
// PInvokeHelper.SendMessage(handle, 0x80, IntPtr.Zero, NaiBaGlobalConfig.SoftwareConfig.LogoIcon.Handle);
//});
}
}
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)
{
if (popWindowType == PopWindowType.NewWindow)
{
newBrowser = null;
return false;
}
else if (popWindowType == PopWindowType.NewTitleWindow)
{
PopData p = new PopData() { Url = targetUrl };
if (OnPop != null)
OnPop(this, p);
newBrowser = null;
return true;
}
else
{
// 不打开新的 tab
browser.MainFrame.LoadUrl(targetUrl);
newBrowser = null;
return true;
}
}
}
}