This commit is contained in:
Josh at WLTechBlog 2025-08-19 10:06:40 -05:00
parent 8fc4f76b28
commit 63860db70b
1 changed files with 19 additions and 60 deletions

View File

@ -3698,68 +3698,27 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in
Success: false, Success: false,
} }
// Try different selector strategies for the field // Try different selector strategies for the field (fast, no individual timeouts)
var element *rod.Element var element *rod.Element
var selectors []string selectors := []string{
fmt.Sprintf("[name='%s']", fieldName),
// If we have a form, search within it first fmt.Sprintf("#%s", fieldName),
if form != nil { fmt.Sprintf("[id='%s']", fieldName),
selectors = []string{ fieldName, // In case it's already a full selector
fmt.Sprintf("[name='%s']", fieldName),
fmt.Sprintf("#%s", fieldName),
fmt.Sprintf("[id='%s']", fieldName),
fieldName, // In case it's already a full selector
}
for _, selector := range selectors {
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
element, err = form.Context(ctx).Element(selector)
if err == nil {
fieldResult.Selector = selector
cancel() // Cancel context now that we found the element
break
}
cancel() // Cancel if element not found
} else {
element, err = form.Element(selector)
if err == nil {
fieldResult.Selector = selector
break
}
}
}
} }
// If not found in form or no form, search in entire page // Search for element (try form first if available, then page)
if element == nil { for _, selector := range selectors {
// Generate selectors if not already done // Try without timeout first (should be instant if element exists)
if selectors == nil { if form != nil {
selectors = []string{ element, err = form.Element(selector)
fmt.Sprintf("[name='%s']", fieldName), } else {
fmt.Sprintf("#%s", fieldName), element, err = page.Element(selector)
fmt.Sprintf("[id='%s']", fieldName),
fieldName, // In case it's already a full selector
}
} }
for _, selector := range selectors { if err == nil {
if timeout > 0 { fieldResult.Selector = selector
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) break
element, err = page.Context(ctx).Element(selector)
if err == nil {
fieldResult.Selector = selector
cancel() // Cancel context now that we found the element
break
}
cancel() // Cancel if element not found
} else {
element, err = page.Element(selector)
if err == nil {
fieldResult.Selector = selector
break
}
}
} }
} }
@ -3770,8 +3729,8 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in
continue continue
} }
// Determine the element type and use appropriate action // Determine the element type using rod's built-in method (much faster than Eval)
tagName, err := element.Eval("() => this.tagName.toLowerCase()") tagName, err := element.Property("tagName")
if err != nil { if err != nil {
fieldResult.Error = fmt.Sprintf("failed to get element tag name: %v", err) fieldResult.Error = fmt.Sprintf("failed to get element tag name: %v", err)
result.FilledFields = append(result.FilledFields, fieldResult) result.FilledFields = append(result.FilledFields, fieldResult)
@ -3780,7 +3739,7 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in
} }
// Handle different element types // Handle different element types
if tagName.Value.String() == "select" { if strings.ToLower(tagName.Str()) == "select" {
// Use select action for select elements // Use select action for select elements
fieldResult.Action = "select" fieldResult.Action = "select"
err = element.Select([]string{fieldValue}, true, rod.SelectorTypeText) err = element.Select([]string{fieldValue}, true, rod.SelectorTypeText)