Skip to content

Commit dd7c8cb

Browse files
committed
优化 URL 和域名匹配逻辑
1 parent c11eae2 commit dd7c8cb

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

Core/UI/SearchWindow/InputData/UrlIdentifier.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,46 @@ public partial class UrlIdentifier : IInputDataIdentifier
2828

2929
private static IEnumerable<PluginCore.SearchWindow.InputData.InputData> MatchAndReturnUrlData(string? s)
3030
{
31-
if (s is null) yield break;
32-
if (DomainRegex().IsMatch(s) || UrlRegex().IsMatch(s))
31+
if (string.IsNullOrWhiteSpace(s)) yield break;
32+
33+
if (UrlRegex().IsMatch(s))
34+
{
35+
if (Uri.TryCreate(s, UriKind.Absolute, out var uri))
36+
yield return new PluginCore.SearchWindow.InputData.InputData
37+
{
38+
InputType = InputType.网址,
39+
Data = uri.ToString()
40+
};
41+
yield break;
42+
}
43+
44+
if (DomainRegex().IsMatch(s))
45+
{
3346
yield return new PluginCore.SearchWindow.InputData.InputData
3447
{
3548
InputType = InputType.网址,
3649
Data = s
3750
};
51+
yield break;
52+
}
3853

39-
if (Uri.TryCreate(s, UriKind.Absolute, out var uri))
54+
if (Uri.TryCreate(s, UriKind.Absolute, out var uri2) && IsWebScheme(uri2.Scheme))
4055
yield return new PluginCore.SearchWindow.InputData.InputData
4156
{
4257
InputType = InputType.网址,
43-
Data = uri.ToString()
58+
Data = uri2.ToString()
4459
};
4560
}
4661

47-
[GeneratedRegex("^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$")]
62+
private static bool IsWebScheme(string scheme)
63+
{
64+
return scheme.Equals("http", StringComparison.OrdinalIgnoreCase) ||
65+
scheme.Equals("https", StringComparison.OrdinalIgnoreCase);
66+
}
67+
68+
[GeneratedRegex(@"^(?=^.{3,255}$)(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$")]
4869
private static partial Regex DomainRegex();
4970

50-
[GeneratedRegex("^\\w+[^\\s]+(\\.[^\\s]+){1,}$")]
71+
[GeneratedRegex(@"^https?://\S+$")]
5172
private static partial Regex UrlRegex();
5273
}

0 commit comments

Comments
 (0)