🎨 优化前端渲染功能(html状态栏通信存在问题)

Signed-off-by: Echo <1711788888@qq.com>
This commit is contained in:
2026-03-03 04:28:33 +08:00
parent cbb4034a91
commit c267b6c76e
4 changed files with 662 additions and 50 deletions

View File

@@ -222,7 +222,13 @@ func (s *RegexScriptService) ExecuteScript(script *app.RegexScript, text string,
global.GVA_LOG.Warn("正则表达式编译失败", zap.String("pattern", script.FindRegex), zap.Error(err))
return text, err
}
result = re.ReplaceAllString(result, script.ReplaceWith)
// 特殊处理:如果正则匹配 <Status_block>,需要提取 YAML 并注入到 HTML 模板中
if strings.Contains(script.FindRegex, "Status_block") {
result = s.replaceStatusBlockWithHTML(result, script.ReplaceWith, re)
} else {
result = re.ReplaceAllString(result, script.ReplaceWith)
}
}
// 3. 修剪字符串
@@ -237,6 +243,35 @@ func (s *RegexScriptService) ExecuteScript(script *app.RegexScript, text string,
return result, nil
}
// replaceStatusBlockWithHTML 替换 <Status_block> 为 HTML 模板,并注入 YAML 数据
func (s *RegexScriptService) replaceStatusBlockWithHTML(text string, htmlTemplate string, statusBlockRegex *regexp.Regexp) string {
return statusBlockRegex.ReplaceAllStringFunc(text, func(match string) string {
// 提取 YAML 数据
yamlRegex := regexp.MustCompile(`<Status_block>\s*([\s\S]*?)\s*</Status_block>`)
yamlMatches := yamlRegex.FindStringSubmatch(match)
if len(yamlMatches) < 2 {
global.GVA_LOG.Warn("无法提取 Status_block 中的 YAML 数据")
return match
}
yamlData := strings.TrimSpace(yamlMatches[1])
// 在 HTML 模板中查找 <script id="yaml-data-source" type="text/yaml"></script>
// 并将 YAML 数据注入其中
injectedHTML := strings.Replace(
htmlTemplate,
`<script id="yaml-data-source" type="text/yaml"></script>`,
fmt.Sprintf(`<script id="yaml-data-source" type="text/yaml">%s</script>`, yamlData),
1,
)
global.GVA_LOG.Info(fmt.Sprintf("[正则脚本] 已将 Status_block YAML 数据注入到 HTML 模板YAML 长度: %d", len(yamlData)))
return injectedHTML
})
}
// substituteMacros 替换宏变量
func (s *RegexScriptService) substituteMacros(text string, userName string, charName string) string {
result := text