diff --git a/daemon/daemon.go b/daemon/daemon.go index 65e10b3..e7d27df 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -1967,29 +1967,28 @@ func (d *Daemon) selectElement(tabID, selector, value string, selectionTimeout, // If text selection failed, use JavaScript as fallback // This handles both option value and option text selection script := fmt.Sprintf(` - (function() { - var select = document.querySelector("%s"); - if (!select) return null; - + var select = document.querySelector("%s"); + var result = null; + if (select) { // First try to select by value select.value = "%s"; if (select.value === "%s") { select.dispatchEvent(new Event('change', { bubbles: true })); - return select.value; - } - - // If value selection failed, try to find by text content - var options = select.options; - for (var i = 0; i < options.length; i++) { - if (options[i].text === "%s") { - select.selectedIndex = i; - select.dispatchEvent(new Event('change', { bubbles: true })); - return select.value; + result = select.value; + } else { + // If value selection failed, try to find by text content + var options = select.options; + for (var i = 0; i < options.length; i++) { + if (options[i].text === "%s") { + select.selectedIndex = i; + select.dispatchEvent(new Event('change', { bubbles: true })); + result = select.value; + break; + } } } - - return null; - })() + } + result; `, selector, value, value, value) // Execute JavaScript and get the result @@ -3602,29 +3601,28 @@ func (d *Daemon) interactMultiple(tabID, interactionsJSON string, timeout int) ( // If text selection failed, use JavaScript as fallback // This handles both option value and option text selection script := fmt.Sprintf(` - (function() { - var select = document.querySelector("%s"); - if (!select) return null; - + var select = document.querySelector("%s"); + var result = null; + if (select) { // First try to select by value select.value = "%s"; if (select.value === "%s") { select.dispatchEvent(new Event('change', { bubbles: true })); - return select.value; - } - - // If value selection failed, try to find by text content - var options = select.options; - for (var i = 0; i < options.length; i++) { - if (options[i].text === "%s") { - select.selectedIndex = i; - select.dispatchEvent(new Event('change', { bubbles: true })); - return select.value; + result = select.value; + } else { + // If value selection failed, try to find by text content + var options = select.options; + for (var i = 0; i < options.length; i++) { + if (options[i].text === "%s") { + select.selectedIndex = i; + select.dispatchEvent(new Event('change', { bubbles: true })); + result = select.value; + break; + } } } - - return null; - })() + } + result; `, interaction.Selector, interaction.Value, interaction.Value, interaction.Value) // Execute JavaScript and get the result @@ -3824,29 +3822,28 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in // If text selection failed, use JavaScript as fallback // This handles both option value and option text selection script := fmt.Sprintf(` - (function() { - var select = document.querySelector("%s"); - if (!select) return null; - + var select = document.querySelector("%s"); + var result = null; + if (select) { // First try to select by value select.value = "%s"; if (select.value === "%s") { select.dispatchEvent(new Event('change', { bubbles: true })); - return select.value; - } - - // If value selection failed, try to find by text content - var options = select.options; - for (var i = 0; i < options.length; i++) { - if (options[i].text === "%s") { - select.selectedIndex = i; - select.dispatchEvent(new Event('change', { bubbles: true })); - return select.value; + result = select.value; + } else { + // If value selection failed, try to find by text content + var options = select.options; + for (var i = 0; i < options.length; i++) { + if (options[i].text === "%s") { + select.selectedIndex = i; + select.dispatchEvent(new Event('change', { bubbles: true })); + result = select.value; + break; + } } } - - return null; - })() + } + result; `, fieldResult.Selector, fieldValue, fieldValue, fieldValue) // Execute JavaScript and get the result