45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace WxAgent.Core;
|
|
|
|
public sealed record UiNodeSnapshot(
|
|
string ControlType,
|
|
string Name,
|
|
string AutomationId,
|
|
bool IsEnabled,
|
|
string Bounds,
|
|
IReadOnlyList<UiNodeSnapshot> Children);
|
|
|
|
public static class UiSnapshotSanitizer
|
|
{
|
|
private static readonly HashSet<string> SafeAutomationIds = new(StringComparer.Ordinal)
|
|
{
|
|
"MainView",
|
|
"MainView.main_tabbar",
|
|
"MainView.main_tabbar.tabbar_setting",
|
|
"session_list",
|
|
"chat_message_page",
|
|
"chat_message_list",
|
|
"chat_message_list.qt_scrollarea_viewport.chat_bubble_item_view",
|
|
"chat_input_field",
|
|
"chat_input_field.qt_scrollarea_viewport",
|
|
"tool_bar_accessible"
|
|
};
|
|
|
|
public static string SanitizeName(string? value) => Sanitize(value, [], "text");
|
|
|
|
public static string SanitizeAutomationId(string? value) => Sanitize(value, SafeAutomationIds, "id");
|
|
|
|
private static string Sanitize(string? value, HashSet<string> allowList, string label)
|
|
{
|
|
if (string.IsNullOrEmpty(value) || allowList.Contains(value))
|
|
{
|
|
return value ?? string.Empty;
|
|
}
|
|
|
|
var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(value));
|
|
return $"[{label}:{value.Length}:{Convert.ToHexString(bytes.AsSpan(0, 4)).ToLowerInvariant()}]";
|
|
}
|
|
}
|