feat: 改进http,返回header和status_code,并实现url级别细粒度控制和ui提示 (#2332)

This commit is contained in:
NyaMisty
2025-10-13 20:26:56 +08:00
committed by GitHub
parent 7ab80da472
commit 81ab66acfa
6 changed files with 179 additions and 23 deletions

View File

@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.Json.Serialization;
using System.Windows.Documents;
using System.Windows.Media;
using BetterGenshinImpact.Core.Script.Group;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.Service.Notification;
@@ -37,13 +39,76 @@ public class ScriptGroupProjectEditorViewModel : ObservableObject
public bool? AllowJsHTTP
{
get => _project.AllowJsHTTP;
get
{
return _project.AllowJsHTTP;
}
set
{
_project.AllowJsHTTP = value;
// 为了避免误用AllowJsHTTP禁止set通过更新Hash来控制
// 脚本作者更新时如果Hash变更会自动禁用http权限避免安全风险
if (value == null || value == false)
{
_project.AllowJsHTTPHash = null;
}
else
{
_project.AllowJsHTTPHash = _project.GetHttpAllowedUrlsHash();
}
OnPropertyChanged();
}
}
public record JsText(string Text, Brush Color);
public List<JsText> JsHTTPInfoText
{
get
{
if (_project.Project == null)
{
_project.BuildScriptProjectRelation();
}
if (_project.Project == null)
{
return new List<JsText>
{
new JsText("当前脚本项目未加载", Brushes.Red)
};
}
var urls = _project.Project.Manifest?.HttpAllowedUrls ?? [];
if (urls.Length == 0)
{
return new List<JsText>
{
new JsText("当前脚本无需使用HTTP资源", Brushes.Green)
};
}
return new List<JsText>
{
new JsText($"当前脚本使用 {urls.Length} 个HTTP资源", Brushes.OrangeRed)
};
}
}
public record JsLine(string Text, Brush Color);
public List<JsLine> JsHTTPInfo
{
get
{
if (_project.Project == null)
{
_project.BuildScriptProjectRelation();
}
var urls = _project.Project?.Manifest?.HttpAllowedUrls ?? [];
var blocks = new List<JsLine>();
foreach (var url in urls)
{
blocks.Add(new JsLine(url, Brushes.OrangeRed));
}
return blocks;
}
}
public string Status
{
@@ -68,7 +133,7 @@ public class ScriptGroupProjectEditorViewModel : ObservableObject
{
OnPropertyChanged(nameof(AllowJsNotification));
}
if (e.PropertyName == nameof(ScriptGroupProject.AllowJsHTTP))
if (e.PropertyName == nameof(ScriptGroupProject.AllowJsHTTPHash))
{
OnPropertyChanged(nameof(AllowJsHTTP));
}