( selector: string, indexValue: number, value: string | undefined, label: string | undefined, optionIndex: number | undefined )
| 199 | } |
| 200 | |
| 201 | function buildAgentSelectScript( |
| 202 | selector: string, |
| 203 | indexValue: number, |
| 204 | value: string | undefined, |
| 205 | label: string | undefined, |
| 206 | optionIndex: number | undefined |
| 207 | ): string { |
| 208 | const payload = { |
| 209 | selector, |
| 210 | index: indexValue, |
| 211 | value: value ?? null, |
| 212 | label: label ?? null, |
| 213 | optionIndex: Number.isFinite(optionIndex) ? optionIndex : null, |
| 214 | }; |
| 215 | return buildEvalScript(` |
| 216 | const payload = ${JSON.stringify(payload)}; |
| 217 | let matches = []; |
| 218 | try { |
| 219 | matches = Array.from(document.querySelectorAll(payload.selector)); |
| 220 | } catch { |
| 221 | return { ok: false, error: "Invalid selector" }; |
| 222 | } |
| 223 | const element = matches[payload.index]; |
| 224 | if (!element) return { ok: false, error: "Element not found" }; |
| 225 | if (!element.tagName || element.tagName.toUpperCase() !== "SELECT") { |
| 226 | return { ok: false, error: "Element is not a select" }; |
| 227 | } |
| 228 | const options = Array.from(element.options || []); |
| 229 | let chosen = null; |
| 230 | if (payload.value !== null) { |
| 231 | chosen = options.find((option) => option.value === payload.value) || null; |
| 232 | } |
| 233 | if (!chosen && payload.label !== null) { |
| 234 | const target = payload.label.trim(); |
| 235 | chosen = options.find((option) => (option.label || option.textContent || "").trim() === target) || null; |
| 236 | } |
| 237 | if (!chosen && payload.optionIndex !== null) { |
| 238 | chosen = options[payload.optionIndex] || null; |
| 239 | } |
| 240 | if (!chosen) return { ok: false, error: "Option not found" }; |
| 241 | element.value = chosen.value; |
| 242 | chosen.selected = true; |
| 243 | element.dispatchEvent(new Event("input", { bubbles: true })); |
| 244 | element.dispatchEvent(new Event("change", { bubbles: true })); |
| 245 | return { |
| 246 | ok: true, |
| 247 | value: element.value, |
| 248 | label: (chosen.label || chosen.textContent || "").trim(), |
| 249 | }; |
| 250 | `); |
| 251 | } |
| 252 | |
| 253 | function buildAgentPageTextScript(limit: number, pattern: string | null, flags: string): string { |
| 254 | const payload = { limit, pattern, flags }; |
no test coverage detected