(limit: number, pattern: string | null, flags: string)
| 251 | } |
| 252 | |
| 253 | function buildAgentPageTextScript(limit: number, pattern: string | null, flags: string): string { |
| 254 | const payload = { limit, pattern, flags }; |
| 255 | return buildEvalScript(` |
| 256 | const payload = ${JSON.stringify(payload)}; |
| 257 | const safeString = (value) => (typeof value === "string" ? value : ""); |
| 258 | const bodyText = safeString(document.body ? document.body.innerText : ""); |
| 259 | const inputText = Array.from(document.querySelectorAll("input, textarea, [contenteditable='true']")) |
| 260 | .map((element) => { |
| 261 | if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") { |
| 262 | return safeString(element.value); |
| 263 | } |
| 264 | return safeString(element.textContent); |
| 265 | }) |
| 266 | .filter(Boolean) |
| 267 | .join("\n"); |
| 268 | const combined = [bodyText, inputText].filter(Boolean).join("\n\n"); |
| 269 | const maxSize = Number.isFinite(payload.limit) ? payload.limit : ${DEFAULT_PAGE_TEXT_LIMIT}; |
| 270 | const text = combined.slice(0, Math.max(0, maxSize)); |
| 271 | let matches = []; |
| 272 | if (payload.pattern) { |
| 273 | try { |
| 274 | const re = new RegExp(payload.pattern, payload.flags || "i"); |
| 275 | let match; |
| 276 | while ((match = re.exec(text)) && matches.length < 50) { |
| 277 | matches.push(match[0]); |
| 278 | if (!re.global) break; |
| 279 | } |
| 280 | } catch { |
| 281 | matches = []; |
| 282 | } |
| 283 | } |
| 284 | return { |
| 285 | url: location.href, |
| 286 | title: document.title, |
| 287 | text, |
| 288 | matches, |
| 289 | }; |
| 290 | `); |
| 291 | } |
| 292 | |
| 293 | function buildAgentListScript(selector: string, limit: number): string { |
| 294 | const payload = { selector, limit }; |
no test coverage detected