jackwener/OpenCLI · error · ArgumentError

targetCount must be an integer between 1 and 100, got ${JSON

Error message

targetCount must be an integer between 1 and 100, got ${JSON.stringify(targetCount)}

What it means

buildScrollUntilJs generates an in-page scroll-until-N-rows-appear script for browser scraping. Before emitting the JS, it validates that targetCount is an integer in the inclusive range 1-100; otherwise it throws ArgumentError. This guards the generated script from nonsensical or runaway scroll targets.

Source

Thrown at clis/ctrip/utils.js:463

          });
        });
        return rows;
      })()
    `;
}

/**
 * Build a scroll-until-enough IIFE for flights/hotels DOM-card pagination.
 *
 * Mirrors `clis/xiaohongshu/search.js#buildScrollUntilJs` (PR #1487) — counts a
 * caller-supplied row selector, scrolls until count >= target / DOM plateau /
 * maxScrolls. Returns final row count so the caller can decide whether to
 * surface an EmptyResultError. (xiaohongshu's helper hardcodes
 * `section.note-item`; this generic version takes a selector.)
 */
export function buildScrollUntilJs(rowSelector, targetCount, maxScrolls = 8) {
    if (!Number.isInteger(targetCount) || targetCount < 1 || targetCount > 100) {
        throw new ArgumentError(`targetCount must be an integer between 1 and 100, got ${JSON.stringify(targetCount)}`);
    }
    if (!Number.isInteger(maxScrolls) || maxScrolls < 1 || maxScrolls > 30) {
        throw new ArgumentError(`maxScrolls must be an integer between 1 and 30, got ${JSON.stringify(maxScrolls)}`);
    }
    return `
      (async () => {
        const sel = ${JSON.stringify(rowSelector)};
        const isVisible = (el) => {
          const style = window.getComputedStyle(el);
          if (style.display === 'none' || style.visibility === 'hidden' || Number(style.opacity) === 0) return false;
          const rect = el.getBoundingClientRect();
          return rect.width > 0 && rect.height > 0;
        };
        const countItems = () => Array.from(document.querySelectorAll(sel)).filter(isVisible).length;
        let lastCount = countItems();
        let plateauRounds = 0;
        for (let i = 0; i < ${maxScrolls}; i++) {
          if (countItems() >= ${targetCount}) break;

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass an integer between 1 and 100 as targetCount (e.g. 10 or 20 rows).
  2. Clamp user-supplied values: Math.min(100, Math.max(1, Math.floor(Number(x)))).
  3. Parse CLI strings with Number()/parseInt before calling, and validate integer-ness first.
  4. If you need more than 100 rows, call the scroll helper multiple times or paginate differently.

Example fix

// before
buildScrollUntilJs('div.hotel-item', '500')
// after
buildScrollUntilJs('div.hotel-item', Math.min(100, Math.max(1, parseInt(raw, 10))))
Defensive patterns

Strategy: validation

Validate before calling

function clampTargetCount(v) {
  const n = Number(v);
  if (!Number.isInteger(n) || n < 1 || n > 100) throw new Error('targetCount must be an integer 1-100');
  return n;
}

Type guard

const isValidTargetCount = (v) => Number.isInteger(v) && v >= 1 && v <= 100;

Try / catch

try {
  const js = buildScrollUntilJs(sel, count);
} catch (e) {
  if (e instanceof ArgumentError && e.message.includes('targetCount')) {
    return buildScrollUntilJs(sel, 20); // safe default
  }
  throw e;
}

Prevention

When it happens

Trigger: Programmatically calling buildScrollUntilJs (used by renderedCardCount/js helpers) with targetCount = 0, negative values, non-integers like 2.5, NaN, undefined, strings like '10', or values above 100 such as 500.

Common situations: Library consumers computing the count from user flags without clamping, passing a string parsed from CLI args, or requesting 'scroll until everything is loaded' with an unbounded number.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/268629207da9ad63. Report an issue: GitHub.