MCPcopy Create free account
hub / github.com/browserwing/browserwing / Play

Method Play

backend/sdk/script.go:173–251  ·  view source on GitHub ↗

Play 执行脚本

(ctx context.Context, scriptID string)

Source from the content-addressed store, hash-verified

171
172// Play 执行脚本
173func (sc *ScriptClient) Play(ctx context.Context, scriptID string) (*ScriptExecution, error) {
174 if sc.client.browserManager == nil {
175 return nil, fmt.Errorf("browser manager not initialized")
176 }
177
178 if !sc.client.browserManager.IsRunning() {
179 return nil, fmt.Errorf("browser is not running, please start browser first")
180 }
181
182 // 获取脚本
183 dbScript, err := sc.client.db.GetScript(scriptID)
184 if err != nil {
185 return nil, fmt.Errorf("failed to get script: %w", err)
186 }
187
188 if dbScript == nil {
189 return nil, fmt.Errorf("script not found: %s", scriptID)
190 }
191
192 // 执行脚本(使用当前实例,传空字符串)
193 result, page, err := sc.client.browserManager.PlayScript(ctx, dbScript, "")
194 if err != nil {
195 return nil, fmt.Errorf("failed to play script: %w", err)
196 }
197
198 // 构建执行结果
199 execution := &ScriptExecution{
200 ID: uuid.New().String(),
201 ScriptID: scriptID,
202 ScriptName: dbScript.Name,
203 Status: "success",
204 StartTime: time.Now().Unix(),
205 EndTime: time.Now().Unix(),
206 Duration: 0,
207 }
208
209 if result.Success {
210 execution.Status = "success"
211 // 转换 ExtractedData
212 if result.ExtractedData != nil {
213 strData := make(map[string]string)
214 for k, v := range result.ExtractedData {
215 if str, ok := v.(string); ok {
216 strData[k] = str
217 }
218 }
219 execution.ExtractedData = strData
220 }
221 } else {
222 execution.Status = "failed"
223 execution.Error = result.Message
224 }
225
226 // 保存执行记录到数据库
227 dbExecution := &models.ScriptExecution{
228 ID: execution.ID,
229 ScriptID: execution.ScriptID,
230 ScriptName: execution.ScriptName,

Callers 2

mainFunction · 0.80
mainFunction · 0.80

Calls 5

SaveScriptExecutionMethod · 0.80
IsRunningMethod · 0.65
GetScriptMethod · 0.65
PlayScriptMethod · 0.65
CloseActivePageMethod · 0.65

Tested by

no test coverage detected