{"record":{"id":"6fd08bb78d30aa4d","repo":"jackwener/OpenCLI","slug":"maxscrolls-must-be-an-integer-between-1-and-30-go","errorCode":null,"errorMessage":"maxScrolls must be an integer between 1 and 30, got ${JSON.stringify(maxScrolls)}","messagePattern":"maxScrolls must be an integer between 1 and 30, got (.+?)","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/ctrip/utils.js","lineNumber":466,"sourceCode":"      })()\n    `;\n}\n\n/**\n * Build a scroll-until-enough IIFE for flights/hotels DOM-card pagination.\n *\n * Mirrors `clis/xiaohongshu/search.js#buildScrollUntilJs` (PR #1487) — counts a\n * caller-supplied row selector, scrolls until count >= target / DOM plateau /\n * maxScrolls. Returns final row count so the caller can decide whether to\n * surface an EmptyResultError. (xiaohongshu's helper hardcodes\n * `section.note-item`; this generic version takes a selector.)\n */\nexport function buildScrollUntilJs(rowSelector, targetCount, maxScrolls = 8) {\n    if (!Number.isInteger(targetCount) || targetCount < 1 || targetCount > 100) {\n        throw new ArgumentError(`targetCount must be an integer between 1 and 100, got ${JSON.stringify(targetCount)}`);\n    }\n    if (!Number.isInteger(maxScrolls) || maxScrolls < 1 || maxScrolls > 30) {\n        throw new ArgumentError(`maxScrolls must be an integer between 1 and 30, got ${JSON.stringify(maxScrolls)}`);\n    }\n    return `\n      (async () => {\n        const sel = ${JSON.stringify(rowSelector)};\n        const isVisible = (el) => {\n          const style = window.getComputedStyle(el);\n          if (style.display === 'none' || style.visibility === 'hidden' || Number(style.opacity) === 0) return false;\n          const rect = el.getBoundingClientRect();\n          return rect.width > 0 && rect.height > 0;\n        };\n        const countItems = () => Array.from(document.querySelectorAll(sel)).filter(isVisible).length;\n        let lastCount = countItems();\n        let plateauRounds = 0;\n        for (let i = 0; i < ${maxScrolls}; i++) {\n          if (countItems() >= ${targetCount}) break;\n          const lastHeight = document.body.scrollHeight;\n          window.scrollTo(0, lastHeight);\n          await new Promise((resolve) => {","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/ctrip/utils.js#L448-L484","documentation":"buildScrollUntilJs also validates its third parameter maxScrolls (default 8): it must be an integer between 1 and 30 inclusive. This caps how many scroll iterations the generated page script will perform, preventing unbounded scrolling. Non-integer or out-of-range values raise ArgumentError.","triggerScenarios":"Calling buildScrollUntilJs with maxScrolls = 0, negative, fractional (1.5), non-numeric types, or values over 30 such as 50 or 100.","commonSituations":"Consumers trying to 'scroll forever' by passing a huge maxScrolls, passing 0 expecting unlimited scrolls, or forwarding raw unparsed strings from CLI/config.","solutions":["Pass an integer between 1 and 30 (or omit it to use the default 8).","Clamp the value: Math.min(30, Math.max(1, Math.floor(Number(x)))).","If the target count needs more scrolls, increase targetCount within limits or re-invoke the helper."],"exampleFix":"// before\nbuildScrollUntilJs('.row', 20, 100)\n// after\nbuildScrollUntilJs('.row', 20, 30)","handlingStrategy":"validation","validationCode":"function clampMaxScrolls(v = 8) {\n  const n = Number(v);\n  if (!Number.isInteger(n) || n < 1 || n > 30) throw new Error('maxScrolls must be an integer 1-30');\n  return n;\n}","typeGuard":"const isValidMaxScrolls = (v) => Number.isInteger(v) && v >= 1 && v <= 30;","tryCatchPattern":"try {\n  return buildScrollUntilJs(sel, count, maxScrolls);\n} catch (e) {\n  if (e instanceof ArgumentError && e.message.includes('maxScrolls')) {\n    return buildScrollUntilJs(sel, count, 8); // library default\n  }\n  throw e;\n}","preventionTips":["Omit maxScrolls to use the default 8 unless you have a reason to change it.","Clamp values into 1-30 rather than trying to 'scroll forever'.","Validate integer-ness before passing values from config files."],"tags":["validation","argument-error","browser-automation","integer-validation"],"backgroundTag":"invalid-cli-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}