mirror of
https://github.com/black-ant/Ant-Browser.git
synced 2026-07-14 18:48:55 +08:00
channel: master
version: 1.2.0
source-ref: D:\code\open_source\ant-chrome master
source-commit: f3d7ec5
merged-public-base: 3d264eb
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package backend
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"ant-chrome/backend/internal/automation"
|
|
)
|
|
|
|
func (a *App) automationScriptRunStore() *automation.ScriptRunStore {
|
|
return automation.NewScriptRunStore(a.resolveAppPath(filepath.ToSlash(filepath.Join("data", "automation", "runs"))))
|
|
}
|
|
|
|
func (a *App) AutomationScriptRunList(limit int) ([]automation.ScriptRunRecord, error) {
|
|
return a.automationScriptRunStore().List(limit)
|
|
}
|
|
|
|
func (a *App) AutomationScriptRun(scriptID string) (*automation.ScriptRunRecord, error) {
|
|
return a.AutomationScriptRunWithOptions(automation.ScriptRunRequest{
|
|
ScriptID: scriptID,
|
|
UseScriptSelector: true,
|
|
UseScriptParams: true,
|
|
})
|
|
}
|
|
|
|
func (a *App) AutomationScriptRunWithOptions(input automation.ScriptRunRequest) (*automation.ScriptRunRecord, error) {
|
|
startedAt := time.Now()
|
|
run := automation.ScriptRunRecord{
|
|
ScriptID: strings.TrimSpace(input.ScriptID),
|
|
Status: "failed",
|
|
StartedAt: startedAt.Format(time.RFC3339),
|
|
}
|
|
|
|
script, err := a.automationScriptStore().Get(run.ScriptID)
|
|
if err != nil {
|
|
run.Summary = "脚本读取失败"
|
|
run.Error = err.Error()
|
|
return a.finalizeAutomationScriptRun(run, startedAt)
|
|
}
|
|
|
|
run.ScriptName = script.Name
|
|
run.ScriptType = script.Type
|
|
|
|
switch script.Type {
|
|
case "launch-api":
|
|
resultText, summary, errText := a.runLaunchAPIScript(script, input)
|
|
run.ResultText = resultText
|
|
run.Summary = summary
|
|
run.Error = errText
|
|
if errText == "" {
|
|
run.Status = "success"
|
|
}
|
|
case "playwright-cdp":
|
|
resultText, summary, errText := a.runPlaywrightScript(script, input)
|
|
run.ResultText = resultText
|
|
run.Summary = summary
|
|
run.Error = errText
|
|
if errText == "" {
|
|
run.Status = "success"
|
|
}
|
|
default:
|
|
run.Summary = "当前脚本类型暂不支持直接执行"
|
|
run.Error = fmt.Sprintf("script type %q is not supported yet", script.Type)
|
|
}
|
|
|
|
return a.finalizeAutomationScriptRun(run, startedAt)
|
|
}
|
|
|
|
func (a *App) finalizeAutomationScriptRun(run automation.ScriptRunRecord, startedAt time.Time) (*automation.ScriptRunRecord, error) {
|
|
run.FinishedAt = time.Now().Format(time.RFC3339)
|
|
run.DurationMs = time.Since(startedAt).Milliseconds()
|
|
saved, err := a.automationScriptRunStore().Save(run)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &saved, nil
|
|
}
|